blob: e69a7095b1c6f4e0ff4d6e1e3fff52d4fe6f4c61 [file] [log] [blame]
Brad Bishopc1283ae2017-05-20 21:42:38 -04001#pragma once
2
Brad Bishopc1283ae2017-05-20 21:42:38 -04003#include "callback.hpp"
4#include "format.hpp"
5
Patrick Venture3d6d3182018-08-31 09:33:09 -07006#include <phosphor-logging/log.hpp>
7
Brad Bishopc1283ae2017-05-20 21:42:38 -04008namespace phosphor
9{
10namespace dbus
11{
12namespace 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 */
22class JournalBase : public IndexedCallback
23{
Brad Bishopd1eac882018-03-29 10:34:05 -040024 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 }
Brad Bishopc1283ae2017-05-20 21:42:38 -040035
Brad Bishopd1eac882018-03-29 10:34:05 -040036 /** @brief Callback interface implementation. */
37 void operator()(Context ctx) override;
Brad Bishopc1283ae2017-05-20 21:42:38 -040038
Brad Bishopd1eac882018-03-29 10:34:05 -040039 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;
Brad Bishopc1283ae2017-05-20 21:42:38 -040044
Brad Bishopd1eac882018-03-29 10:34:05 -040045 /** @brief The client provided message to be traced. */
46 const char* message;
Brad Bishopc1283ae2017-05-20 21:42:38 -040047};
48
Brad Bishopec2ed2f2017-05-31 21:10:43 -040049/** @struct Display
50 * @brief Convert strings to const char*.
51 */
52namespace detail
53{
Patrick Venture3d6d3182018-08-31 09:33:09 -070054template <typename T>
55struct Display
Brad Bishopec2ed2f2017-05-31 21:10:43 -040056{
57 static auto op(T&& value)
58 {
59 return std::forward<T>(value);
60 }
61};
62
Patrick Venture3d6d3182018-08-31 09:33:09 -070063template <>
64struct Display<std::string>
Brad Bishopec2ed2f2017-05-31 21:10:43 -040065{
66 static auto op(const std::string& value)
67 {
68 return value.c_str();
69 }
70};
71} // namespace detail
72
Brad Bishopc1283ae2017-05-20 21:42:38 -040073/** @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 */
79template <typename T, phosphor::logging::level Severity>
80class Journal : public JournalBase
81{
Brad Bishopd1eac882018-03-29 10:34:05 -040082 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 }
Brad Bishopc1283ae2017-05-20 21:42:38 -040093
Brad Bishopd1eac882018-03-29 10:34:05 -040094 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 }
Brad Bishopc1283ae2017-05-20 21:42:38 -0400109};
110
111} // namespace monitoring
112} // namespace dbus
113} // namespace phosphor