|
1 | 1 | /*
|
2 | 2 | * tinylog
|
3 |
| - * Copyright (C) 2018-2020 Ruilin Peng (Nick) <[email protected]> |
| 3 | + * Copyright (C) 2018-2021 Ruilin Peng (Nick) <[email protected]> |
4 | 4 | * https://github.com/pymumu/tinylog
|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | #ifndef TLOG_H
|
8 | 8 | #define TLOG_H
|
9 | 9 | #include <stdarg.h>
|
| 10 | +#include <sys/stat.h> |
10 | 11 |
|
11 | 12 | #ifdef __cplusplus
|
12 | 13 | #include <functional>
|
@@ -196,66 +197,83 @@ extern int tlog_localtime(struct tlog_time *tm);
|
196 | 197 | /* set max line size */
|
197 | 198 | extern void tlog_set_maxline_size(struct tlog_log *log, int size);
|
198 | 199 |
|
| 200 | +/* |
| 201 | +Function: set log file and archive permission |
| 202 | +log: log stream |
| 203 | +file: log file permission, default is 640 |
| 204 | +archive: archive file permission, default is 440 |
| 205 | +*/ |
| 206 | + |
| 207 | +extern void tlog_set_permission(struct tlog_log *log, mode_t file, mode_t archive); |
| 208 | + |
199 | 209 | #ifdef __cplusplus
|
200 | 210 | class Tlog {
|
201 |
| - using Stream = std::ostringstream; |
202 |
| - using Buffer = std::unique_ptr<Stream, std::function<void(Stream *)>>; |
203 |
| - |
204 | 211 | public:
|
205 |
| - Tlog() { } |
206 |
| - ~Tlog() { } |
| 212 | + Tlog(tlog_level level, const char *file, int line, const char *func, void *userptr) |
| 213 | + { |
| 214 | + level_ = level; |
| 215 | + file_ = file; |
| 216 | + line_ = line; |
| 217 | + func_ = func; |
| 218 | + userptr_ = userptr; |
| 219 | + } |
207 | 220 |
|
208 |
| - static Tlog &Instance() |
| 221 | + ~Tlog() |
209 | 222 | {
|
210 |
| - static Tlog logger; |
211 |
| - return logger; |
| 223 | + tlog_ext(level_, file_, line_, func_, userptr_, "%s", msg_.str().c_str()); |
212 | 224 | }
|
213 | 225 |
|
214 |
| - Buffer LogStream(tlog_level level, const char *file, int line, const char *func, void *userptr) |
| 226 | + std::ostream &Stream() |
215 | 227 | {
|
216 |
| - return Buffer(new Stream, [=](Stream *st) { |
217 |
| - tlog_ext(level, file, line, func, userptr, "%s", st->str().c_str()); |
218 |
| - delete st; |
219 |
| - }); |
| 228 | + return msg_; |
220 | 229 | }
|
| 230 | + |
| 231 | +private: |
| 232 | + tlog_level level_; |
| 233 | + const char *file_; |
| 234 | + int line_; |
| 235 | + const char *func_; |
| 236 | + void *userptr_; |
| 237 | + std::ostringstream msg_; |
221 | 238 | };
|
222 | 239 |
|
223 | 240 | class TlogOut {
|
224 |
| - using Stream = std::ostringstream; |
225 |
| - using Buffer = std::unique_ptr<Stream, std::function<void(Stream *)>>; |
226 |
| - |
227 | 241 | public:
|
228 |
| - TlogOut() { } |
229 |
| - ~TlogOut() { } |
| 242 | + TlogOut(tlog_log *log) |
| 243 | + { |
| 244 | + log_ = log; |
| 245 | + } |
230 | 246 |
|
231 |
| - static TlogOut &Instance() |
| 247 | + ~TlogOut() |
232 | 248 | {
|
233 |
| - static TlogOut logger; |
234 |
| - return logger; |
| 249 | + if (log_ == nullptr) { |
| 250 | + return; |
| 251 | + } |
| 252 | + |
| 253 | + tlog_printf(log_, "%s", msg_.str().c_str()); |
235 | 254 | }
|
236 | 255 |
|
237 |
| - Buffer Out(tlog_log *log) |
| 256 | + std::ostream &Stream() |
238 | 257 | {
|
239 |
| - return Buffer(new Stream, [=](Stream *st) { |
240 |
| - tlog_printf(log, "%s", st->str().c_str()); |
241 |
| - delete st; |
242 |
| - }); |
| 258 | + return msg_; |
243 | 259 | }
|
| 260 | + |
| 261 | +private: |
| 262 | + tlog_log *log_; |
| 263 | + std::ostringstream msg_; |
244 | 264 | };
|
245 | 265 |
|
246 |
| -#define Tlog_logger (Tlog::Instance()) |
247 | 266 | #define Tlog_stream(level) \
|
248 | 267 | if (tlog_getlevel() <= level) \
|
249 |
| - *Tlog_logger.LogStream(level, BASE_FILE_NAME, __LINE__, __func__, NULL) |
| 268 | + Tlog(level, BASE_FILE_NAME, __LINE__, __func__, NULL).Stream() |
250 | 269 | #define tlog_debug Tlog_stream(TLOG_DEBUG)
|
251 | 270 | #define tlog_info Tlog_stream(TLOG_INFO)
|
252 | 271 | #define tlog_notice Tlog_stream(TLOG_NOTICE)
|
253 | 272 | #define tlog_warn Tlog_stream(TLOG_WARN)
|
254 | 273 | #define tlog_error Tlog_stream(TLOG_ERROR)
|
255 | 274 | #define tlog_fatal Tlog_stream(TLOG_FATAL)
|
256 | 275 |
|
257 |
| -#define Tlog_out_logger (TlogOut::Instance()) |
258 |
| -#define tlog_out(stream) (*Tlog_out_logger.Out(stream)) |
| 276 | +#define tlog_out(stream) TlogOut(stream).Stream() |
259 | 277 |
|
260 | 278 | } /*__cplusplus */
|
261 | 279 | #else
|
|
0 commit comments