-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogger.cpp
126 lines (121 loc) · 3.93 KB
/
Logger.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QTextCodec>
#include <QDebug>
bool Logger::verbose=true;
Logger* Logger::m_instance=NULL;
Logger::Logger(){
//初始化线程
logWidget=NULL;
qMutex=new QMutex();
qNotEmpty=new QWaitCondition();
logQueue=new QQueue<Log>();
this->start();
}
Logger* Logger::instance(){
static QMutex insMutex;
if(!Logger::m_instance){
QMutexLocker locker(&insMutex);
if(!Logger::m_instance)
Logger::m_instance=new Logger();
}
return Logger::m_instance;
}
void Logger::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg){
Log temp;
temp.type=type;
temp.file=QString(context.file);
temp.line=context.line;
temp.function=QString(context.function);
temp.message=msg;
temp.time=QDateTime::currentDateTime();
temp.flushTag=false;
if(Logger::verbose){
const char* msgStr=qPrintable(msg);
switch (type) {
case QtDebugMsg:
fprintf(stdout, "[DEBUG]\t[%s]%s\n",context.function, msgStr);
break;
case QtInfoMsg:
fprintf(stderr, "[INFO]\t[%s]%s\n",context.function, msgStr);
break;
case QtWarningMsg:
fprintf(stderr, "[WARN]\t[%s]%s\n",context.function, msgStr);
break;
case QtCriticalMsg:
fprintf(stderr, "[ERROR]\t[%s]%s\n",context.function, msgStr);
break;
case QtFatalMsg:
fprintf(stderr, "[FATAL]\t[%s]%s\n",context.function, msgStr);
abort();
}
fflush(stdout);
fflush(stderr);
}
Logger::instance()->enqueue(temp);
Logger::instance()->addToLogWidget(temp);
}
void Logger::setLogWidget(QListWidget* widget){
this->logWidget=widget;
Log t;
t.flushTag=true;
addToLogWidget(t);
}
void Logger::addToLogWidget(Log log){
static QQueue<Log> widgetCache;
if(!log.flushTag)
widgetCache.enqueue(log);
if(this->logWidget==NULL) return;
while(!widgetCache.empty()){
log=widgetCache.dequeue();
QString LogContent=log.time.toString("[hh:mm:ss]")+log.message;
QListWidgetItem* item=new QListWidgetItem();
item->setText(LogContent);
switch(log.type){
case QtDebugMsg: item->setForeground(QBrush(Qt::black));break;
case QtInfoMsg: item->setForeground(QBrush(Qt::blue));break;
case QtWarningMsg: item->setForeground(QBrush(Qt::darkYellow));break;
case QtCriticalMsg: item->setForeground(QBrush(Qt::red));break;
default:break;
}
logWidget->addItem(item);
logWidget->scrollToBottom();
}
}
void Logger::enqueue(Log log){
logQueue->enqueue(log);
qNotEmpty->wakeAll();
}
void Logger::run(){
QSqlDatabase db;
QSqlQuery sqlHandle(db);
Log temp;
while(1){
qMutex->lock();
if(logQueue->length()<1)
qNotEmpty->wait(qMutex);
qMutex->unlock();
db.transaction();
//DBUtil::beginTransaction();
//sqlHandle=DBUtil::getSqlHandle();
while(!logQueue->empty()){
temp=logQueue->dequeue();
sqlHandle.prepare("insert into faceLog (timestamp,day,content,type,file,line,function) values (?,?,?,?,?,?,?)");
sqlHandle.bindValue(0,QVariant(temp.time.toTime_t()));
sqlHandle.bindValue(1,QVariant(temp.time.toString("yyyy-MM-dd")));
sqlHandle.bindValue(2,temp.message);
sqlHandle.bindValue(3,QVariant(temp.type));
sqlHandle.bindValue(4,temp.file);
sqlHandle.bindValue(5,QVariant(temp.line));
sqlHandle.bindValue(6,temp.function);
sqlHandle.exec();
}
db.commit();
//DBUtil::commitTransaction();
}
}