-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathdebug_log.cpp
More file actions
67 lines (58 loc) · 1.26 KB
/
debug_log.cpp
File metadata and controls
67 lines (58 loc) · 1.26 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
#include "debug_log.h"
void DEBUG_LOG(const char *msg, ...) {
char message[256] = {0};
va_list args;
va_start(args, msg);
vsprintf(message, msg, args);
va_end(args);
Debug_LOG::log()->write_log(message);
}
Debug_LOG *Debug_LOG::m_log = NULL;
Debug_LOG::Debug_LOG():
tim(0),
t(NULL),
fp(NULL),
filepath(),
message(),
last_log_time()
{
#ifdef __WRITE_FILE__
create_log_file();
#endif
}
Debug_LOG::~Debug_LOG(){
#ifdef __WRITE_FILE__
fclose(fp);
#endif
}
void Debug_LOG::create_log_file(){
if(fp != NULL)
fclose(fp);
sprintf(filepath, "./log/debuglog_");
time(&tim);
t = localtime(&tim);
memcpy(&last_log_time, t, sizeof(struct tm));
sprintf(filepath + 15, "%02d_%02d",t->tm_mon + 1, t->tm_mday);
fp = fopen(filepath, "a+");
}
Debug_LOG *Debug_LOG::log(){
if(m_log == NULL){
m_log = new Debug_LOG();
}
return m_log;
}
void Debug_LOG::write_log(const char *msg){
time(&tim);
t = localtime(&tim);
sprintf(message, "[%02d:%02d:%02d] %s\n", t->tm_hour, t->tm_min, t->tm_sec, msg);
#ifdef __WRITE_FILE__
if(t->tm_mday != last_log_time.tm_mday || t->tm_mon != last_log_time.tm_mon
|| t->tm_year != last_log_time.tm_year)
create_log_file();
fwrite(message, strlen(message), 1, fp);
fflush(fp);
#else
printf("\n%s", message);
fflush(stdout);
#endif
}