27 #define ZEROERR_INFO(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_INFO_(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
28 #define ZEROERR_LOG(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_LOG_(LOG_l, __VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
29 #define ZEROERR_WARN(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_LOG_(WARN_l, __VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
30 #define ZEROERR_ERROR(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_LOG_(ERROR_l, __VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
31 #define ZEROERR_FATAL(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_LOG_(FATAL_l, __VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
34 #ifdef ZEROERR_USE_SHORT_LOG_MACRO
60 #define INFO(...) ZEROERR_INFO(__VA_ARGS__)
61 #define LOG(...) ZEROERR_LOG(__VA_ARGS__)
62 #define WARN(...) ZEROERR_WARN(__VA_ARGS__)
63 #define ERR(...) ZEROERR_ERROR(__VA_ARGS__)
64 #define FATAL(...) ZEROERR_FATAL(__VA_ARGS__)
65 #define VERBOSE(v) ZEROERR_VERBOSE(v)
67 #define LOG_GET(func, id, name, type) ZEROERR_LOG_GET(func, id, name, type)
71 #define ZEROERR_LOG_IF(condition, ACTION, ...) \
73 if (condition) ACTION(__VA_ARGS__); \
77 #define INFO_IF(cond, ...) ZEROERR_LOG_IF(cond, ZEROERR_INFO, __VA_ARGS__)
78 #define LOG_IF(cond, ...) ZEROERR_LOG_IF(cond, ZEROERR_LOG, __VA_ARGS__)
79 #define WARN_IF(cond, ...) ZEROERR_LOG_IF(cond, ZEROERR_WARN, __VA_ARGS__)
80 #define ERR_IF(cond, ...) ZEROERR_LOG_IF(cond, ZEROERR_ERROR, __VA_ARGS__)
81 #define FATAL_IF(cond, ...) ZEROERR_LOG_IF(cond, ZEROERR_FATAL, __VA_ARGS__)
84 #define ZEROERR_LOG_EVERY_(n, ACTION, ...) \
86 unsigned counter = 0; \
89 ACTION(__VA_ARGS__); \
95 #define INFO_EVERY_(n, ...) ZEROERR_LOG_EVERY_(n, ZEROERR_INFO, __VA_ARGS__)
96 #define LOG_EVERY_(n, ...) ZEROERR_LOG_EVERY_(n, ZEROERR_LOG, __VA_ARGS__)
97 #define WARN_EVERY_(n, ...) ZEROERR_LOG_EVERY_(n, ZEROERR_WARN, __VA_ARGS__)
98 #define ERR_EVERY_(n, ...) ZEROERR_LOG_EVERY_(n, ZEROERR_ERROR, __VA_ARGS__)
99 #define FATAL_EVERY_(n, ...) ZEROERR_LOG_EVERY_(n, ZEROERR_FATAL, __VA_ARGS__)
102 #define ZEROERR_LOG_IF_EVERY_(n, cond, ACTION, ...) \
104 unsigned counter = 0; \
105 if (counter == 0 && (cond)) { \
107 ACTION(__VA_ARGS__); \
112 #define INFO_IF_EVERY_(n, cond, ...) ZEROERR_LOG_IF_EVERY_(n, cond, ZEROERR_INFO, __VA_ARGS__)
113 #define LOG_IF_EVERY_(n, cond, ...) ZEROERR_LOG_IF_EVERY_(n, cond, ZEROERR_LOG, __VA_ARGS__)
114 #define WARN_IF_EVERY_(n, cond, ...) ZEROERR_LOG_IF_EVERY_(n, cond, ZEROERR_WARN, __VA_ARGS__)
115 #define ERR_IF_EVERY_(n, cond, ...) ZEROERR_LOG_IF_EVERY_(n, cond, ZEROERR_ERROR, __VA_ARGS__)
116 #define FATAL_IF_EVERY_(n, cond, ...) ZEROERR_LOG_IF_EVERY_(n, cond, ZEROERR_FATAL, __VA_ARGS__)
118 #define ZEROERR_LOG_FIRST(cond, ACTION, ...) \
121 if (first && (cond)) { \
123 ACTION(__VA_ARGS__); \
127 #define INFO_FIRST(cond, ...) ZEROERR_LOG_FIRST(cond, ZEROERR_INFO, __VA_ARGS__)
128 #define LOG_FIRST(cond, ...) ZEROERR_LOG_FIRST(cond, ZEROERR_LOG, __VA_ARGS__)
129 #define WARN_FIRST(cond, ...) ZEROERR_LOG_FIRST(cond, ZEROERR_WARN, __VA_ARGS__)
130 #define ERR_FIRST(cond, ...) ZEROERR_LOG_FIRST(cond, ZEROERR_ERROR, __VA_ARGS__)
131 #define FATAL_FIRST(cond, ...) ZEROERR_LOG_FIRST(cond, ZEROERR_FATAL, __VA_ARGS__)
133 #define ZEROERR_LOG_FIRST_(n, cond, ACTION, ...) \
135 unsigned counter = n; \
136 if (n-- && (cond)) { \
137 ACTION(__VA_ARGS__); \
141 #define INFO_FIRST_(n, cond, ...) ZEROERR_LOG_FIRST_(n, cond, ZEROERR_INFO, __VA_ARGS__)
142 #define LOG_FIRST_(n, cond, ...) ZEROERR_LOG_FIRST_(n, cond, ZEROERR_LOG, __VA_ARGS__)
143 #define WARN_FIRST_(n, cond, ...) ZEROERR_LOG_FIRST_(n, cond, ZEROERR_WARN, __VA_ARGS__)
144 #define ERR_FIRST_(n, cond, ...) ZEROERR_LOG_FIRST_(n, cond, ZEROERR_ERROR, __VA_ARGS__)
145 #define FATAL_FIRST_(n, cond, ...) ZEROERR_LOG_FIRST_(n, cond, ZEROERR_FATAL, __VA_ARGS__)
148 #define DLOG(ACTION, ...) ZEROERR_EXPAND(ACTION(__VA_ARGS__))
150 #define DLOG(ACTION, ...)
155 #define ZEROERR_VERBOSE(v) if (zeroerr::_ZEROERR_G_VERBOSE >= (v))
157 #define ZEROERR_LOG_(severity, message, ...) \
159 ZEROERR_G_CONTEXT_SCOPE(true); \
160 auto msg = zeroerr::log(__VA_ARGS__); \
162 static zeroerr::LogInfo log_info{__FILE__, \
165 ZEROERR_LOG_CATEGORY, \
168 zeroerr::LogSeverity::severity}; \
169 msg.log->info = &log_info; \
170 if (msg.stream.getFlushMode() == zeroerr::LogStream::FlushMode::FLUSH_AT_ONCE) \
171 msg.stream.flush(); \
174 #define ZEROERR_INFO_(...) \
175 ZEROERR_INFO_IMPL(ZEROERR_NAMEGEN(_capture_), ZEROERR_NAMEGEN(_capture_), __VA_ARGS__)
177 #define ZEROERR_INFO_IMPL(mb_name, v_name, ...) \
178 auto v_name = zeroerr::MakeContextScope([&](std::ostream& _capture_name) { \
179 Printer print(_capture_name); \
180 print.isQuoted = false; \
181 print(__VA_ARGS__); \
184 #ifdef ZEROERR_G_CONTEXT_SCOPE
185 #undef ZEROERR_G_CONTEXT_SCOPE
188 #define ZEROERR_G_CONTEXT_SCOPE(x) \
190 for (auto* i : zeroerr::_ZEROERR_G_CONTEXT_SCOPE_VECTOR) { \
195 #ifdef ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER
196 #undef ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER
199 #define ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER(cond, level, ...) \
200 ZEROERR_LOG_IF(cond, level, __VA_ARGS__)
204 #define ZEROERR_LOG_GET(func, id, name, type) \
205 zeroerr::LogStream::getDefault().getLog<type>(#func, id, #name)
210 template <
typename T,
unsigned... I>
212 return format(msg, std::get<I>(args)...);
215 template <
typename T>
258 const char*
function;
311 virtual std::string
str()
const = 0;
319 virtual std::map<std::string, std::string>
getData()
const = 0;
325 std::chrono::system_clock::time_point
time;
335 template <
typename... T>
340 std::string
str()
const override {
347 template <
typename H>
360 std::map<std::string, std::string>
data;
369 template <
typename H>
375 std::map<std::string, std::string>
getData()
const override {
378 printer.
name = it->first;
437 template <
typename T>
440 if (data)
return *(T*)(data);
511 template <
typename... T>
517 p = alloc_block_lockfree(size);
519 p = alloc_block(size);
522 return {msg, size, *
this};
537 template <
typename T>
538 T
getLog(std::string func,
unsigned line, std::string name) {
539 void* data = getRawLog(func, line, name);
540 if (data)
return *(T*)(data);
555 template <
typename T>
556 T
getLog(std::string func, std::string msg, std::string name) {
557 void* data = getRawLog(func, msg, name);
558 if (data)
return *(T*)(data);
562 LogIterator begin(std::string message =
"", std::string function_name =
"",
int line = -1) {
563 return LogIterator(*
this, message, function_name, line);
566 LogIterator current(std::string message =
"", std::string function_name =
"",
int line = -1);
597 #ifndef ZEROERR_NO_THREAD_SAFE
602 void* alloc_block(
unsigned size);
603 void* alloc_block_lockfree(
unsigned size);
607 void* getRawLog(std::string func,
unsigned line, std::string name);
608 void* getRawLog(std::string func, std::string msg, std::string name);
612 template <
typename... T>
617 template <
typename... T>
619 return stream.
push(std::forward<T>(args)...);
630 virtual void str(std::ostream& os)
const = 0;
635 template <
typename F>
641 virtual void str(std::ostream& os)
const override {
return f_(os); }
647 template <
typename F>
ContextScope(F f)
Definition: log.h:638
F f_
Definition: log.h:644
virtual void str(std::ostream &os) const override
Definition: log.h:641
~ContextScope()
Definition: log.h:639
ContextScope is a helper class created in each basic block where you use INFO(). The context scope ca...
Definition: log.h:628
virtual void str(std::ostream &os) const =0
LogIterator is a class to iterate the log messages.
Definition: log.h:418
void check_at_safe_pos()
Definition: log.cpp:182
DataBlock * p
Definition: log.h:459
LogIterator & operator++()
Definition: log.cpp:202
LogMessage & get() const
Definition: log.h:447
int line_filter
Definition: log.h:464
LogMessage & operator*() const
Definition: log.h:448
LogIterator(const LogIterator &rhs)
Definition: log.h:423
std::string message_filter
Definition: log.h:463
LogIterator operator++(int)
Definition: log.h:431
std::string function_name_filter
Definition: log.h:462
LogIterator()
Definition: log.h:420
LogIterator & operator=(const LogIterator &rhs)
Definition: log.h:424
bool operator!=(const LogIterator &rhs) const
Definition: log.h:445
LogMessage * q
Definition: log.h:460
bool operator==(const LogIterator &rhs) const
Definition: log.h:444
T get(std::string name)
Definition: log.h:438
bool check_filter()
Definition: log.cpp:209
void next()
Definition: log.cpp:189
LogMessage * operator->() const
Definition: log.h:449
LogStream is a class to manage the log messages.
Definition: log.h:475
LogIterator begin(std::string message="", std::string function_name="", int line=-1)
Definition: log.h:562
void setFlushMode(FlushMode mode)
Definition: log.h:583
void setFlushManually()
Definition: log.h:578
T getLog(std::string func, unsigned line, std::string name)
get a log message from the stream
Definition: log.h:538
void setLogMode(LogMode mode)
Definition: log.h:585
void setFlushWhenFull()
Definition: log.h:577
void setFileLogger(std::string name, DirMode mode1=SINGLE_FILE, DirMode mode2=SINGLE_FILE, DirMode mode3=SINGLE_FILE)
Definition: log.cpp:377
LogMode
Definition: log.h:481
@ SYNC
Definition: log.h:481
@ ASYNC
Definition: log.h:481
virtual ~LogStream()
Definition: log.cpp:72
void flush()
Definition: log.cpp:140
void setAsyncLog()
Definition: log.h:579
LogStream()
Definition: log.cpp:63
FlushMode
Definition: log.h:480
@ FLUSH_AT_ONCE
Definition: log.h:480
@ FLUSH_MANUALLY
Definition: log.h:480
@ FLUSH_WHEN_FULL
Definition: log.h:480
void setSyncLog()
Definition: log.h:580
LogIterator current(std::string message="", std::string function_name="", int line=-1)
Definition: log.cpp:132
LogMode getLogMode() const
Definition: log.h:584
PushResult push(T &&... args)
push a log message to the stream
Definition: log.h:512
bool use_lock_free
Definition: log.h:587
DirMode
Definition: log.h:482
@ SPLIT_BY_SEVERITY
Definition: log.h:485
@ SPLIT_BY_CATEGORY
Definition: log.h:486
@ DAILY_FILE
Definition: log.h:484
@ SINGLE_FILE
Definition: log.h:483
void setStderrLogger()
Definition: log.cpp:394
void setFlushAtOnce()
Definition: log.h:576
FlushMode getFlushMode() const
Definition: log.h:582
void setStdoutLogger()
Definition: log.cpp:389
friend class LogIterator
Definition: log.h:589
LogIterator end()
Definition: log.h:565
T getLog(std::string func, std::string msg, std::string name)
get a log message from the stream
Definition: log.h:556
static LogStream & getDefault()
Definition: log.cpp:372
virtual void flush(DataBlock *)=0
virtual ~Logger()=default
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition: config.h:265
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition: config.h:218
ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH const char * ZEROERR_LOG_CATEGORY
Definition: log.cpp:13
std::string gen_str(const char *msg, const T &args, seq< I... >)
Definition: log.h:211
typename to_store_type< T >::type to_store_type_t
Definition: typetraits.h:171
std::string gen_str(const char *msg, const T &, seq<>)
Definition: log.h:216
void visit_at(const std::tuple< Ts... > &tup, size_t idx, F &&fun)
Definition: typetraits.h:192
Definition: benchmark.cpp:17
void suspendLog()
suspend the log to flush to the file
Definition: log.cpp:427
thread_local std::vector< IContextScope * > _ZEROERR_G_CONTEXT_SCOPE_VECTOR
Definition: log.cpp:20
void setLogCustomCallback(LogCustomCallback callback)
set the log custom callback, this can support custom format of the log message
Definition: log.cpp:24
LogSeverity
Definition: log.h:223
@ FATAL_l
Definition: log.h:228
@ WARN_l
Definition: log.h:226
@ LOG_l
Definition: log.h:225
@ INFO_l
Definition: log.h:224
@ ERROR_l
Definition: log.h:227
PushResult log(T &&... args)
Definition: log.h:613
void resumeLog()
resume the log to flush to the file
Definition: log.cpp:432
void setLogLevel(LogSeverity level)
set the log level
Definition: log.cpp:406
ContextScope< F > MakeContextScope(const F &f)
Definition: log.h:648
std::string format(const char *fmt, T... args)
Format a string with arguments.
Definition: format.h:25
std::string(* LogCustomCallback)(const LogMessage &, bool colorful)
Definition: log.h:271
void setLogCategory(const char *categories)
set the log category
Definition: log.cpp:408
int _ZEROERR_G_VERBOSE
Definition: log.cpp:18
LogInfo is a struct to store the meta data of the log message.
Definition: log.h:256
LogSeverity severity
Definition: log.h:263
std::map< std::string, int > names
Definition: log.h:264
const char * category
Definition: log.h:260
const char * filename
Definition: log.h:257
unsigned line
Definition: log.h:261
LogInfo(const char *filename, const char *function, const char *message, const char *category, unsigned line, unsigned size, LogSeverity severity)
Definition: log.cpp:27
unsigned size
Definition: log.h:262
const char * message
Definition: log.h:259
void * ptr
Definition: log.h:346
void operator()(H &v)
Definition: log.h:348
PrintTupleData()
Definition: log.h:364
Printer print
Definition: log.h:361
std::map< std::string, std::string > data
Definition: log.h:360
std::string name
Definition: log.h:362
void operator()(H &v)
Definition: log.h:370
LogMessageImpl is the implementation of the LogMessage.
Definition: log.h:336
void * getRawLog(std::string name) const override
Definition: log.h:353
LogMessageImpl(T... args)
Definition: log.h:338
std::tuple< T... > args
Definition: log.h:337
std::map< std::string, std::string > getData() const override
Definition: log.h:375
std::string str() const override
Definition: log.h:340
LogMessage is a class to store the log message.
Definition: log.h:306
std::chrono::system_clock::time_point time
Definition: log.h:325
virtual std::string str() const =0
const LogInfo * info
Definition: log.h:322
virtual std::map< std::string, std::string > getData() const =0
LogMessage()
Definition: log.h:308
virtual void * getRawLog(std::string name) const =0
A functor class Printer for printing a value of any type.
Definition: print.h:80
bool isCompact
Definition: print.h:122
const char * line_break
Definition: print.h:125
LogStream & stream
Definition: log.h:397
LogMessage * log
Definition: log.h:395
unsigned size
Definition: log.h:396
Definition: typetraits.h:58
Definition: typetraits.h:55
#define ZEROERR_ATOMIC(x)
Definition: threadsafe.h:16