Using spdlog in a C++ project
Tree structure of the project.
The code of main.cc.
The code of test.cc.
Attention:
Spdlog print the info level log by default. If you want to print the debug level log, you need to set the spdlog default level. As follows:
The code of main.cc.
#include <iostream>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
void do_work_in_another_file();
void rotating_example()
{
try
{
// Create a file rotating logger with 5 MB size max and 3 rotated files
auto max_size = 1048576 * 5;
auto max_files = 3;
// Set output time pattern
// %f: Time accurate to microseconds
// %n: Output mode "some_logger_name"
// %l: Prints the severity level of the log message. Example include info, warn, error, debug or critical.
// %v: Prints the actual text or payload of the log message that you passed to the function.
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%n] [%l] %v");
// Set it as default so you can spdlog::info() directly.
auto logger = spdlog::rotating_logger_mt("some_logger_name",
"logs/rotating.txt", max_size, max_files);
logger->info("This is a log message that will trigger rotation once the file is full.");
} catch (const spdlog::spdlog_ex& ex){
std::cerr << "Log initialization failed:" << ex.what() << std::endl;
}
}
int main(void)
{
rotating_example();
do_work_in_another_file();
return 0;
}
The code of test.cc.
#include "spdlog/spdlog.h"
void do_work_in_another_file()
{
// Get the logger by its registered name
auto logger = spdlog::get("some_logger_name");
if (logger) {
logger->info("This message come from another file!");
}
}
The output log:Attention:
Spdlog print the info level log by default. If you want to print the debug level log, you need to set the spdlog default level. As follows:
spdlog::set_level(spdlog::level::debug);
Multiple log file must set in the main.cc or spdlog will show error message, as following:$ cmake --build .
[1/2] Building CXX object CMakeFiles/my_app.dir/sma.cc.o
FAILED: [code=1] CMakeFiles/my_app.dir/sma.cc.o
/ucrt64/bin/g++.exe -I/d/cc/ex1/third_party/spdlog/include -std=gnu++11 -MD -MT CMakeFiles/my_app.dir/sma.cc.o -MF CMakeFiles/my_app.dir/sma.cc.o.d -o CMakeFiles/my_app.dir/sma.cc.o -c /d/cc/ex1/sma.cc
D:/cc/ex1/sma.cc: In function 'double Sma(const std::vector&, int)':
D:/cc/ex1/sma.cc:30:9: error: 'log_sma' was not declared in this scope
30 | log_sma->info("The size of prices={} less than period={}!",
Eliminate errors, you should declare all log files in main.cc:void rotating_example()
{
try
{
// Create a file rotating logger with 5 MB size max and 3 rotated files
auto max_size = 1048576 * 5; // 5M
auto max_files = 8;
// Set output time pattern
// %f: Time accurate to microseconds
// %n: Output mode "some_logger_name"
// %l: Prints the severity level of the log message. Example include info, warn, error, debug or critical.
// %v: Prints the actual text or payload of the log message that you passed to the function.
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%n] [%l] %v");
// Set it as default so you can spdlog::info() directly.
auto logger = spdlog::rotating_logger_mt("some_logger_name",
"logs/rotating.txt", max_size, max_files);
auto log_sma = spdlog::rotating_logger_mt("SMA",
"logs/sma_log.txt", max_size, max_files);
logger->info("This is a log message that will trigger rotation once the file is full.");
} catch (const spdlog::spdlog_ex& ex){
std::cerr << "Log initialization failed:" << ex.what() << std::endl;
}
}
Getting "SMA" logger in sma.cc:
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include <numeric>
#include <iostream>
// Function to calculate Simple Moving Average
double Sma(const std::vector &prices, int period){
auto logger = spdlog::get("SMA");
if (logger) {
logger->info("Getting logger succeed!");
}
if (prices.size() > period) {
logger->info("The size of prices={} less than period={}!",
prices.size(), period);
return 0.0;
}
// Sum the last 'period' elements
double sum = std::accumulate(prices.end() - period, prices.end(),0);
return sum / period;
}
Comments
Post a Comment