argv.h 1.2 KB

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