forked from omniedgeio/omniedge-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.cpp
107 lines (94 loc) · 2.3 KB
/
syslog.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "syslog.h"
#include <stdio.h>
#include <stdlib.h>
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>
#include <QTime>
#include <QDir>
#define TIME qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
static FILE *q_fileStream = nullptr;
static int g_logLevel = 0;
enum TraceLevel
{
TL_NULL = 0,
TL_CRIT,
TL_ERRO,
TL_WARN,
TL_INFO
};
static const int g_TraceLevel [] = { TL_INFO, TL_WARN, TL_ERRO, TL_CRIT, TL_NULL };
syslog::syslog(QObject *parent) : QObject(parent)
{
}
syslog::~syslog()
{
}
void syslog::SoftWareShutDown()
{
if (nullptr != q_fileStream)
{
qDebug("Close log file.");
fclose (q_fileStream);
q_fileStream = nullptr;
}
}
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(type);
Q_UNUSED(context);
QByteArray localMsg = msg.toLocal8Bit();
if (g_logLevel && q_fileStream)
{
int iLevel = g_TraceLevel[type];
if (g_logLevel < iLevel)
{
return;
}
fprintf(q_fileStream, "%s %s \n", TIME, localMsg.constData());
fflush(q_fileStream);
}
}
void installMsgHandler(QString strLogFile)
{
QByteArray byteArrayLogFile = strLogFile.toLatin1();
if (nullptr != q_fileStream)
{
fclose (q_fileStream);
q_fileStream = nullptr;
}
q_fileStream = fopen (byteArrayLogFile.constData(), "a+");
if (nullptr != q_fileStream)
{
qDebug("Opened log file.");
qInstallMessageHandler(myMessageOutput);
}
else
{
qDebug("Failed to open log file.");
}
}
bool syslog::isFileExist(QString fullFileName)
{
QFileInfo fileInfo(fullFileName);
if(fileInfo.isFile())
{
return true;
}
return false;
}
void syslog::installReleaseMsgHandler()
{
QString strAppPath = QCoreApplication::applicationDirPath();
QString strIniFile = strAppPath + "/RunInfo.ini";
QString strLogFile;
strLogFile =QString("%1/Omniedge.log").arg(strAppPath);
if(isFileExist(strLogFile))
{
QFile file(strLogFile);
file.remove();
}
QSettings pSettings(strIniFile, QSettings::IniFormat);
g_logLevel = pSettings.value("TraceSet/TraceLevel", 4).toInt();
installMsgHandler(strLogFile);
}