-
Notifications
You must be signed in to change notification settings - Fork 0
/
logthread.cpp
executable file
·68 lines (54 loc) · 1.98 KB
/
logthread.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
#include "logthread.h"
#include <QMetaObject>
#include <QDebug>
#include <QIODevice>
QList<logInfo> logThread::pendingLines;
QMutex logThread::mutex;
QFile logThread::mainLog;
logThread::logThread(QObject *parent) : QThread(parent)
{
}
void logThread::addLog(const QString &line, logInfo::TYPE_LOG type) {
mutex.lock();
if (mainLog.isOpen())
pendingLines.append(logInfo(QDateTime::currentDateTime(), line, type));
mutex.unlock();
}
void logThread::openLogFiles()
{
if (!QFile::exists(LOGS_FOLDER))
QDir::current().mkdir(LOGS_FOLDER);
mainLog.setFileName(MAIN_LOG);
if (!mainLog.open(QIODevice::Append | QIODevice::Unbuffered)) {
utils::mostrarError("Impossible to open log files");
return;
}
mainLog.write(QByteArray("\n*****************************************************************************\n"));
mainLog.write((QDateTime::currentDateTime().toString("[dd/MM hh:mm:ss] ") + "Openning log. VERSION: ").toLatin1());
mainLog.write(VERSION.toLatin1());
mainLog.write(QByteArray("\n*****************************************************************************"));
}
void logThread::closeLogFiles()
{
if (mainLog.isOpen()) {
mainLog.write(QByteArray("\n*****************************************************************************\n"));
mainLog.write((QDateTime::currentDateTime().toString("[dd/MM hh:mm:ss] ") + "Clossing log").toLatin1());
mainLog.write(QByteArray("\n*****************************************************************************\n"));
mainLog.close();
}
}
void logThread::run()
{
forever {
mutex.lock();
if (mainLog.isOpen()) {
foreach (const logInfo &info, pendingLines)
if (info.type == logInfo::MAIN)
mainLog.write(('\n' + info.time.toString("[dd/MM hh:mm:ss] ") + info.msg).toLatin1());
pendingLines.clear();
}
mainLog.flush();
mutex.unlock();
msleep(250);
}
}