-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
executable file
·203 lines (181 loc) · 4.11 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Gowtham Kudupudi 20/08/2016
* MIT License
*/
#include "logger.h"
#include <cstdarg>
#include <stdio.h>
#if defined(unix) || defined(__unix__) || defined(__unix)
#include <unistd.h>
#elif defined(_WIN64) || defined(_WIN32)
#include <io.h>
#endif
#include <sys/types.h>
#ifdef __CYGWIN__
#include <pthread.h>
#elif __APPLE__
#include <thread>
#elif _WIN64 || _WIN32
#include <Windows.h>
#else
#include <sys/syscall.h>
#endif
#include <stdlib.h>
#include <ferrybase/FerryTimeStamp.h>
/**
* Log level count
*/
#define FFLT_COUNT 10
static const char * const log_type_names[] = {
"ERR",
"WRN",
"NTC",
"DBG",
"INF",
"PSR",
"HDR",
"EXT",
"CLN",
"LNC",
};
/*int _ff_log(_ff_log_type t, const char* format, ...) {
char buf[300];
va_list ap;
if (!(ff_log_type & t))
return 0;
va_start(ap, format);
vsnprintf(buf, sizeof (buf), format, ap);
buf[sizeof (buf) - 1] = '\0';
va_end(ap);
char buf2[300];
unsigned long long now;
int n;
buf2[0] = '\0';
for (n = 0; n < FFLT_COUNT; n++)
if (t == (1 << n)) {
//now = time_in_microseconds() / 100;
sprintf(buf2, "[%s %s]: ", (const char*) getuTime().c_str(), log_type_names[n]);
break;
}
fprintf(stderr, "%s%s\n", buf2, buf);
fflush(stderr);
return 0;
};*/
int _ff_log(const char* func, FF_LOG_TYPE t,
unsigned int l, const char* format, ...) {
char buf[1000];
unsigned int nnl = 1 << 31;
bool no_new_line = (l & nnl) == nnl;
l &= ~nnl;
va_list ap;
if (!(fflAllowedType & t) || !(fflAllowedLevel & l))
return 0;
va_start(ap, format);
vsnprintf(buf, sizeof (buf), format, ap);
buf[sizeof (buf) - 1] = '\0';
va_end(ap);
char buf2[300];
int n;
buf2[0] = '\0';
FerryTimeStamp fTS;
fTS.Update();
for (n = 0; n < FFLT_COUNT; n++)
if (t == (1 << n)) {
//now = time_in_microseconds() / 100;
sprintf(buf2, "[%s %s %s]: ", (const char*) fTS.GetTime().c_str(),
log_type_names[n], func);
break;
}
if (no_new_line) {
fprintf(stderr, "%s%s", buf2, buf);
} else {
fprintf(stderr, "%s%s\n", buf2, buf);
}
fflush(stderr);
return 0;
};
int _ff_log (FF_LOG_TYPE t, unsigned int l, const char* func,
const char* file_name, int line_no, const char* format, ...
) {
char buf[1000];
unsigned int nnl = 1 << 31;
bool no_new_line = (l & nnl) == nnl;
l &= ~nnl;
va_list ap;
if (!(fflAllowedType & t) || !(fflAllowedLevel & l))
return 0;
va_start(ap, format);
vsnprintf(buf, sizeof (buf), format, ap);
buf[sizeof (buf) - 1] = '\0';
va_end(ap);
char buf2[300];
int n;
buf2[0] = '\0';
FerryTimeStamp fTS;
fTS.Update();
for (n = 0; n < FFLT_COUNT; n++)
if (t == (1 << n)) {
//now = time_in_microseconds() / 100;
sprintf(buf2, "[%s %s %05ld %s:%s:%d]: ",
(const char*) fTS.GetUTime().c_str(), log_type_names[n],
#ifdef __CYGWIN__
pthread_self(),
#elif __APPLE__
std::hash<std::thread::id>()(std::this_thread::get_id()),
#elif _WIN64 || _WIN32
GetCurrentThreadId(),
#else
syscall(SYS_gettid),
#endif
func, file_name, line_no
);
break;
}
if (no_new_line) {
fprintf(stderr, "%s%s", buf2, buf);
} else {
fprintf(stderr, "%s%s\n", buf2, buf);
}
fflush(stderr);
return 0;
};
int _ff_log_contnu(FF_LOG_TYPE t,
unsigned int l,
const char* format,
...) {
char buf[1000];
unsigned int nnl = 1 << 31;
bool no_new_line = (l & nnl) == nnl;
l &= ~nnl;
va_list ap;
if (!(fflAllowedType & t) || !(fflAllowedLevel & l))
return 0;
va_start(ap, format);
vsnprintf(buf, sizeof (buf), format, ap);
buf[sizeof (buf) - 1] = '\0';
va_end(ap);
if (no_new_line) {
fprintf(stderr, "%s", buf);
} else {
fprintf(stderr, "%s\n", buf);
}
fflush(stderr);
return 0;
};
bool _ffl_level(FF_LOG_TYPE t,
unsigned int l) {
if (((fflAllowedType & t) == t)&&((fflAllowedLevel & l) == l)) {
return true;
}
return false;
};
//void SetLogType(FF_LOG_TYPE t) {
// fflAllowedType = t;
//}
//
//void SetLogLevel(unsigned int l) {
// fflAllowedLevel = l;
//}
void PrintLogTypeNLevel(){
printf("fflAllowedType: %08X, fflAllowedLevel: %08X\n", (unsigned int)fflAllowedType, (unsigned int)fflAllowedLevel);
}