-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathtlog.h
316 lines (254 loc) · 8.47 KB
/
tlog.h
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* tinylog
* Copyright (C) 2018-2024 Ruilin Peng (Nick) <[email protected]>
* https://github.com/pymumu/tinylog
*/
#ifndef TLOG_H
#define TLOG_H
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef __cplusplus
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
extern "C" {
#endif /*__cplusplus */
typedef enum {
TLOG_DEBUG = 0,
TLOG_INFO = 1,
TLOG_NOTICE = 2,
TLOG_WARN = 3,
TLOG_ERROR = 4,
TLOG_FATAL = 5,
TLOG_OFF = 6,
TLOG_END = 7
} tlog_level;
struct tlog_time {
int year;
unsigned int usec;
unsigned char mon;
unsigned char mday;
unsigned char hour;
unsigned char min;
unsigned char sec;
} __attribute__((packed));
#ifndef TLOG_MAX_LINE_LEN
#define TLOG_MAX_LINE_LEN (1024)
#endif
/* TLOG FLAGS LIST */
/* set tlog not compress file when archive */
#define TLOG_NOCOMPRESS (1 << 0)
/* Set the segmentation mode to process the log, Used by the callback function to return a full log*/
#define TLOG_SEGMENT (1 << 1)
/*
multiwrite: enable multi process write mode.
NOTICE: maxlogsize in all processes must be same when enable this mode.
*/
#define TLOG_MULTI_WRITE (1 << 2)
/* Not Block if buffer is insufficient. */
#define TLOG_NONBLOCK (1 << 3)
/* enable log to screen */
#define TLOG_SCREEN (1 << 4)
/* enable support fork process */
#define TLOG_SUPPORT_FORK (1 << 5)
/* enable output to screen with color */
#define TLOG_SCREEN_COLOR (1 << 6)
/* Not output prefix */
#define TLOG_FORMAT_NO_PREFIX (1 << 7)
struct tlog_loginfo {
tlog_level level;
const char *file;
const char *func;
int line;
struct tlog_time time;
} __attribute__((packed));
/*
Function: Print log
level: Current log Levels
format: Log formats
*/
#ifndef BASE_FILE_NAME
#define BASE_FILE_NAME \
(__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 \
: __FILE__)
#endif
#define tlog(level, format, ...) tlog_ext(level, BASE_FILE_NAME, __LINE__, __func__, NULL, format, ##__VA_ARGS__)
extern int tlog_ext(tlog_level level, const char *file, int line, const char *func, void *userptr, const char *format, ...)
__attribute__((format(printf, 6, 7))) __attribute__((nonnull(6)));
extern int tlog_vext(tlog_level level, const char *file, int line, const char *func, void *userptr, const char *format, va_list ap);
/* write buff to log file */
extern int tlog_write_log(char *buff, int bufflen);
/* set log level */
extern int tlog_setlevel(tlog_level level);
/* is log level enabled*/
extern int tlog_log_enabled(tlog_level level);
/* get log level */
extern tlog_level tlog_getlevel(void);
/* set log file */
extern void tlog_set_logfile(const char *logfile);
/* enable log to screen */
extern void tlog_setlogscreen(int enable);
/* Get log level in string */
extern const char *tlog_get_level_string(tlog_level level);
/* set max log count */
extern void tlog_set_maxlog_count(int count);
/*
Function: Initialize log module
logfile: log file.
maxlogsize: The maximum size of a single log file.
maxlogcount: Number of archived logs.
buffsize: Buffer size, zero for default (128K)
flag: read tlog flags
*/
extern int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag);
/* flush pending log message, and exit tlog */
extern void tlog_exit(void);
/*
customize log output format
steps:
1. define format function, function must be defined as tlog_format_func, use snprintf or vsnprintf format log to buffer
2. call tlog_reg_format_func to register format function.
read _tlog_format for example.
*/
typedef int (*tlog_format_func)(char *buff, int maxlen, struct tlog_loginfo *info, void *userptr, const char *format, va_list ap);
extern int tlog_reg_format_func(tlog_format_func callback);
/* register log output callback
Note: info is invalid when flag TLOG_SEGMENT is not set.
*/
typedef int (*tlog_log_output_func)(struct tlog_loginfo *info, const char *buff, int bufflen, void *private_data);
extern int tlog_reg_log_output_func(tlog_log_output_func output, void *private_data);
/* enable early log to screen */
extern void tlog_set_early_printf(int enable, int no_prefix, int color);
/* set early log callback */
typedef void (*tlog_early_print_func)(struct tlog_loginfo *loginfo, const char *format, va_list ap);
extern void tlog_reg_early_printf_callback(tlog_early_print_func callback);
/* set early log output callback */
extern void tlog_reg_early_printf_output_callback(tlog_log_output_func callback, int log_screen, void *private_data);
struct tlog_log;
typedef struct tlog_log tlog_log;
/* get root log handler */
extern tlog_log *tlog_get_root(void);
/*
Function: open a new log stream, handler should close by tlog_close
logfile: log file.
maxlogsize: The maximum size of a single log file.
maxlogcount: Number of archived logs.
buffsize: Buffer size, zero for default (128K)
flag: read tlog flags
return: log stream handler.
*/
extern tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag);
/* write buff to log file */
extern int tlog_write(struct tlog_log *log, const char *buff, int bufflen);
/* close log stream */
extern void tlog_close(tlog_log *log);
/* change log file */
extern void tlog_rename_logfile(struct tlog_log *log, const char *logfile);
/*
Function: Print log to log stream
log: log stream
format: Log formats
*/
extern int tlog_printf(tlog_log *log, const char *format, ...) __attribute__((format(printf, 2, 3))) __attribute__((nonnull(1, 2)));
/*
Function: Print log to log stream with ap
log: log stream
format: Log formats
va_list: args list
*/
extern int tlog_vprintf(tlog_log *log, const char *format, va_list ap);
/* enable log to screen */
extern void tlog_logscreen(tlog_log *log, int enable);
/* register output callback */
typedef int (*tlog_output_func)(struct tlog_log *log, const char *buff, int bufflen);
extern int tlog_reg_output_func(tlog_log *log, tlog_output_func output);
/* set private data */
extern void tlog_set_private(tlog_log *log, void *private_data);
/* get private data */
extern void *tlog_get_private(tlog_log *log);
/* get local time */
extern int tlog_localtime(struct tlog_time *tm);
/* set max line size */
extern void tlog_set_maxline_size(struct tlog_log *log, int size);
/* set max log count */
extern void tlog_logcount(struct tlog_log *log, int count);
/*
Function: set log file and archive permission
log: log stream
file: log file permission, default is 640
archive: archive file permission, default is 440
*/
extern void tlog_set_permission(struct tlog_log *log, mode_t file, mode_t archive);
/* Utility function, output colored logs to standard output */
extern int tlog_stdout_with_color(tlog_level level, const char *buff, int bufflen);
#ifdef __cplusplus
class Tlog {
public:
Tlog(tlog_level level, const char *file, int line, const char *func, void *userptr)
{
level_ = level;
file_ = file;
line_ = line;
func_ = func;
userptr_ = userptr;
}
~Tlog()
{
tlog_ext(level_, file_, line_, func_, userptr_, "%s", msg_.str().c_str());
}
std::ostream &Stream()
{
return msg_;
}
private:
tlog_level level_;
const char *file_;
int line_;
const char *func_;
void *userptr_;
std::ostringstream msg_;
};
class TlogOut {
public:
TlogOut(tlog_log *log)
{
log_ = log;
}
~TlogOut()
{
if (log_ == nullptr) {
return;
}
tlog_printf(log_, "%s", msg_.str().c_str());
}
std::ostream &Stream()
{
return msg_;
}
private:
tlog_log *log_;
std::ostringstream msg_;
};
#define Tlog_stream(level) \
if (tlog_getlevel() <= level) \
Tlog(level, BASE_FILE_NAME, __LINE__, __func__, NULL).Stream()
#define tlog_debug Tlog_stream(TLOG_DEBUG)
#define tlog_info Tlog_stream(TLOG_INFO)
#define tlog_notice Tlog_stream(TLOG_NOTICE)
#define tlog_warn Tlog_stream(TLOG_WARN)
#define tlog_error Tlog_stream(TLOG_ERROR)
#define tlog_fatal Tlog_stream(TLOG_FATAL)
#define tlog_out(stream) TlogOut(stream).Stream()
} /*__cplusplus */
#else
#define tlog_debug(...) tlog(TLOG_DEBUG, ##__VA_ARGS__)
#define tlog_info(...) tlog(TLOG_INFO, ##__VA_ARGS__)
#define tlog_notice(...) tlog(TLOG_NOTICE, ##__VA_ARGS__)
#define tlog_warn(...) tlog(TLOG_WARN, ##__VA_ARGS__)
#define tlog_error(...) tlog(TLOG_ERROR, ##__VA_ARGS__)
#define tlog_fatal(...) tlog(TLOG_FATAL, ##__VA_ARGS__)
#endif
#endif // !TLOG_H