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