-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
36 lines (27 loc) · 947 Bytes
/
logger.cpp
File metadata and controls
36 lines (27 loc) · 947 Bytes
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
#include "logger.h"
#include <QFile>
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#define LOG_LEVEL_INFO_STRING "INFO "
#define LOG_LEVEL_WARNING_STRING "WARNING"
#define LOG_LEVEL_ERROR_STRING "ERROR "
Logger::Logger()
{
}
void Logger::log(QString message, int logLevel)
{
QString time = QDateTime::currentDateTime().toString("yyyy.MM.dd HH:mm:ss");
QString level = LOG_LEVEL_INFO_STRING;
if (logLevel == LOG_LEVEL_WARNING)
level = LOG_LEVEL_WARNING_STRING;
else if (logLevel == LOG_LEVEL_ERROR)
level = LOG_LEVEL_ERROR_STRING;
QString fullMessage = QString("%1 %2 %3\n").arg(time).arg(level).arg(message);
QString logFileName = qApp->applicationName() + ".log";
QFile file(qApp->applicationDirPath() + QDir::separator() + logFileName);
if (!file.open(QFile::WriteOnly | QFile::Append))
return;
file.write(fullMessage.toUtf8());
file.close();
}