blob: 4e476709a8d9b00ab7c5de7b71b5271787f8365b [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. */
34 void operator()() override;
35
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
49/** @class Journal
50 * @brief C++ type specific logic for the journal callback.
51 *
52 * @tparam T - The C++ type of the property values being traced.
53 * @tparam Severity - The log severity of the log entry.
54 */
55template <typename T, phosphor::logging::level Severity>
56class Journal : public JournalBase
57{
58 public:
59 Journal() = delete;
60 Journal(const Journal&) = delete;
61 Journal(Journal&&) = default;
62 Journal& operator=(const Journal&) = delete;
63 Journal& operator=(Journal&&) = default;
64 ~Journal() = default;
65 Journal(const char* msg, const PropertyIndex& index) :
66 JournalBase(msg, index) {}
67
68 private:
69 /** @brief log interface implementation. */
70 void log(
71 const char* message,
72 const std::string& pathMeta,
73 const std::string& path,
74 const std::string& propertyMeta,
75 const any_ns::any& value) const override
76 {
77 phosphor::logging::log<Severity>(
78 message,
79 phosphor::logging::entry(
80 pathMeta + GetFormat<decltype(pathMeta)>::format,
81 path),
82 phosphor::logging::entry(
83 propertyMeta + GetFormat<T>::format,
84 any_ns::any_cast<T>(value)));
85 }
86};
87
88} // namespace monitoring
89} // namespace dbus
90} // namespace phosphor