Updated example

This commit is contained in:
gabime 2020-02-26 19:21:36 +02:00
parent e696978d11
commit c861e2d9cf

View File

@ -10,8 +10,6 @@ void stdout_logger_example();
void basic_example(); void basic_example();
void rotating_example(); void rotating_example();
void daily_example(); void daily_example();
void env_cfg_example();
void argv_cfg_example(int, char*[]);
void async_example(); void async_example();
void binary_example(); void binary_example();
void trace_example(); void trace_example();
@ -21,10 +19,21 @@ void err_handler_example();
void syslog_example(); void syslog_example();
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "spdlog/cfg/env.h" // for loading levels from the environment variables
int main(int args, char *argv[]) int main(int, char *[])
{ {
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
// Optionally load log levels from the SPDLOG_LEVEL env variable or from argv.
// For example: set the global level to info and mylogger to to trace:
// SPDLOG_LEVEL=info,mylogger=trace && ./example
spdlog::cfg::load_env();
// or from command line: "./example SPDLOG_LEVEL=info,mylogger=trace"
// #include "spdlog/cfg/argv.h" // for loading levels from argv
// spdlog::cfg::load_argv(args, argv);
spdlog::warn("Easy padding in numbers like {:08d}", 12); spdlog::warn("Easy padding in numbers like {:08d}", 12);
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
spdlog::info("Support for floats {:03.2f}", 1.23456); spdlog::info("Support for floats {:03.2f}", 1.23456);
@ -67,12 +76,6 @@ int main(int args, char *argv[])
err_handler_example(); err_handler_example();
trace_example(); trace_example();
// Init levels from SPDLOG_LEVEL env variable
env_cfg_example();
// Init levels from args (example.exe "SPDLOG_LEVEL=trace,l2=info,l3=off")
argv_cfg_example(args, argv);
// Flush all *registered* loggers using a worker thread every 3 seconds. // Flush all *registered* loggers using a worker thread every 3 seconds.
// note: registered loggers *must* be thread safe for this to work correctly! // note: registered loggers *must* be thread safe for this to work correctly!
spdlog::flush_every(std::chrono::seconds(3)); spdlog::flush_every(std::chrono::seconds(3));
@ -80,7 +83,6 @@ int main(int args, char *argv[])
// Apply some function on all registered loggers // Apply some function on all registered loggers
spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); }); spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); });
// Release all spdlog resources, and drop all loggers in the registry. // Release all spdlog resources, and drop all loggers in the registry.
// This is optional (only mandatory if using windows + async log). // This is optional (only mandatory if using windows + async log).
spdlog::shutdown(); spdlog::shutdown();
@ -125,19 +127,6 @@ void daily_example()
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
} }
#include "spdlog/cfg/env.h"
void env_cfg_example()
{
// set levels from SPDLOG_LEVEL env variable
spdlog::cfg::load_env();
}
#include "spdlog/cfg/argv.h"
void argv_cfg_example(int args, char *argv[])
{
spdlog::cfg::load_argv(args, argv);
}
#include "spdlog/async.h" #include "spdlog/async.h"
void async_example() void async_example()
{ {