helpers-inl.h 3.1 KB

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