st_logger.cpp 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include "st_logger.h"
#include <QTextStream>
namespace STMsgLogger{
	st_logger::st_logger(QObject *parent) :
		QObject(parent)
	{
		m_pLogFile = 0;
		m_bUseLogFile = true;
		m_nLogLevel = 2;
		m_nMaxFileSize = 256*1024*1024;
	}
	void st_logger::setMaxFileSize(int nSize)
	{
		if (nSize >=1024*1024 && nSize <=1024*1024*1024)
			m_nMaxFileSize = nSize;
	}
	int st_logger::maxFileSize() const
	{
		return m_nMaxFileSize;
	}
	int st_logger::logLevel() const
	{
		return m_nLogLevel;
	}
	void st_logger::setLogLevel(int lv)
	{
		if (lv >=0 && lv <=3)
			m_nLogLevel = lv;
	}

	bool st_logger::CreateNewLogFile(QCoreApplication * app)
	{
		bool res = false;
		QDateTime dtmCur = QDateTime::currentDateTime();
		m_currLogFileName = app->applicationDirPath() + "/Log/" + dtmCur.toString("yyyy_MM") + "/";
		QDir dir;
		dir.mkpath(m_currLogFileName);
		m_currLogFileName += dtmCur.toString("yyyy_MM_dd_HH_mm_ss_");
		m_currLogFileName += app->applicationName() + QString("(%1).txt").arg(app->applicationPid());
		if (m_pLogFile)
		{
			if (m_pLogFile->isOpen()==true)
				m_pLogFile->close();
			m_pLogFile->deleteLater();
			m_pLogFile = 0;
		}
		m_pLogFile = new QFile (m_currLogFileName,this);
		if (m_pLogFile)
		{
			if (m_pLogFile->open(QIODevice::WriteOnly)==false)
			{
				m_pLogFile->deleteLater();
				m_pLogFile = 0;
			}
			else
				res = true;
		}
		return res;
	}
	void st_logger::MessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
	{
62
		m_mutextLogger.lock();
63 64
		switch (type) {
		case QtDebugMsg:
65 66 67 68 69
			if (m_nLogLevel < 3)
			{
				m_mutextLogger.unlock();
				return;
			}
70 71
			break;
		case QtWarningMsg:
72 73 74 75 76
			if (m_nLogLevel < 2)
			{
				m_mutextLogger.unlock();
				return;
			}
77 78
			break;
		case QtCriticalMsg:
79 80 81 82 83
			if (m_nLogLevel < 1)
			{
				m_mutextLogger.unlock();
				return;
			}
84 85
			break;
		case QtFatalMsg:
86 87 88 89 90
			if (m_nLogLevel < 0)
			{
				m_mutextLogger.unlock();
				return;
			}
91 92
			break;
		default:
93 94 95 96 97
			if (m_nLogLevel < 3)
			{
				m_mutextLogger.unlock();
				return;
			}
98 99 100 101 102 103 104
			break;
		}
		if (m_pLogFile==0)
		{
			if (m_bUseLogFile==true)
				m_bUseLogFile = CreateNewLogFile(QCoreApplication::instance());
			if (m_pLogFile==0)
105 106
			{
				m_mutextLogger.unlock();
107
				return;
108
			}
109 110 111 112
		}

		QDateTime dtmCur = QDateTime::currentDateTime().toUTC();

113
		QString strMsg ;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
		strMsg += dtmCur.toString("yyyy-MM-dd HH:mm:ss.zzz");
		QString strMsgHeader = dtmCur.toString("\n                       ");
		strMsg += "(UTC)>";
		strMsgHeader += "      ";
		switch (type) {
		case QtDebugMsg:
			strMsg += QString("Debug   :");
			break;
		case QtWarningMsg:
			strMsg += QString("Warning :");
			break;
		case QtCriticalMsg:
			strMsg += QString("Critical:");
			break;
		case QtFatalMsg:
			strMsg += QString("Fatal   :");
			break;
		default:
			strMsg += QString("Unknown :");
			break;
		}
		strMsg.append(msg);
		strMsgHeader += QString("         From {%1:%2,%3}\n").arg(QString(context.file)).arg(context.line).arg(QString(context.function));
		strMsg.append(strMsgHeader);

		QTextStream stream(m_pLogFile);
		stream << strMsg;
		stream.flush();
		if (m_pLogFile->pos()>=m_nMaxFileSize)
			m_bUseLogFile = CreateNewLogFile(QCoreApplication::instance());
144
		m_mutextLogger.unlock();
145 146 147
	}

}