helpers-inl.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifndef SPDLOG_HEADER_ONLY
  5. # include <spdlog/cfg/helpers.h>
  6. #endif
  7. #include <spdlog/spdlog.h>
  8. #include <spdlog/details/os.h>
  9. #include <spdlog/details/registry.h>
  10. #include <algorithm>
  11. #include <string>
  12. #include <utility>
  13. #include <sstream>
  14. namespace spdlog {
  15. namespace cfg {
  16. namespace helpers {
  17. // inplace convert to lowercase
  18. inline std::string &to_lower_(std::string &str)
  19. {
  20. std::transform(
  21. str.begin(), str.end(), str.begin(), [](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
  22. return str;
  23. }
  24. // inplace trim spaces
  25. inline std::string &trim_(std::string &str)
  26. {
  27. const char *spaces = " \n\r\t";
  28. str.erase(str.find_last_not_of(spaces) + 1);
  29. str.erase(0, str.find_first_not_of(spaces));
  30. return str;
  31. }
  32. // return (name,value) trimmed pair from given "name=value" string.
  33. // return empty string on missing parts
  34. // "key=val" => ("key", "val")
  35. // " key = val " => ("key", "val")
  36. // "key=" => ("key", "")
  37. // "val" => ("", "val")
  38. inline std::pair<std::string, std::string> extract_kv_(char sep, const std::string &str)
  39. {
  40. auto n = str.find(sep);
  41. std::string k, v;
  42. if (n == std::string::npos)
  43. {
  44. v = str;
  45. }
  46. else
  47. {
  48. k = str.substr(0, n);
  49. v = str.substr(n + 1);
  50. }
  51. return std::make_pair(trim_(k), trim_(v));
  52. }
  53. // return vector of key/value pairs from sequence of "K1=V1,K2=V2,.."
  54. // "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...}
  55. inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str)
  56. {
  57. std::string token;
  58. std::istringstream token_stream(str);
  59. std::unordered_map<std::string, std::string> rv{};
  60. while (std::getline(token_stream, token, ','))
  61. {
  62. if (token.empty())
  63. {
  64. continue;
  65. }
  66. auto kv = extract_kv_('=', token);
  67. rv[kv.first] = kv.second;
  68. }
  69. return rv;
  70. }
  71. SPDLOG_INLINE void load_levels(const std::string &input)
  72. {
  73. if (input.empty() || input.size() > 512)
  74. {
  75. return;
  76. }
  77. auto key_vals = extract_key_vals_(input);
  78. std::unordered_map<std::string, level::level_enum> levels;
  79. level::level_enum global_level = level::info;
  80. bool global_level_found = false;
  81. for (auto &name_level : key_vals)
  82. {
  83. auto &logger_name = name_level.first;
  84. auto level_name = to_lower_(name_level.second);
  85. auto level = level::from_str(level_name);
  86. // ignore unrecognized level names
  87. if (level == level::off && level_name != "off")
  88. {
  89. continue;
  90. }
  91. if (logger_name.empty()) // no logger name indicate global level
  92. {
  93. global_level_found = true;
  94. global_level = level;
  95. }
  96. else
  97. {
  98. levels[logger_name] = level;
  99. }
  100. }
  101. details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
  102. }
  103. } // namespace helpers
  104. } // namespace cfg
  105. } // namespace spdlog