1 #pragma once
2 
3 #include "callback.hpp"
4 #include "format.hpp"
5 
6 #include <phosphor-logging/log.hpp>
7 #include <string>
8 
9 namespace phosphor
10 {
11 namespace dbus
12 {
13 namespace monitoring
14 {
15 
16 /** @class JournalBase
17  *  @brief Journal callback implementation.
18  *
19  *  The journal callback logs the client message and
20  *  journal metadata key value pairs as specified by the
21  *  client supplied property index.
22  */
23 class JournalBase : public IndexedCallback
24 {
25   public:
26     JournalBase() = delete;
27     JournalBase(const JournalBase&) = delete;
28     JournalBase(JournalBase&&) = default;
29     JournalBase& operator=(const JournalBase&) = delete;
30     JournalBase& operator=(JournalBase&&) = default;
31     virtual ~JournalBase() = default;
32     JournalBase(const char* msg, const PropertyIndex& index) :
33         IndexedCallback(index), message(msg)
34     {
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 {
58     static auto op(T&& value)
59     {
60         return std::forward<T>(value);
61     }
62 };
63 
64 template <>
65 struct Display<std::string>
66 {
67     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;
90     Journal(const char* msg, const PropertyIndex& index) :
91         JournalBase(msg, index)
92     {
93     }
94 
95   private:
96     /** @brief log interface implementation. */
97     void log(const char* message, const std::string& pathMeta,
98              const std::string& path, const std::string& propertyMeta,
99              const std::any& value) const override
100     {
101         phosphor::logging::log<Severity>(
102             message,
103             phosphor::logging::entry(
104                 (pathMeta + GetFormat<decltype(pathMeta)>::format).c_str(),
105                 path.c_str()),
106             phosphor::logging::entry(
107                 (propertyMeta + GetFormat<T>::format).c_str(),
108                 detail::Display<T>::op(std::any_cast<T>(value))));
109     }
110 };
111 
112 } // namespace monitoring
113 } // namespace dbus
114 } // namespace phosphor
115