-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
111 lines (100 loc) · 3.94 KB
/
main.cpp
File metadata and controls
111 lines (100 loc) · 3.94 KB
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
#include "mainwindow.h"
#include <QApplication>
#include <QTranslator>
#include <QMessageBox>
#include <QDir>
#include <QDebug>
#include <QTextCodec>
#include <QLocale>
#include <QTime>
#include "settings.h"
bool g_verboseOutput = false;
static QTextCodec *logCodec = nullptr;
static FILE *logStream = nullptr;
QString g_logFilePath = "";
/** @brief For convenient parsing log files, messages have to be formatted as:
* level: message (`placeInSource`)
* where:
* level - Debug, Warning, Critical, Fatal
* message - log message
* placeInSource - point, where message was emited in format: (`filename:line, function_signature`)
*/
void logging(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = logCodec->fromUnicode(msg);
QString fileName(context.file);
fileName.remove(0, fileName.lastIndexOf("\\") + 1);
fileName.remove(0, fileName.lastIndexOf("/") + 1);
QByteArray file = logCodec->fromUnicode(fileName);
QTime time = QTime::currentTime();
QString formatedTime = time.toString("hh:mm:ss.zzz");
fprintf(logStream, "%s ", qPrintable(formatedTime));
switch (type) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
case QtInfoMsg:
fprintf(logStream, "Info: %s (`%s:%u, %s`)\n", localMsg.constData(), file.constData(), context.line, context.function);
break;
#endif
case QtDebugMsg:
fprintf(logStream, "Debug: %s (`%s:%u, %s`)\n", localMsg.constData(), file.constData(), context.line, context.function);
break;
case QtWarningMsg:
fprintf(logStream, "Warning: %s (`%s:%u, %s`)\n", localMsg.constData(), file.constData(), context.line, context.function);
break;
case QtCriticalMsg:
fprintf(logStream, "Critical: %s (`%s:%u, %s`)\n", localMsg.constData(), file.constData(), context.line, context.function);
break;
case QtFatalMsg:
fprintf(logStream, "Fatal: %s (`%s:%u, %s`)\n", localMsg.constData(), file.constData(), context.line, context.function);
abort();
break;
}
fflush(logStream);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Configure and redirect log output to stderr or in text file
QByteArray envVar = qgetenv("RUN_IN_QTCREATOR"); // this variable is only set when run application in QtCreator
if (envVar.isEmpty()) {
g_logFilePath = QString("%1/log_asa.txt").arg(QDir::currentPath());
#ifdef Q_OS_WIN
errno_t error = _wfopen_s(&logStream, g_logFilePath.toStdWString().c_str(), L"w");
if (error != 0) {
logStream = stderr;
qCritical() << "couldn't open log file" << g_logFilePath << "error:" << error << errno;
}
#else
// for Linux - log file will be put in a User's home directory
logStream = fopen(g_logFilePath.toUtf8().data(), "w");
#endif
} else {
logStream = stderr;
}
logCodec = QTextCodec::codecForName("Windows-1251");
qInstallMessageHandler(logging);
qDebug() << "Start application. Write log to" << g_logFilePath;
QTranslator translator;
Settings *settings = Settings::getSettings();
QString locale = settings->value("Global/Locale", "en_US").toString();
QString filename = QString(":/languages%1lang_%2").arg(QDir::separator()).arg(locale);
if (translator.load(filename) ){
QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8"));
if (!QCoreApplication::installTranslator(&translator)) {
qWarning() << "Translator couldn't be installed";
Q_ASSERT(false);
}
} else {
qWarning() << "Translation file not loaded:" << filename;
}
QStringList appArguments = QApplication::arguments();
if (appArguments.contains("-v")) {
qDebug() << "Enable verbose output";
g_verboseOutput = true;
} else {
qDebug() << "Verbose output is disabled";
}
MainWindow w;
w.show();
return a.exec();
}