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