blob: d9209aff413c86988fc8f71f9d386acf3ee1516f [file] [log] [blame]
Brad Bishopc1283ae2017-05-20 21:42:38 -04001#pragma once
2
3#include <phosphor-logging/log.hpp>
4#include "callback.hpp"
5#include "format.hpp"
6
7namespace phosphor
8{
9namespace dbus
10{
11namespace 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 */
21class 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. */
Ratan Guptaa45e0862018-02-21 19:03:13 +053034 void operator()(Context ctx) override;
Brad Bishopc1283ae2017-05-20 21:42:38 -040035
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
Brad Bishopec2ed2f2017-05-31 21:10:43 -040049/** @struct Display
50 * @brief Convert strings to const char*.
51 */
52namespace detail
53{
54template <typename T> struct Display
55{
56 static auto op(T&& value)
57 {
58 return std::forward<T>(value);
59 }
60};
61
62template <> 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
Brad Bishopc1283ae2017-05-20 21:42:38 -040071/** @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 */
77template <typename T, phosphor::logging::level Severity>
78class 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(
Brad Bishopec2ed2f2017-05-31 21:10:43 -0400102 (pathMeta + GetFormat<decltype(pathMeta)>::format).c_str(),
103 path.c_str()),
Brad Bishopc1283ae2017-05-20 21:42:38 -0400104 phosphor::logging::entry(
Brad Bishopec2ed2f2017-05-31 21:10:43 -0400105 (propertyMeta + GetFormat<T>::format).c_str(),
106 detail::Display<T>::op(any_ns::any_cast<T>(value))));
Brad Bishopc1283ae2017-05-20 21:42:38 -0400107 }
108};
109
110} // namespace monitoring
111} // namespace dbus
112} // namespace phosphor