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