-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlogger.cpp
More file actions
62 lines (51 loc) · 1.45 KB
/
Copy pathlogger.cpp
File metadata and controls
62 lines (51 loc) · 1.45 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
#include <QCoreApplication>
#include <QDateTime>
#include <QFileInfo>
#include <QMutexLocker>
#include <iostream>
#include "logger.h"
static bool enabled, timestamps;
static QFile file;
static QMutex mutex;
static QString service;
static QString typeString(QtMsgType type)
{
switch (type)
{
case QtDebugMsg: return "dbg";
case QtWarningMsg: return "wrn";
default: return "inf";
}
}
static QString formatMessage(QString message)
{
message.front() = message.front().toUpper();
return message;
}
void setLogEnabled(bool value)
{
enabled = value;
}
void setLogTimestams(bool value)
{
timestamps = value;
}
void setLogFile(const QString &value)
{
file.setFileName(value);
}
void logger(QtMsgType type, const QMessageLogContext &, const QString &message)
{
QMutexLocker lock(&mutex);
QString timestamp = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"), data;
if (service.isEmpty())
service = QCoreApplication::applicationName().split('-').last().append(':').leftJustified(11);
data = QString("(%1) %2 %3").arg(typeString(type), service, formatMessage(message));
if (enabled && file.open(QIODevice::WriteOnly | QIODevice::Append))
{
QTextStream stream(&file);
stream << QString("%1 %2").arg(timestamp, data) << Qt::endl;
file.close();
}
std::cout << (timestamps ? QString("%1 %2").arg(timestamp, data) : data).toStdString() << std::endl;
}