-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.cpp
154 lines (143 loc) · 3.96 KB
/
logger.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
#include "logger.h"
#include <QDebug>
Logger::Logger(QObject *parent) :
QObject(parent)
{
m_Dir = new QDir();
m_Dir->setPath(QDir::currentPath());
arrayOfES[0] = "Notify";
arrayOfES[1] = "Minor";
arrayOfES[2] = "Warning";
arrayOfES[3] = "Critical";
arrayOfES[4] = "Fatal";
}
Logger::~Logger()
{
QFile *temp;
QMap<QString, QFile *>::iterator it = m_ListOfLogs.begin();
for (; it != m_ListOfLogs.end(); ++it)
{
temp = it.value();
temp->close();
delete it.value();
}
delete m_Dir;
}
void Logger::AddToChannelLog(QString &ChannelName, QString &Message)
{
if(m_currentDay != QDate::currentDate().day())
changeFolder();
QString outMsg;
outMsg = "[" + QTime::currentTime().toString("hh:mm:ss") + "]";
if(!m_ListOfLogs.contains(ChannelName))
{
QFile *file = new QFile(ChannelName + ".txt");
if(file->exists())
{
if(!file->open(QIODevice::Append))
{
QString msg = "Unable open log file: " + file->fileName();
emit logMessage(msg);
return;
}
}
else
{
if(!file->open(QIODevice::WriteOnly))
{
QString msg = "Unable open log file: " + file->fileName();
emit logMessage(msg);
return;
}
QStringList List(file->fileName());
emit addToListOfLogs(List);
}
m_ListOfLogs.insert(ChannelName, file);
}
QTextStream out(m_ListOfLogs.value(ChannelName));
outMsg += Message;
if(QString(ChannelName + ".txt") == m_currentLog)
emit logMessage(outMsg);
outMsg += "\n";
out << outMsg;
}
void Logger::AddToServerLog(ErrorStatus Status, QString &Message)
{
if(m_currentDay != QDate::currentDate().day())
changeFolder();
QString outMsg;
outMsg = "[" + QTime::currentTime().toString("hh:mm:ss") + "]";
if(!m_ListOfLogs.contains("server"))
{
QFile *file = new QFile("server.txt");
if(file->exists())
{
if(!file->open(QIODevice::Append))
{
QString msg = "Unable open server log file";
emit logMessage(msg);
return;
}
}
else
{
if(!file->open(QIODevice::WriteOnly))
{
QString msg = "Unable open server log file";
emit logMessage(msg);
return;
}
QStringList List(file->fileName());
emit addToListOfLogs(List);
}
m_ListOfLogs.insert("server", file);
}
QTextStream out(m_ListOfLogs.value("server"));
outMsg = outMsg + "[" + arrayOfES[Status] + "]" + Message;
if(QString("server.txt") == m_currentLog)
emit logMessage(outMsg);
qDebug() << outMsg;
outMsg += "\n";
out << outMsg;
}
void Logger::currentLog(QString str)
{
m_currentLog = str;
}
void Logger::SetSettings(LoggerConfig *p_LoggerConfig)
{
m_Path = p_LoggerConfig->Path;
}
void Logger::StartLogger()
{
QString currentDate;
currentDate = QDate::currentDate().toString("dd.MM.yyyy");
m_currentDay = QDate::currentDate().day();
if(!m_Dir->cd(m_Path)) //create log directory
{
m_Dir->mkdir(m_Path);
m_Dir->cd(m_Path);
}
if(!m_Dir->cd(currentDate)) //create directory for specific date
{
m_Dir->mkdir(currentDate);
m_Dir->cd(currentDate);
}
QDir::setCurrent(m_Dir->absolutePath());
QStringList List;
m_Dir->makeAbsolute();
List = m_Dir->entryList(QDir::Files, QDir::Name);
emit addToListOfLogs(List);
}
void Logger::changeFolder()
{
m_ListOfLogs.clear();
m_Dir->makeAbsolute();
m_Dir->cdUp();
QString currentDate;
currentDate = QDate::currentDate().toString("dd.MM.yyyy");
m_Dir->mkdir(currentDate);
m_Dir->cd(currentDate);
m_currentDay++;
QDir::setCurrent(m_Dir->absolutePath());
}