Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-01 09:06:34

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 #include <map>
0007 #include <string>
0008 
0009 #include <spdlog/common.h>
0010 
0011 // MDC is a simple map of key->string values stored in thread local storage whose content will be printed by the loggers.
0012 // Note: Not supported in async mode (thread local storage - so the async thread pool have different copy).
0013 //
0014 // Usage example:
0015 // spdlog::mdc::put("mdc_key_1", "mdc_value_1");
0016 // spdlog::info("Hello, {}", "World!");  // => [2024-04-26 02:08:05.040] [info] [mdc_key_1:mdc_value_1] Hello, World!
0017 
0018 namespace spdlog {
0019 class SPDLOG_API mdc {
0020 public:
0021     using mdc_map_t = std::map<std::string, std::string>;
0022 
0023     static void put(const std::string &key, const std::string &value) {
0024         get_context()[key] = value;
0025     }
0026 
0027     static std::string get(const std::string &key) {
0028         auto &context = get_context();
0029         auto it = context.find(key);
0030         if (it != context.end()) {
0031             return it->second;
0032         }
0033         return "";
0034     }
0035 
0036     static void remove(const std::string &key) { get_context().erase(key); }
0037 
0038     static void clear() { get_context().clear(); }
0039 
0040     static mdc_map_t &get_context() {
0041         static thread_local mdc_map_t context;
0042         return context;
0043     }
0044 };
0045 
0046 }  // namespace spdlog