Share a simplified LOG output macro written by myself, and a simplified log output macro
Share a simplified LOG output macro written by yourself
extern int verbose;#define DBG(...) XLOG(4, "DBG", __VA_ARGS__)#define INFO(...) XLOG(3, "INF", __VA_ARGS__)#define WRN(...) XLOG(2, "WRN", __VA_ARGS__)#define ERR(...) XLOG(1, "ERR", __VA_ARGS__)#define OUT(...) XLOG(0, "OUT", __VA_ARGS__)#define XLOG(level, LOG_LEVEL, ...) \do{\ if(verbose >= (level)) {\ time_t _tm = 0;\ struct tm log_tm = {0};\ time(&_tm);\ localtime_r(&_tm, &log_tm);\ fprintf(stdout, "%4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d %3s | ", log_tm.tm_year+1900, log_tm.tm_mon+1, log_tm.tm_mday, log_tm.tm_hour, log_tm.tm_min, log_tm.tm_sec, LOG_LEVEL);\ fprintf(stdout, __VA_ARGS__);}\ }\}while(0)
Verbose is a global variable used to limit the output level. Information smaller than this level will be output. For example, if verbose = 4, all information will be output. If verbose = 3, DBG information is not output.
The usage is the same as that of printf, except that printf is replaced with DBG and INFO.
DBG ("abcdefg % d. \ n", );
This is a very simplified version and can only be output to the screen. You only need to add it to a. h file.
Another version requires. c and. h, which is more complex to use: