blob: 94869167411471df8fbc56d13d20e12b01092cd9 [file] [log] [blame]
Patrick Williamsb2a3aa22021-07-27 13:30:52 -05001#pragma once
2
3#if __cplusplus < 202002L
4#error "phosphor-logging lg2 requires C++20"
5#else
6
7#include <phosphor-logging/lg2/concepts.hpp>
8#include <phosphor-logging/lg2/conversion.hpp>
9#include <phosphor-logging/lg2/flags.hpp>
Patrick Williamscbdc2832021-08-02 16:31:04 -050010#include <phosphor-logging/lg2/header.hpp>
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050011#include <phosphor-logging/lg2/level.hpp>
Patrick Williamsa91a62b2021-08-28 14:17:55 -050012#include <phosphor-logging/lg2/source_location.hpp>
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050013
14namespace lg2
15{
16/** Implementation of the structured logging `lg2::log` interface. */
Patrick Williamsa91a62b2021-08-28 14:17:55 -050017template <level S = level::debug, details::any_but<lg2::source_location>... Ts>
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050018struct log
19{
20 /** log with a custom source_location.
21 *
22 * @param[in] s - The custom source location.
23 * @param[in] msg - The message to log.
24 * @param[in] ts - The rest of the arguments.
25 */
Patrick Williamsa91a62b2021-08-28 14:17:55 -050026 explicit log(const lg2::source_location& s, const char* msg,
Patrick Williamscbdc2832021-08-02 16:31:04 -050027 details::header_str_conversion_t<Ts&&>... ts)
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050028 {
Patrick Williamscbdc2832021-08-02 16:31:04 -050029 details::log_conversion::start(
30 S, s, msg,
31 std::forward<details::header_str_conversion_t<Ts&&>>(ts)...);
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050032 }
33
34 /** default log (source_location is determined by calling location).
35 *
36 * @param[in] msg - The message to log.
37 * @param[in] ts - The rest of the arguments.
38 * @param[in] s - The derived source_location.
39 */
40 explicit log(
Patrick Williamsb5988ff2021-08-26 11:58:36 -050041 const char* msg, details::header_str_conversion_t<Ts&&>... ts,
Patrick Williamsa91a62b2021-08-28 14:17:55 -050042 const lg2::source_location& s = lg2::source_location::current()) :
Patrick Williamscbdc2832021-08-02 16:31:04 -050043 log(s, msg, std::forward<details::header_str_conversion_t<Ts&&>>(ts)...)
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050044 {
45 }
46
47 // Give a nicer compile error if someone tries to log without a message.
48 log() = delete;
49};
50
51// Deducation guides to help the compiler out...
52
53template <level S = level::debug, typename... Ts>
Patrick Williamsb5988ff2021-08-26 11:58:36 -050054explicit log(const char*, Ts&&...) -> log<S, Ts...>;
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050055
56template <level S = level::debug, typename... Ts>
Patrick Williamsa91a62b2021-08-28 14:17:55 -050057explicit log(const lg2::source_location&, const char*, Ts&&...)
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050058 -> log<S, Ts...>;
59
60/** Macro to define aliases for lg2::level(...) -> lg2::log<level>(...)
61 *
62 * Creates a simple inherited structure and corresponding deduction guides.
63 */
64#define PHOSPHOR_LOG2_DECLARE_LEVEL(levelval) \
65 template <typename... Ts> \
66 struct levelval : public log<level::levelval, Ts...> \
67 { \
68 using log<level::levelval, Ts...>::log; \
69 }; \
70 \
71 template <typename... Ts> \
Patrick Williamsb5988ff2021-08-26 11:58:36 -050072 explicit levelval(const char*, Ts&&...) -> levelval<Ts...>; \
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050073 \
74 template <typename... Ts> \
Patrick Williamsa91a62b2021-08-28 14:17:55 -050075 explicit levelval(const lg2::source_location&, const char*, Ts&&...) \
Patrick Williamsb2a3aa22021-07-27 13:30:52 -050076 ->levelval<Ts...>
77
78// Enumerate the aliases for each log level.
79PHOSPHOR_LOG2_DECLARE_LEVEL(emergency);
80PHOSPHOR_LOG2_DECLARE_LEVEL(alert);
81PHOSPHOR_LOG2_DECLARE_LEVEL(critical);
82PHOSPHOR_LOG2_DECLARE_LEVEL(error);
83PHOSPHOR_LOG2_DECLARE_LEVEL(warning);
84PHOSPHOR_LOG2_DECLARE_LEVEL(notice);
85PHOSPHOR_LOG2_DECLARE_LEVEL(info);
86PHOSPHOR_LOG2_DECLARE_LEVEL(debug);
87
88#undef PHOSPHOR_LOG2_DECLARE_LEVEL
89
90/** Handy scope-level `using` to get the necessary bits of lg2. */
91#define PHOSPHOR_LOG2_USING \
92 using lg2::emergency; \
93 using lg2::alert; \
94 using lg2::critical; \
95 using lg2::error; \
96 using lg2::warning; \
97 using lg2::notice; \
98 using lg2::info; \
99 using lg2::debug
100// We purposefully left out `using lg2::log` above to avoid collisions with
101// the math function `log`. There is little use for direct calls to `lg2::log`,
102// when the level-aliases are available, since it is just a more awkward syntax.
103
104/** Scope-level `using` to get the everything, incluing format flags. */
105#define PHOSPHOR_LOG2_USING_WITH_FLAGS \
106 PHOSPHOR_LOG2_USING; \
107 PHOSPHOR_LOG2_USING_FLAGS
108
109} // namespace lg2
110
111#endif