blob: 29d01d1ea2775708909f737a090aa8a947cdfb9d [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>
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05007#include <string>
Patrick Venture3d6d3182018-08-31 09:33:09 -07008
Brad Bishopc1283ae2017-05-20 21:42:38 -04009namespace phosphor
10{
11namespace dbus
12{
13namespace 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 */
23class JournalBase : public IndexedCallback
24{
Brad Bishopd1eac882018-03-29 10:34:05 -040025 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 }
Brad Bishopc1283ae2017-05-20 21:42:38 -040036
Brad Bishopd1eac882018-03-29 10:34:05 -040037 /** @brief Callback interface implementation. */
38 void operator()(Context ctx) override;
Brad Bishopc1283ae2017-05-20 21:42:38 -040039
Brad Bishopd1eac882018-03-29 10:34:05 -040040 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 any_ns::any& value) const = 0;
Brad Bishopc1283ae2017-05-20 21:42:38 -040045
Brad Bishopd1eac882018-03-29 10:34:05 -040046 /** @brief The client provided message to be traced. */
47 const char* message;
Brad Bishopc1283ae2017-05-20 21:42:38 -040048};
49
Brad Bishopec2ed2f2017-05-31 21:10:43 -040050/** @struct Display
51 * @brief Convert strings to const char*.
52 */
53namespace detail
54{
Patrick Venture3d6d3182018-08-31 09:33:09 -070055template <typename T>
56struct Display
Brad Bishopec2ed2f2017-05-31 21:10:43 -040057{
58 static auto op(T&& value)
59 {
60 return std::forward<T>(value);
61 }
62};
63
Patrick Venture3d6d3182018-08-31 09:33:09 -070064template <>
65struct Display<std::string>
Brad Bishopec2ed2f2017-05-31 21:10:43 -040066{
67 static auto op(const std::string& value)
68 {
69 return value.c_str();
70 }
71};
72} // namespace detail
73
Brad Bishopc1283ae2017-05-20 21:42:38 -040074/** @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 */
80template <typename T, phosphor::logging::level Severity>
81class Journal : public JournalBase
82{
Brad Bishopd1eac882018-03-29 10:34:05 -040083 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 }
Brad Bishopc1283ae2017-05-20 21:42:38 -040094
Brad Bishopd1eac882018-03-29 10:34:05 -040095 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 any_ns::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(any_ns::any_cast<T>(value))));
109 }
Brad Bishopc1283ae2017-05-20 21:42:38 -0400110};
111
112} // namespace monitoring
113} // namespace dbus
114} // namespace phosphor