-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
117 lines (96 loc) · 1.99 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
#include "logger.h"
#include "config.h"
#include "utils.h"
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#define WRITE_INFO_LOG(FMT) fprintf(stderr, "INFO: %s", FMT)
#define WRITE_ERROR_LOG(FMT) fprintf(stderr, "ERROR: %s", FMT)
#define WRITE_DEBUG_LOG(FMT) fprintf(stderr, "DEBUG: %s", FMT)
Logger* Logger::self = NULL;
Logger::Logger() : logfile(NULL)
{
}
Logger::~Logger()
{
}
bool Logger::Init()
{
if (!self) {
self = new Logger();
}
if (!self) {
fprintf(stderr, "Error: Initializing Logger\n");
return false;
}
return self->Open();
}
bool Logger::Open()
{
std::string log_file_name;
std::string _log_file_path = config.log_file_path;
Utils::GetCurrDateTimeMs(log_file_name);
log_file_name += ".log";
if (mkdir(_log_file_path.c_str(), 0755) == -1) {
if (errno != EEXIST) {
fprintf(stderr, "ERROR: Creating/Opening LOG Directory\n");
return false;
}
}
if (_log_file_path[_log_file_path.length()-1] != '/') {
_log_file_path += "/";
}
_log_file_path += log_file_name;
logfile = fopen(_log_file_path.c_str(), "w");
if (!logfile) {
fprintf(stderr, "Error: Opening Log File\n");
return false;
}
return true;
}
void Logger::LogWrite(LogMode mode, const char *fmt, ...)
{
if (!self) {
fprintf(stderr, "Error: Logger Not Inited\n");
return;
}
va_list vl1, vl2;
va_start(vl1, fmt);
va_copy(vl2, vl1);
const int num = vsnprintf(0, 0, fmt, vl1);
char temp[num+1];
memset(temp, 0, sizeof(temp));
vsnprintf(temp, (num+1), fmt, vl2);
switch(mode) {
case LOG_INFO:
{
WRITE_INFO_LOG(temp);
break;
}
case LOG_ERROR:
{
WRITE_ERROR_LOG(temp);
break;
}
case LOG_DEBUG:
{
WRITE_DEBUG_LOG(temp);
break;
}
default:
{
WRITE_DEBUG_LOG(temp);
break;
}
}
va_end(vl1);
va_end(vl2);
}
void Logger::Destroy()
{
if (self) {
delete self;
}
}