Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-07 08:53:14

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #if defined(SPDLOG_NO_TLS)
0007     #error "This header requires thread local storage support, but SPDLOG_NO_TLS is defined."
0008 #endif
0009 
0010 #include <map>
0011 #include <string>
0012 
0013 #include <spdlog/common.h>
0014 
0015 // MDC is a simple map of key->string values stored in thread local storage whose content will be
0016 // printed by the loggers. Note: Not supported in async mode (thread local storage - so the async
0017 // thread pool have different copy).
0018 //
0019 // Usage example:
0020 // spdlog::mdc::put("mdc_key_1", "mdc_value_1");
0021 // spdlog::info("Hello, {}", "World!");  // => [2024-04-26 02:08:05.040] [info]
0022 // [mdc_key_1:mdc_value_1] Hello, World!
0023 
0024 namespace spdlog {
0025 class SPDLOG_API mdc {
0026 public:
0027     using mdc_map_t = std::map<std::string, std::string>;
0028 
0029     static void put(const std::string &key, const std::string &value) {
0030         get_context()[key] = value;
0031     }
0032 
0033     static std::string get(const std::string &key) {
0034         auto &context = get_context();
0035         auto it = context.find(key);
0036         if (it != context.end()) {
0037             return it->second;
0038         }
0039         return "";
0040     }
0041 
0042     static void remove(const std::string &key) { get_context().erase(key); }
0043 
0044     static void clear() { get_context().clear(); }
0045 
0046     static mdc_map_t &get_context() {
0047         static thread_local mdc_map_t context;
0048         return context;
0049     }
0050 };
0051 
0052 }  // namespace spdlog