diff --git a/example/example.cpp b/example/example.cpp index 46a641d2..9bb34fdf 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -10,6 +10,8 @@ void stdout_logger_example(); void basic_example(); void rotating_example(); void daily_example(); +void env_cfg_example(); +void argv_cfg_example(int, char*[]); void async_example(); void binary_example(); void trace_example(); @@ -19,17 +21,9 @@ void err_handler_example(); void syslog_example(); #include "spdlog/spdlog.h" -#include "spdlog/cfg/argv.h" -#include "spdlog/cfg/env.h" -int main(int args, char *argv []) +int main(int args, char *argv[]) { - // optionally init log levels from argv - spdlog::cfg::load_argv(args, argv); - - // optionally init load levels from an environment variable (SPDLOG_LEVEL) - spdlog::cfg::load_env(); - spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); 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); @@ -37,15 +31,12 @@ int main(int args, char *argv []) spdlog::info("Positional args are {1} {0}..", "too", "supported"); spdlog::info("{:>8} aligned, {:<8} aligned", "right", "left"); - // Runtime log levels spdlog::set_level(spdlog::level::info); // Set global log level to info spdlog::debug("This message should not be displayed!"); spdlog::set_level(spdlog::level::trace); // Set specific logger's log level spdlog::debug("This message should be displayed.."); - - // Customize msg format for all loggers spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); spdlog::info("This an info message with custom format"); @@ -76,6 +67,12 @@ int main(int args, char *argv []) err_handler_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. // note: registered loggers *must* be thread safe for this to work correctly! spdlog::flush_every(std::chrono::seconds(3)); @@ -83,6 +80,7 @@ int main(int args, char *argv []) // Apply some function on all registered loggers spdlog::apply_all([&](std::shared_ptr l) { l->info("End of example."); }); + // Release all spdlog resources, and drop all loggers in the registry. // This is optional (only mandatory if using windows + async log). spdlog::shutdown(); @@ -127,6 +125,19 @@ void daily_example() 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" void async_example() {