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

Source: Internet
Author: User

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

References:

Introduction and use of spdlog library-network resources are unlimited-CSDN blog http://blog.csdn.net/fengbingchun/article/details/78347105

Use of spdLog-column of Yunxiao bug-CSDN blog http://blog.csdn.net/yanxiaobugyunsan/article/details/79088533

Official reference: QuickStart · gabime/spdlog Wiki · GitHub

Https://github.com/gabime/spdlog/wiki/1.-QuickStart


1. Download source code

Code address in https://github.com/gabime/spdlog

Click downLoad to downLoad.

2. example Parsing

Download the compressed package and decompress it: Use visual studio to open the project file with the vcxproj suffix (VS2013 is used)

Find example. cpp in the solution. The source file illustrates the usage of spdlog:

First, you must include the spdlog header file.

#include "spdlog/spdlog.h"

And declare the namespace of spdlog.

namespace spd = spdlog;

(1) log output on the console

To output logs on the console, you need these two header files:

# Include <iostream>
# Include <memory>

The Code is as follows:

// Console logger with color        auto console = spd::stdout_color_mt("console");        console->info("Welcome to spdlog!");        console->error("Some error message with arg{}..", 1);        // Formatting examples        console->warn("Easy padding in numbers like {:08d}", 12);        console->critical("Support for int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);        console->info("Support for floats {:03.2f}", 1.23456);        console->info("Positional args are {1} {0}..", "too", "supported");        console->info("{:<30}", "left aligned");        spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function");

Auto console = spd: stdout_color_mt ("console"); "console" is the logger name, which can be named at will.

Warn, critical, and info are logs of different levels. The output is displayed in different colors on the console.

Note: After logger is used up, you need to call the drop function to release the logger object before the program is closed. Otherwise, logger with the same name cannot be created if the program is not closed.

In example. cpp, the main function is called at the end.

// Release and close all loggers        spdlog::drop_all();

If you only want to disable the console log, you can write it as follows:

spd::drop("basic_logger");

(2) basic log

Without scrolling, log files are constantly written and become larger.

// Create basic file logger (not rotated)        auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt");        my_logger->info("Some log message");

(3) rotating log

Scroll logs. When the log file exceeds the specified size, all content in the current log file will be deleted and written again.

From the function declaration, we can see that the max_file_size parameter specifies the maximum value of the file. If the file content exceeds this value, it will be cleared.

rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)

The max_files parameter specifies the number of rolling files. When logger_name is full, change its name to logger_name.1, and create a new logger_name file to store new logs. When logger_name.1 is full again, change logger_name.2, logger_name to logger_name.1, and create a new logger_name to store new logs. If the number of max_files is a few, several logger_name files can be used to scroll.

The following example generates three log files.

// Create a file rotating logger with 5mb size max and 3 rotated files        //auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);        auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 256, 2);        for (int i = 0; i < 10; ++i)            rotating_logger->info("{} * {} equals {:>10}", i, i, i*i);

The content of each file is as follows. The larger the suffix number, the earlier the log Content:

(4) daily log

A log file is created every day. You can set the time for creating a log file.

// Create a daily logger - a new file is created every day on 2:30am        auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);        // trigger flush if the log severity is error or higher        daily_logger->flush_on(spd::level::err);        daily_logger->info(123.44);

Logs output by the code above. If the program does not exit, a new file is created at every day. If you run this program multiple times a day, there will be multiple log files, such:

In order to write the daily log to the same file, refer to the http://blog.csdn.net/yanxiaobugyunsan/article/details/79088533

You can write as follows:

// Create a file name similar to: log_2018-01-17.txt typedef spdlog: sinks: daily_file_sink <std: mutex, spdlog: sinks: rows> dateonly_daily_file_sink_mt; auto m_logger = spdlog :: create <dateonly_daily_file_sink_mt> ("m_logger", "logs/dateonly.txt", 0, 0); m_logger-> info ("test daily info "); m_logger-> error ("test daily error ");

(5) flush the buffer into the file

When a specified level of log is encountered, the cache is immediately output to the file. If the log is not written immediately, when the program crashes or an exception occurs and exits, some important logs may not be written into the file. The following code shows the log levels:

typedef enum{    trace = 0,    debug = 1,    info = 2,    warn = 3,    err = 4,    critical = 5,    off = 6} level_enum;
// trigger flush if the log severity is error or higher                daily_logger->flush_on(spd::level::err);        daily_logger->info(123.44);        daily_logger->error("Error happended! ");


3. Call spdlog in MFC

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.