blob: c9c2d8b8d05dd212ca4aebad6b865f121a3f8e9d [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>
George Liu3fe976c2022-06-21 09:37:04 +08007
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05008#include <string>
Patrick Venture3d6d3182018-08-31 09:33:09 -07009
Brad Bishopc1283ae2017-05-20 21:42:38 -040010namespace phosphor
11{
12namespace dbus
13{
14namespace monitoring
15{
16
17/** @class JournalBase
18 * @brief Journal callback implementation.
19 *
20 * The journal callback logs the client message and
21 * journal metadata key value pairs as specified by the
22 * client supplied property index.
23 */
24class JournalBase : public IndexedCallback
25{
Brad Bishopd1eac882018-03-29 10:34:05 -040026 public:
27 JournalBase() = delete;
28 JournalBase(const JournalBase&) = delete;
29 JournalBase(JournalBase&&) = default;
30 JournalBase& operator=(const JournalBase&) = delete;
31 JournalBase& operator=(JournalBase&&) = default;
32 virtual ~JournalBase() = default;
33 JournalBase(const char* msg, const PropertyIndex& index) :
34 IndexedCallback(index), message(msg)
George Liu3fe976c2022-06-21 09:37:04 +080035 {}
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,
Patrick Williams26dc0bc2022-06-16 17:06:18 -050044 const std::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)
George Liu3fe976c2022-06-21 09:37:04 +080092 {}
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,
Patrick Williams26dc0bc2022-06-16 17:06:18 -050098 const std::any& value) const override
Brad Bishopd1eac882018-03-29 10:34:05 -040099 {
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(),
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500107 detail::Display<T>::op(std::any_cast<T>(value))));
Brad Bishopd1eac882018-03-29 10:34:05 -0400108 }
Brad Bishopc1283ae2017-05-20 21:42:38 -0400109};
110
111} // namespace monitoring
112} // namespace dbus
113} // namespace phosphor