提交 5c7c2437 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

Simply add a logger class, with the help of which , develpers can re-direct...

Simply add a logger class, with the help of which , develpers can re-direct qDebug(), qWarning(), qCritical() and QFatal(...) outputs to log files , instead of stderr.
上级 45bd4540
......@@ -29,7 +29,8 @@ SOURCES += main.cpp\
cluster/zp_clusterterm.cpp \
dialogaddressinput.cpp \
cluster/zp_clusternode.cpp \
smartlink/st_cross_svr_node.cpp
smartlink/st_cross_svr_node.cpp \
logger/st_logger.cpp
HEADERS += zpmainframe.h \
network/zp_tcpserver.h \
......@@ -50,7 +51,8 @@ HEADERS += zpmainframe.h \
dialogaddressinput.h \
cluster/zp_clusternode.h \
smartlink/st_cross_svr_msg.h \
smartlink/st_cross_svr_node.h
smartlink/st_cross_svr_node.h \
logger/st_logger.h
FORMS += zpmainframe.ui \
dialogaddressinput.ui
......
#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)
{
switch (type) {
case QtDebugMsg:
if (m_nLogLevel < 3) return;
break;
case QtWarningMsg:
if (m_nLogLevel < 2) return;
break;
case QtCriticalMsg:
if (m_nLogLevel < 1) return;
break;
case QtFatalMsg:
if (m_nLogLevel < 0) return;
break;
default:
if (m_nLogLevel < 3) return;
break;
}
if (m_pLogFile==0)
{
if (m_bUseLogFile==true)
m_bUseLogFile = CreateNewLogFile(QCoreApplication::instance());
if (m_pLogFile==0)
return;
}
QDateTime dtmCur = QDateTime::currentDateTime().toUTC();
QString strMsg = "\n";
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());
}
}
#ifndef ST_LOGGER_H
#define ST_LOGGER_H
#include <QObject>
#include <QDateTime>
#include <QDir>
#include <QByteArray>
#include <QCoreApplication>
namespace STMsgLogger{
class st_logger : public QObject
{
Q_OBJECT
public:
explicit st_logger(QObject *parent = 0);
void MessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
int maxFileSize() const;
int logLevel() const;
protected:
bool CreateNewLogFile(QCoreApplication * app);
QFile * m_pLogFile;
bool m_bUseLogFile;
QString m_currLogFileName;
int m_nLogLevel;
int m_nMaxFileSize;
signals:
public slots:
void setMaxFileSize(int nSize);
void setLogLevel(int);
};
}
#endif // ST_LOGGER_H
......@@ -2,9 +2,20 @@
#include <QApplication>
#include <QTranslator>
#include <QLibraryInfo>
#include "logger/st_logger.h"
STMsgLogger::st_logger g_logger;
void stMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
g_logger.MessageOutput(type,context,msg);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//Install message handler
qInstallMessageHandler(stMessageOutput);
QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(),
......@@ -20,7 +31,7 @@ int main(int argc, char *argv[])
app.installTranslator(&appTranslator);
ZPMainFrame w;
w.setLogger(&g_logger);
w.show();
//!the main program arg formats:
......
......@@ -25,6 +25,7 @@ extern quint64 g_secSent;
ZPMainFrame::ZPMainFrame(QWidget *parent)
:QMainWindow(parent)
,ui(new Ui::ZPMainFrame)
,m_pLogger(0)
{
m_currentConfigFile = QCoreApplication::applicationFilePath()+".ini";
ui->setupUi(this);
......@@ -110,6 +111,10 @@ void ZPMainFrame::changeEvent(QEvent *e)
break;
}
}
void ZPMainFrame::setLogger(STMsgLogger::st_logger * plogger)
{
this->m_pLogger = plogger;
}
void ZPMainFrame::initUI()
{
......
......@@ -11,6 +11,7 @@
#include "smartlink/st_client_table.h"
#include "database/databaseresource.h"
#include "cluster/zp_clusterterm.h"
#include "logger/st_logger.h"
namespace Ui {
class ZPMainFrame;
}
......@@ -25,6 +26,7 @@ public:
void timerEvent(QTimerEvent *);
//Auto start support
void LoadSettingsAndForkServer(const QString & configfile);
void setLogger(STMsgLogger::st_logger * plogger);
protected:
void changeEvent(QEvent *e);
QStandardItemModel * m_pMsgModelNetwork;
......@@ -45,6 +47,8 @@ protected:
QStandardItemModel * m_pModelCluster;
//Logger
STMsgLogger::st_logger * m_pLogger;
private:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册