-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.cpp
111 lines (92 loc) · 2.36 KB
/
log.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
//
// Created by macskas on 12/9/20.
//
#include "log.h"
#include <string>
#include <cstring>
#include <ctime>
#include <cstdio>
#include <cstdarg>
#include <unistd.h>
#include <fcntl.h>
#include "common.h"
static int debug_enabled = -1;
static int log_output_enabled_stdout = -1;
static int log_output_enabled_stderr = -1;
static int debug_pid = getpid();
bool is_debug()
{
if (debug_enabled == 1)
return true;
return false;
}
void log_output_check()
{
if (fcntl(fileno(stderr), F_GETFD) == 0) {
log_output_enabled_stderr = 1;
} else {
log_output_enabled_stderr = 0;
}
if (fcntl(fileno(stdout), F_GETFD) == 0) {
log_output_enabled_stdout = 1;
} else {
log_output_enabled_stdout = 0;
}
}
void debug_enable()
{
debug_enabled = 1;
}
void debug_setpid()
{
debug_pid = getpid();
}
static
void global_sprintf(const char *type, const char *fmt, va_list argptr) {
if (log_output_enabled_stderr != 1 && log_output_enabled_stdout != 1)
return;
char buffer[512];
char timebuffer[256];
time_t now = time(nullptr);
struct tm *timeinfo = nullptr;
DMEMZERO(buffer,512);
DMEMZERO(timebuffer, 256);
timeinfo = localtime( &now );
vsnprintf(buffer, 512, fmt, argptr);
strftime(timebuffer, 256, "%Y-%m-%d %H:%M:%S",timeinfo);
if (type[0] == 'D' || type[0] == 'I') {
if (log_output_enabled_stdout) {
fprintf(stdout, "%s %-5s [%-5d] > %s\n", timebuffer, type, debug_pid, buffer);
}
} else {
if (log_output_enabled_stderr) {
fprintf(stderr, "%s %-5s [%-5d] > %s\n", timebuffer, type, debug_pid, buffer);
}
}
}
void debug_sprintf(const char *fmt, ...) {
if (debug_enabled != 1)
return;
va_list argptr;
va_start(argptr, fmt);
global_sprintf("DEBUG", fmt, argptr);
va_end(argptr);
}
void info_sprintf(const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
global_sprintf("INFO", fmt, argptr);
va_end(argptr);
}
void warning_sprintf(const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
global_sprintf("WARN", fmt, argptr);
va_end(argptr);
}
void error_sprintf(const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
global_sprintf("ERROR", fmt, argptr);
va_end(argptr);
}