blob: 249a7a2d162ef013dc1bc5475c633df7d7905f1a [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{
Brad Bishopd1eac882018-03-29 10:34:05 -040023 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 }
Brad Bishopc1283ae2017-05-20 21:42:38 -040034
Brad Bishopd1eac882018-03-29 10:34:05 -040035 /** @brief Callback interface implementation. */
36 void operator()(Context ctx) override;
Brad Bishopc1283ae2017-05-20 21:42:38 -040037
Brad Bishopd1eac882018-03-29 10:34:05 -040038 private:
39 /** @brief Delegate type specific calls to subclasses. */
40 virtual void log(const char* message, const std::string& pathMeta,
41 const std::string& path, const std::string& propertyMeta,
42 const any_ns::any& value) const = 0;
Brad Bishopc1283ae2017-05-20 21:42:38 -040043
Brad Bishopd1eac882018-03-29 10:34:05 -040044 /** @brief The client provided message to be traced. */
45 const char* message;
Brad Bishopc1283ae2017-05-20 21:42:38 -040046};
47
Brad Bishopec2ed2f2017-05-31 21:10:43 -040048/** @struct Display
49 * @brief Convert strings to const char*.
50 */
51namespace detail
52{
53template <typename T> struct Display
54{
55 static auto op(T&& value)
56 {
57 return std::forward<T>(value);
58 }
59};
60
61template <> struct Display<std::string>
62{
63 static auto op(const std::string& value)
64 {
65 return value.c_str();
66 }
67};
68} // namespace detail
69
Brad Bishopc1283ae2017-05-20 21:42:38 -040070/** @class Journal
71 * @brief C++ type specific logic for the journal callback.
72 *
73 * @tparam T - The C++ type of the property values being traced.
74 * @tparam Severity - The log severity of the log entry.
75 */
76template <typename T, phosphor::logging::level Severity>
77class Journal : public JournalBase
78{
Brad Bishopd1eac882018-03-29 10:34:05 -040079 public:
80 Journal() = delete;
81 Journal(const Journal&) = delete;
82 Journal(Journal&&) = default;
83 Journal& operator=(const Journal&) = delete;
84 Journal& operator=(Journal&&) = default;
85 ~Journal() = default;
86 Journal(const char* msg, const PropertyIndex& index) :
87 JournalBase(msg, index)
88 {
89 }
Brad Bishopc1283ae2017-05-20 21:42:38 -040090
Brad Bishopd1eac882018-03-29 10:34:05 -040091 private:
92 /** @brief log interface implementation. */
93 void log(const char* message, const std::string& pathMeta,
94 const std::string& path, const std::string& propertyMeta,
95 const any_ns::any& value) const override
96 {
97 phosphor::logging::log<Severity>(
98 message,
99 phosphor::logging::entry(
100 (pathMeta + GetFormat<decltype(pathMeta)>::format).c_str(),
101 path.c_str()),
102 phosphor::logging::entry(
103 (propertyMeta + GetFormat<T>::format).c_str(),
104 detail::Display<T>::op(any_ns::any_cast<T>(value))));
105 }
Brad Bishopc1283ae2017-05-20 21:42:38 -0400106};
107
108} // namespace monitoring
109} // namespace dbus
110} // namespace phosphor