-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogger.cpp
186 lines (158 loc) · 5.94 KB
/
blogger.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <QtCore>
#include "blogger.h"
#include "json.h"
#include "cdefines.h"
static QString dumpFileName="";
QFile file;
/*
*Setup the logger. It must be called before any debug, assert or critical message.
*It's called from Studio MMobile constructor, EngineRuntime constructor
*/
void BLogger::setup(const QString &dumpFile){
openDumpFile(dumpFile);
}
/*
*Debug the message to the console when compiled in debug mode
*If dumpFile is set, also dumps the message to the file.
*/
void BLogger::debug(QObject *o, const QString &methodName, const QString &msg) {
log(LogTypeDebug,o,methodName,msg);
}
/*
*
*/
void BLogger::warning(QObject *o, const QString &methodName, const QString &msg, bool displayDialog){
log(LogTypeWarning,o,methodName,msg,QString(),0,displayDialog);
}
/*
*If displayDialog is set, shows the error to the user.
*If dumpFile is set, also dumps the message to the file.
*/
void BLogger::error(QObject *o, const QString &methodName, const QString &msg, bool displayDialog) {
log(LogTypeError,o,methodName,msg,QString(),0,displayDialog);
}
/*
*If displayDialog is set, shows the error to the user.
*If dumpFile is set, also dumps the message to the file.
*Also exists the application
*/
void BLogger::criticalError(QObject *o, const QString &methodName, const QString &internalMsg, qint32 lineNumber, bool displayDialog){
log(LogTypeCritical,o,methodName,INTERNALERRORMSG,internalMsg,lineNumber,displayDialog);
}
/*
*If displayDialog is set, shows the error to the user.
*If dumpFile is set, also dumps the message to the file.
*Also exists the application
*/
void BLogger::criticalError(QObject *o, const QString &methodName, qint32 lineNumber, bool displayDialog){
log(LogTypeCritical,o,methodName,INTERNALERRORMSG,"",lineNumber,displayDialog);
}
/*
*If displayDialog is set, shows the error to the user.
*If dumpFile is set, also dumps the message to the file.
*Also exists the application
*/
void BLogger::criticalError(QObject *o, const QString &methodName, const QString &userMsg, const QString &internalMsg, qint32 lineNumber, bool displayDialog){
log(LogTypeCritical,o,methodName,userMsg,internalMsg,lineNumber,displayDialog);
}
/*
*Checks the test condition.
*If it fails, procced as a critical error.
*/
void BLogger::assertError(bool test, QObject *o, const QString &methodName, const QString &userMsg, const QString &internalMsg, qint32 lineNumber, bool displayDialog){
if(test)return;
log(LogTypeCritical,o,methodName,QString("Assert failure: ")+userMsg,internalMsg,lineNumber,displayDialog);
}
/*
*Checks the test condition.
*If it fails, procced as a critical error.
*/
void BLogger::assertError(bool test, QObject *o, const QString &methodName, qint32 lineNumber, bool displayDialog){
if(test)return;
log(LogTypeCritical,o,methodName,QString("Assert failure"),"",lineNumber,displayDialog);
}
void BLogger::doubleAssert(bool test1, bool test2, QObject *o, const QString &methodName, qint32 lineNumber){
if(!test1)log(LogTypeCritical,o,methodName,QString("Assert failure"),"test1",lineNumber);
if(!test2)log(LogTypeCritical,o,methodName,QString("Assert failure"),"test2",lineNumber);
}
/*
*This function should be used internally only.
*/
void BLogger::log(LogType logType, QObject *o, const QString &methodName, const QString &msg, const QString &internalMessage, qint32 lineNumber, bool displayDialog) {
QString className;
QString completeMethodName;
if(o){
className=QString(o->metaObject()->className());
completeMethodName="("+className+")"+methodName+".";
}else{
if(!methodName.isEmpty())completeMethodName=methodName+".";
}
QString completeMsg=completeMethodName+msg;
if(!internalMessage.isEmpty())completeMsg.append(".InternalMsg:"+ internalMessage);
if(lineNumber>0)completeMsg.append(".EditorLine:"+QString::number(lineNumber));
switch(logType){
case LogTypeWarning:
qWarning()<<"Warning:"<<completeMsg;
if(!dumpFileName.isEmpty())dumpToFile(completeMsg);
//if(displayDialog)QMessageBox::warning(0,"Warning",msg);
break;
case LogTypeDebug:{//debug nao grava no arquivo
qDebug()<<completeMsg;
//if(displayDialog)QMessageBox::information(0,"Information",msg);
break;
}
case LogTypeError:{
qCritical()<<"Error:"<<completeMsg;
if(!dumpFileName.isEmpty())dumpToFile(completeMsg);
//if(displayDialog)QMessageBox::critical(0,"Error",msg);
break;
}
case LogTypeCritical:{
//if(displayDialog)QMessageBox::critical(0,"Critical Error",msg+"\nApplication will exit.");
qFatal(completeMsg.toUtf8().constData());//exits the app
if(!dumpFileName.isEmpty())dumpToFile(completeMsg);
/*if(sendCriticalErrorsToServer)sendErrorToWebService(className,methodName,msg,internalMessage,lineNumber);
qWarning()<<"Critical:"<<completeMsg;
#ifdef Q_OS_WIN
enum { ExitCode = 0 };
::TerminateProcess(::GetCurrentProcess(), ExitCode);
#else
qint64 pid = QCoreApplication::applicationPid();
QProcess::startDetached("kill -9 " + QString::number(pid));
#endif*/
break;
}
}
}
/*
*Closes the dump file
*/
void BLogger::closeDumpFile(){
dumpFileName="";
if(file.isOpen())file.close();
}
/*
*Uses the 'dumpFileName' as dump file
*/
void BLogger::openDumpFile(const QString &dumpFile){
if(dumpFile.trimmed().isEmpty()) return;
dumpFileName=dumpFile;
QFileInfo fileInfo(dumpFile);
if(fileInfo.exists() && (fileInfo.size()>(DUMPFILESIZEMB*1000000))){
if(!QFile::remove(dumpFile))qWarning()<<"Unable to remove dump file "+dumpFile;
}
file.setFileName(dumpFile);
if(file.isOpen())return;
if(!file.open(QIODevice::Text | QIODevice::Append))qWarning()<<"Unable to open dump file "+dumpFile;
}
/*
*Appends completeMessage to the end of the dumpfile
*If dumpFile is not set, it does nothing.
*/
void BLogger::dumpToFile(const QString &completeMessage){
QString msg=QDateTime::currentDateTime().toString(Qt::ISODate)+": "+completeMessage+"\n";
if(file.isOpen()){
file.write(msg.toUtf8().constData());
file.flush();//do not remove this line
}
}