-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
103 lines (94 loc) · 1.74 KB
/
Copy pathlogger.cpp
File metadata and controls
103 lines (94 loc) · 1.74 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
#include <QCoreApplication>
#include <ctime>
#include "logger.hpp"
#include "main.hpp"
unsigned char Logger::LogLevel=LOG_DEBUG;
bool Logger::LogFileEnabled=true;
bool Logger::pIsBusy=false;
Logger *gAppLogger;
const char mgstypestring[8][16]=
{
"EMERGENCY",
"ALERT",
"CRITICAL",
"ERROR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG"
};
Logger::Logger()
{
pLogFilePath=string(PROGRAM_NAME ".log");
pLogFile=nullptr;
}
Logger::~Logger()
{
if(pLogFile)
{
fclose(pLogFile);
}
}
void Logger::SetLogFilePath(const char *newPath)
{
string npath(newPath);
if(npath.length())
{
SetLogFilePath(npath);
}
}
void Logger::SetLogFilePath(string newPath)
{
while(pIsBusy)
{
QCoreApplication::processEvents();
}
pIsBusy=true;
if(newPath.size())
{
pLogFilePath=newPath;
}
pIsBusy=false;
}
void Logger::Log(const char *message, unsigned char msg_level)
{
if(msg_level>LogLevel)
{
return;
}
if(strlen(message))
{
Log(string(message), msg_level);
}
}
void Logger::Log(string message, unsigned char msg_level)
{
if(msg_level>LogLevel)
{
return;
}
while(pIsBusy)
{
QCoreApplication::processEvents();
}
pIsBusy=true;
time(&pRawTime);
pTimeInfo=localtime(&pRawTime);
sprintf(pLogDTstr, "%.2i.%.2i.%i | %.2i:%.2i:%.2i", pTimeInfo->tm_mday, 1+pTimeInfo->tm_mon, 1900+pTimeInfo->tm_year, pTimeInfo->tm_hour, pTimeInfo->tm_min, pTimeInfo->tm_sec);
if(message.size())
{
fprintf(stdout, "%s [%s] %s\n", pLogDTstr, mgstypestring[msg_level], message.data());
fflush(stdout);
if(LogFileEnabled)
{
pLogFile=fopen(pLogFilePath.data(), "a");
if(pLogFile)
{
fprintf(pLogFile, "%s [%s] %s\n", pLogDTstr, mgstypestring[msg_level], message.data());
fflush(pLogFile);
fclose(pLogFile);
}
}
}
pIsBusy=false;
}