LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
example_logit_mdc_ndc.cpp
Go to the documentation of this file.
1#include <logit.hpp>
2
3#include <thread>
4
5int main() {
6#ifndef LOGIT_WITH_CONTEXT
8 LOGIT_WARN("MDC/NDC example requires LOGIT_WITH_CONTEXT=ON");
10 return 0;
11#else
13 "[%T] [%l] request=%K{request_id} user=%K{user_id} ndc=[%J] %v",
14 false);
15
16 LOGIT_MDC_PUT("request_id", "req-42");
17 LOGIT_MDC_PUT("user_id", "alice");
18 LOGIT_NDC_PUSH("http");
19 LOGIT_NDC_PUSH("POST /checkout");
20
21 LOGIT_INFO("request accepted");
22
23 {
24 LOGIT_NDC_GUARD("payment");
25 LOGIT_WARN("payment provider latency is high");
26 }
27
28 LOGIT_INFO("payment scope has ended");
29
30 std::thread worker([]() {
31 LOGIT_MDC_PUT("request_id", "worker-7");
32 LOGIT_MDC_PUT("user_id", "background");
33 LOGIT_NDC_PUSH("worker");
34 LOGIT_INFO("background task has its own thread-local context");
37 });
38 worker.join();
39
40 LOGIT_INFO("main thread context is unchanged");
41
42 LOGIT_MDC_REMOVE("user_id");
44 LOGIT_INFO("specific MDC keys can be removed and NDC can be popped");
45
49 return 0;
50#endif
51}
int main()
#define LOGIT_NDC_CLEAR()
#define LOGIT_MDC_PUT(key, value)
#define LOGIT_NDC_PUSH(value)
#define LOGIT_NDC_GUARD(value)
#define LOGIT_NDC_POP()
#define LOGIT_MDC_CLEAR()
#define LOGIT_MDC_REMOVE(key)
#define LOGIT_ADD_CONSOLE_DEFAULT()
Macro for adding the default console logger.
#define LOGIT_WAIT()
Macro for waiting for all asynchronous loggers to finish processing.
#define LOGIT_INFO(...)
#define LOGIT_SHUTDOWN()
Macro for shutting down logger system.
#define LOGIT_WARN(...)
#define LOGIT_ADD_CONSOLE(pattern, async)
Macro for adding a console logger with a specific pattern and mode.
Unified umbrella header for the LogIt++ library.