Introduction to c ++ log output library spdlog (2), output spdlog

Source: Internet
Author: User

Introduction to c ++ log output library spdlog (2), output spdlog

Continue with the previous article, example. cpp parsing.

1. set_pattern custom log format

Official reference: https://github.com/gabime/spdlog/wiki/3.-Custom-formatting

You can specify the format for all logs or the specified log. Note that rotating_logger is the pointer variable in the following code.

auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 256, 2);
// Customize msg format for all messages        spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***");        rotating_logger->info("This is another message with custom format");        //Customize msg format for a specific logger object:        rotating_logger->set_pattern("[%H:%M:%S %f] [thread %t] %v ***");

Detailed format descriptions are available on github as follows:

2. set_level: Set the Log Level

Logs lower than the set level will not be output. Sort by level. The higher the value, the higher the level:

// Runtime log levels        spd::set_level(spd::level::info); //Set global log level to info        console->debug("This message should not be displayed!");        console->set_level(spd::level::debug); // Set specific logger's log level        console->debug("This message should be displayed..");

The debug level of the first line of log is lower than the set level info. It is not output when the level is info.

The debug level of the second line of log is the same as that set, so it can be displayed.

typedef enum{    trace = 0,    debug = 1,    info = 2,    warn = 3,    err = 4,    critical = 5,    off = 6} level_enum;

3. Modify the log output level SPDLOG_TRACE and SPDLOG_DEBUG in the compilation phase.

Official reference: https://github.com/gabime/spdlog/wiki/8.-Tweaking

When defining a macro to define SPDLOG_TRACE_ON, you can use the SPDLOG_TRACE statement to output trace-level logs. The same applies to SPDLOG_DEBUG_ON.

#define SPDLOG_TRACE_ON#define SPDLOG_DEBUG_ON
// Compile time log levels        // define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON        SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23);        SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23);

Note that if you do not use the set_level command to set the log output level, the default level is the info level. Even if the two macros are defined, the debug and trace information will not be output. Therefore, you need to set the level to trace before using set_level.

console->set_level(spd::level::trace); // Set specific logger's log level        // Compile time log levels        // define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON        SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23);        SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23);

The output result is as follows:

Note that the trace output is not the same as the debug output. Open spdlog. h and check the definition of SPDLOG_TRACE. You can find that the trace also outputs the FILE location ("_ FILE __").

#ifdef SPDLOG_TRACE_ON#  define SPDLOG_STR_H(x) #x#  define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x)#  ifdef _MSC_VER#    define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__)#  else#    define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__)#  endif#else#  define SPDLOG_TRACE(logger, ...) (void)0#endif#ifdef SPDLOG_DEBUG_ON#  define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__)#else#  define SPDLOG_DEBUG(logger, ...) (void)0#endif

4. Set Asynchronous logging synchronously and Asynchronously

Official Description: https://github.com/gabime/spdlog/wiki/6.-Asynchronous-logging

By default, the asynchronous mode is disabled. The asynchronous mode is enabled as follows:

size_t q_size = 4096; //queue size must be power of 2    spdlog::set_async_mode(q_size);

Queue mechanism:

In asynchronous mode, all output logs are first stored in the queue, and then the worker thread extracts and outputs the logs from the queue. When the queue is full, you need to handle new logs (blocking or discarding messages) based on the configured queue full policy ). If an exception is thrown in the worker thread, the exception will be thrown again when the next log is written to the queue, and the exception of the worker thread can be captured when the queue is written.

Full queue policy:

(1) block new logs until there is space in the queue. This is the default processing method.

(2) discard new logs. As follows:

spdlog::set_async_mode(q_size, spdlog::async_overflow_policy::discard_log_msg)

The two methods are as follows:

// Async overflow policy - block by default.//enum class async_overflow_policy{    block_retry, // Block / yield / sleep until message can be enqueued    discard_log_msg // Discard the message it enqueue fails};


5. drop -- release logger

When the program ends, you should call drop_all () to release all logger.

There is a bug in VS runtime that cause the application dead lock when it exits. if you use async logging, please make sure to call spdlog: drop_all () before main () exit. if some loggers are not in the registry, those shoshould be released manually as well. stackoverflow: std: thread join hangs if called after main exits when using vs2012 rc

Call spdlog in MFC

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.