xref: /openbmc/phosphor-dbus-monitor/src/journal.hpp (revision 223c40931e4f413431ab2f84d92c5f404e2d9cf2)
1 #pragma once
2 
3 #include "callback.hpp"
4 #include "format.hpp"
5 
6 #include <phosphor-logging/log.hpp>
7 
8 namespace phosphor
9 {
10 namespace dbus
11 {
12 namespace monitoring
13 {
14 
15 /** @class JournalBase
16  *  @brief Journal callback implementation.
17  *
18  *  The journal callback logs the client message and
19  *  journal metadata key value pairs as specified by the
20  *  client supplied property index.
21  */
22 class JournalBase : public IndexedCallback
23 {
24   public:
25     JournalBase() = delete;
26     JournalBase(const JournalBase&) = delete;
27     JournalBase(JournalBase&&) = default;
28     JournalBase& operator=(const JournalBase&) = delete;
29     JournalBase& operator=(JournalBase&&) = default;
30     virtual ~JournalBase() = default;
31     JournalBase(const char* msg, const PropertyIndex& index) :
32         IndexedCallback(index), message(msg)
33     {
34     }
35 
36     /** @brief Callback interface implementation. */
37     void operator()(Context ctx) override;
38 
39   private:
40     /** @brief Delegate type specific calls to subclasses. */
41     virtual void log(const char* message, const std::string& pathMeta,
42                      const std::string& path, const std::string& propertyMeta,
43                      const any_ns::any& value) const = 0;
44 
45     /** @brief The client provided message to be traced.  */
46     const char* message;
47 };
48 
49 /** @struct Display
50  *  @brief Convert strings to const char*.
51  */
52 namespace detail
53 {
54 template <typename T>
55 struct Display
56 {
57     static auto op(T&& value)
58     {
59         return std::forward<T>(value);
60     }
61 };
62 
63 template <>
64 struct Display<std::string>
65 {
66     static auto op(const std::string& value)
67     {
68         return value.c_str();
69     }
70 };
71 } // namespace detail
72 
73 /** @class Journal
74  *  @brief C++ type specific logic for the journal callback.
75  *
76  *  @tparam T - The C++ type of the property values being traced.
77  *  @tparam Severity - The log severity of the log entry.
78  */
79 template <typename T, phosphor::logging::level Severity>
80 class Journal : public JournalBase
81 {
82   public:
83     Journal() = delete;
84     Journal(const Journal&) = delete;
85     Journal(Journal&&) = default;
86     Journal& operator=(const Journal&) = delete;
87     Journal& operator=(Journal&&) = default;
88     ~Journal() = default;
89     Journal(const char* msg, const PropertyIndex& index) :
90         JournalBase(msg, index)
91     {
92     }
93 
94   private:
95     /** @brief log interface implementation. */
96     void log(const char* message, const std::string& pathMeta,
97              const std::string& path, const std::string& propertyMeta,
98              const any_ns::any& value) const override
99     {
100         phosphor::logging::log<Severity>(
101             message,
102             phosphor::logging::entry(
103                 (pathMeta + GetFormat<decltype(pathMeta)>::format).c_str(),
104                 path.c_str()),
105             phosphor::logging::entry(
106                 (propertyMeta + GetFormat<T>::format).c_str(),
107                 detail::Display<T>::op(any_ns::any_cast<T>(value))));
108     }
109 };
110 
111 } // namespace monitoring
112 } // namespace dbus
113 } // namespace phosphor
114