-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlog.cpp
181 lines (161 loc) · 6.36 KB
/
log.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
#include <log.h>
#include <QTextStream>
#include <QFile>
#include <QDir>
#include <QTime>
#include <QDialog>
#include <QMessageBox>
#include <QApplication>
#include <iostream>
#include <global.h>
//#include <windows.h>
//#include <io.h>
//#include <fcntl.h>
QMessageBox::StandardButtons log::lastPressedButton;
//BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lParam)
//{
// if(GetWindowThreadProcessId(hwnd, NULL) == GetCurrentThreadId())
// {
// *(HWND*)lParam = hwnd;
// return FALSE;
// }
// return TRUE;
//}
void log::setEnabledCon(bool c, QWidget *parent) {
if(c)
{
if(parent != 0)
{
QRect newGeometry = con->geometry();
int newX = parent->width() + parent->geometry().x();
int screenWidth =
QGuiApplication::primaryScreen()->geometry().width();
if(newX + 600 > screenWidth)
{
newX = screenWidth - 600;
}
newGeometry.setX(newX);
newGeometry.setY(parent->y() + 30);
newGeometry.setWidth(600);
newGeometry.setHeight(400);
con->setGeometry(newGeometry);
}
con->show();
}
else con->hide();
}
console *log::init() {
con = new console();
con->setWindowTitle(QString("YourDroid | %1").arg(QObject::tr("Debug console")));
return con;
}
void log::consoleSetParent(QWidget *parent)
{
con->setParent(parent);
con->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
}
void log::message(QtMsgType level, const QMessageLogContext &context, const QString &message) {
bool window = false;
QString mess = message;
if(mess[0] == '^') {
window = true;
mess.remove(0, 1);
}
QMessageBox::StandardButtons buttons;
if(mess[mess.length() - 1] == '|') {
mess.chop(1);
int pos = mess.lastIndexOf('|');
QString buttonsString = mess.mid(pos + 1, (mess.length() - pos - 1));
mess.chop(mess.length() - pos);
if(buttonsString.contains('+')) buttons = QMessageBox::Ok;
if(buttonsString.contains('-')) buttons = buttons | QMessageBox::Cancel;
if(buttonsString.contains('y')) buttons = QMessageBox::Yes;
if(buttonsString.contains('n')) buttons = buttons | QMessageBox::No;
}
else buttons = QMessageBox::Ok;
static QString logDir;
static bool first = true;
if(first)
{
if(QFile(QCoreApplication::applicationDirPath() + "/run_as_appimage").exists())
{
system("echo \"Running as an appimage\"");
logDir = "./log";
}
else
{
system("echo \"Not running as an appimage\"");
logDir = globalGetWorkDir() + "/log";
}
//qDebug().noquote() << "Log directory path: " << logDir;
}
if(!QDir(logDir).exists()) QDir().mkdir(logDir);
static QString logName = logDir + QString("/log-") + QDate::currentDate().toString("dMyy") + QTime::currentTime().toString("hhmmss") + ".txt";
static QFile preLog(logName);
if(first)
{
if(preLog.exists()) preLog.setFileName(logName.chopped(4) + "_.txt");
first = false;
QString str = QString("DEBUG: Log file path: " + QFileInfo(preLog.fileName()).absoluteFilePath());
fputs(str.toStdString().c_str(), stderr);
system(QString("echo \"%1\"").arg(str).toStdString().c_str());
}
static QTextStream logFile(&preLog);
if(!preLog.isOpen()) {
preLog.open(QIODevice::WriteOnly);
logFile << QString("# Created by YourDroid %1\n# Aplication dir: %2\n# Work dir: %3\n\n")
.arg(QString(VER_PRODUCTVERSION_STR), qApp->applicationDirPath(),
QDir("./").absolutePath());
std::freopen(logName.toStdString().c_str(), "a+", stderr);
}
//static ofstream logFile("log.txt");
QString typeName;
switch(level) {
case QtInfoMsg: typeName = "INFO:"; break;
case QtDebugMsg: typeName = "DEBUG:"; break;
case QtWarningMsg: typeName = "WARNING:"; break;
case QtCriticalMsg: typeName = "ERROR:"; break;
case QtFatalMsg: typeName = "FATAL ERROR:"; break;
}
#if LINUX
QString color;
switch(level) {
case QtWarningMsg: color = "\x1b[33m"; break;
case QtCriticalMsg: color = "\x1b[31m"; break;
case QtFatalMsg: color = "\x1b[31m"; break;
default: color = "\x1b[0m"; break;
}
QString messFull = color + typeName + QString(' ') + mess + "\x1b[0m";
#elif WIN
QString messFull = typeName + QString(' ') + mess;
#endif
logFile << "TIME:" << " " << QTime::currentTime().toString("hh:mm:ss") << " FILE: " << context.file << " LINE: " << QString::number(context.line) << " " << typeName << " " << mess << "\n\r";
std::cout << (messFull.toStdString() + '\n') << std::flush;
logFile.flush();
Qt::GlobalColor _color;
switch(level) {
case QtWarningMsg: _color = Qt::yellow; break;
case QtCriticalMsg: _color = Qt::red; break;
case QtFatalMsg: _color = Qt::red; break;
default: _color = Qt::white; break;
}
emit con->consoleWriteMess(typeName + QString(' ') + mess, _color);
//if(mess.isEmpty()) return;
if(window) {
QApplication::alert(con, 0);
switch(level) {
case QtWarningMsg: lastPressedButton = QMessageBox::warning(0, QObject::tr("Warning!"),
mess,
buttons); break;
case QtCriticalMsg: lastPressedButton = QMessageBox::critical(0, QObject::tr("Error!"),
mess,
buttons); break;
case QtFatalMsg: lastPressedButton = QMessageBox::critical(0, QObject::tr("Fatal error!"),
mess,
buttons); qApp->exit(-1);
default: lastPressedButton = QMessageBox::information(0, QObject::tr("Information"),
mess,
buttons); break;
}
}
}