-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
/* | ||
* tinylog | ||
* Copyright (C) 2018-2020 Ruilin Peng (Nick) <[email protected]> | ||
* Copyright (C) 2018-2021 Ruilin Peng (Nick) <[email protected]> | ||
* https://github.com/pymumu/tinylog | ||
*/ | ||
|
||
#ifndef TLOG_H | ||
#define TLOG_H | ||
#include <stdarg.h> | ||
#include <sys/stat.h> | ||
|
||
#ifdef __cplusplus | ||
#include <functional> | ||
|
@@ -196,66 +197,83 @@ extern int tlog_localtime(struct tlog_time *tm); | |
/* set max line size */ | ||
extern void tlog_set_maxline_size(struct tlog_log *log, int size); | ||
|
||
/* | ||
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); | ||
|
||
#ifdef __cplusplus | ||
class Tlog { | ||
using Stream = std::ostringstream; | ||
using Buffer = std::unique_ptr<Stream, std::function<void(Stream *)>>; | ||
|
||
public: | ||
Tlog() { } | ||
~Tlog() { } | ||
Tlog(tlog_level level, const char *file, int line, const char *func, void *userptr) | ||
{ | ||
level_ = level; | ||
file_ = file; | ||
line_ = line; | ||
func_ = func; | ||
userptr_ = userptr; | ||
} | ||
|
||
static Tlog &Instance() | ||
~Tlog() | ||
{ | ||
static Tlog logger; | ||
return logger; | ||
tlog_ext(level_, file_, line_, func_, userptr_, "%s", msg_.str().c_str()); | ||
} | ||
|
||
Buffer LogStream(tlog_level level, const char *file, int line, const char *func, void *userptr) | ||
std::ostream &Stream() | ||
{ | ||
return Buffer(new Stream, [=](Stream *st) { | ||
tlog_ext(level, file, line, func, userptr, "%s", st->str().c_str()); | ||
delete st; | ||
}); | ||
return msg_; | ||
} | ||
|
||
private: | ||
tlog_level level_; | ||
const char *file_; | ||
int line_; | ||
const char *func_; | ||
void *userptr_; | ||
std::ostringstream msg_; | ||
}; | ||
|
||
class TlogOut { | ||
using Stream = std::ostringstream; | ||
using Buffer = std::unique_ptr<Stream, std::function<void(Stream *)>>; | ||
|
||
public: | ||
TlogOut() { } | ||
~TlogOut() { } | ||
TlogOut(tlog_log *log) | ||
{ | ||
log_ = log; | ||
} | ||
|
||
static TlogOut &Instance() | ||
~TlogOut() | ||
{ | ||
static TlogOut logger; | ||
return logger; | ||
if (log_ == nullptr) { | ||
return; | ||
} | ||
|
||
tlog_printf(log_, "%s", msg_.str().c_str()); | ||
} | ||
|
||
Buffer Out(tlog_log *log) | ||
std::ostream &Stream() | ||
{ | ||
return Buffer(new Stream, [=](Stream *st) { | ||
tlog_printf(log, "%s", st->str().c_str()); | ||
delete st; | ||
}); | ||
return msg_; | ||
} | ||
|
||
private: | ||
tlog_log *log_; | ||
std::ostringstream msg_; | ||
}; | ||
|
||
#define Tlog_logger (Tlog::Instance()) | ||
#define Tlog_stream(level) \ | ||
if (tlog_getlevel() <= level) \ | ||
*Tlog_logger.LogStream(level, BASE_FILE_NAME, __LINE__, __func__, NULL) | ||
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_logger (TlogOut::Instance()) | ||
#define tlog_out(stream) (*Tlog_out_logger.Out(stream)) | ||
#define tlog_out(stream) TlogOut(stream).Stream() | ||
|
||
} /*__cplusplus */ | ||
#else | ||
|