1 | // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. |
2 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
3 | |
4 | #pragma once |
5 | #include <spdlog/cfg/helpers.h> |
6 | #include <spdlog/details/registry.h> |
7 | |
8 | // |
9 | // Init log levels using each argv entry that starts with "SPDLOG_LEVEL=" |
10 | // |
11 | // set all loggers to debug level: |
12 | // example.exe "SPDLOG_LEVEL=debug" |
13 | |
14 | // set logger1 to trace level |
15 | // example.exe "SPDLOG_LEVEL=logger1=trace" |
16 | |
17 | // turn off all logging except for logger1 and logger2: |
18 | // example.exe "SPDLOG_LEVEL=off,logger1=debug,logger2=info" |
19 | |
20 | namespace spdlog { |
21 | namespace cfg { |
22 | |
23 | // search for SPDLOG_LEVEL= in the args and use it to init the levels |
24 | inline void load_argv_levels(int argc, const char **argv) |
25 | { |
26 | const std::string spdlog_level_prefix = "SPDLOG_LEVEL=" ; |
27 | for (int i = 1; i < argc; i++) |
28 | { |
29 | std::string arg = argv[i]; |
30 | if (arg.find(spdlog_level_prefix) == 0) |
31 | { |
32 | auto levels_string = arg.substr(spdlog_level_prefix.size()); |
33 | helpers::load_levels(levels_string); |
34 | } |
35 | } |
36 | } |
37 | |
38 | inline void load_argv_levels(int argc, char **argv) |
39 | { |
40 | load_argv_levels(argc, const_cast<const char **>(argv)); |
41 | } |
42 | |
43 | } // namespace cfg |
44 | } // namespace spdlog |
45 | |