Share a simplified LOG output macro written by myself, and a simplified log output macro

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.