6 changed files with 96 additions and 17 deletions
			
			
		@ -0,0 +1,56 @@ | 
				
			|||||
 | 
					#include "logger.h" | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#include <QDateTime> | 
				
			||||
 | 
					#include <QDir> | 
				
			||||
 | 
					#include <QObject> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					QFile* Logger::_logFile = Q_NULLPTR; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					QHash<QtMsgType, QString> Logger::_logTitles = { | 
				
			||||
 | 
					    {QtMsgType::QtDebugMsg, " Debug  "}, | 
				
			||||
 | 
					    {QtMsgType::QtInfoMsg, "  Info  "}, | 
				
			||||
 | 
					    {QtMsgType::QtWarningMsg, "Warning "}, | 
				
			||||
 | 
					    {QtMsgType::QtCriticalMsg, "Critical"}, | 
				
			||||
 | 
					    {QtMsgType::QtFatalMsg, " Fatal  "} | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					Logger::Logger(QString fileName) | 
				
			||||
 | 
					{ | 
				
			||||
 | 
					    _logFile = new QFile; | 
				
			||||
 | 
					    //_logFile->setFileName(fileName);
 | 
				
			||||
 | 
					    _logFile->setFileName(fileName); | 
				
			||||
 | 
					    _logFile->open(QIODevice::Append | QIODevice::Text); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    qInstallMessageHandler(Logger::messageOutput); | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					Logger::~Logger() | 
				
			||||
 | 
					{ | 
				
			||||
 | 
					    qDebug() << "Logger closed" << "\n"; | 
				
			||||
 | 
					    delete _logFile; | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					void Logger::makeFinishingLine() | 
				
			||||
 | 
					{ | 
				
			||||
 | 
					    _logFile->write( | 
				
			||||
 | 
					        "**************************************************************************************\n"); | 
				
			||||
 | 
					    _logFile->flush(); | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					void Logger::messageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) | 
				
			||||
 | 
					{ | 
				
			||||
 | 
					    QString log = QObject::tr("%1 | %2 | %3 | %4 | %5 | %6\n"). | 
				
			||||
 | 
					                  arg(QDateTime::currentDateTime().toString("yyyy/MM/dd - hh:mm:ss")). | 
				
			||||
 | 
					                  arg(Logger::_logTitles.value(type)). | 
				
			||||
 | 
					                  arg(context.line). | 
				
			||||
 | 
					                  arg(QString(context.file). | 
				
			||||
 | 
					                      section('\\', -1)). //File name without file path
 | 
				
			||||
 | 
					                  arg(QString(context.function). | 
				
			||||
 | 
					                      section('(', -2, -2). //Function name only
 | 
				
			||||
 | 
					                      section(' ', -1). | 
				
			||||
 | 
					                      section(':', -1)). | 
				
			||||
 | 
					                  arg(msg); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    _logFile->write(log.toLocal8Bit()); | 
				
			||||
 | 
					    _logFile->flush(); | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,23 @@ | 
				
			|||||
 | 
					#ifndef LOGGER_H | 
				
			||||
 | 
					#define LOGGER_H | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#include <QDebug> | 
				
			||||
 | 
					#include <QFile> | 
				
			||||
 | 
					#include <QHash> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					class Logger | 
				
			||||
 | 
					{ | 
				
			||||
 | 
					private: | 
				
			||||
 | 
					    static QFile* _logFile; | 
				
			||||
 | 
					    static QHash<QtMsgType, QString> _logTitles; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					public: | 
				
			||||
 | 
					    Logger(QString fileName = "./DefaultHatefLogFile.log"); | 
				
			||||
 | 
					    ~Logger(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    static void makeFinishingLine(); | 
				
			||||
 | 
					    static void messageOutput(QtMsgType type, const QMessageLogContext& context, | 
				
			||||
 | 
					                              const QString& msg); | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#endif //LOGGER_H
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue