argv.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. {
  20. const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
  21. for (int i = 1; i < argc; i++)
  22. {
  23. std::string arg = argv[i];
  24. if (arg.find(spdlog_level_prefix) == 0)
  25. {
  26. auto levels_string = arg.substr(spdlog_level_prefix.size());
  27. helpers::load_levels(levels_string);
  28. }
  29. }
  30. }
  31. inline void load_argv_levels(int argc, char **argv)
  32. {
  33. load_argv_levels(argc, const_cast<const char **>(argv));
  34. }
  35. } // namespace cfg
  36. } // namespace spdlog