Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 3 | #include "callback.hpp" |
| 4 | #include "format.hpp" |
| 5 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
| 7 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace dbus |
| 11 | { |
| 12 | namespace monitoring |
| 13 | { |
| 14 | |
| 15 | /** @class JournalBase |
| 16 | * @brief Journal callback implementation. |
| 17 | * |
| 18 | * The journal callback logs the client message and |
| 19 | * journal metadata key value pairs as specified by the |
| 20 | * client supplied property index. |
| 21 | */ |
| 22 | class JournalBase : public IndexedCallback |
| 23 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 24 | public: |
| 25 | JournalBase() = delete; |
| 26 | JournalBase(const JournalBase&) = delete; |
| 27 | JournalBase(JournalBase&&) = default; |
| 28 | JournalBase& operator=(const JournalBase&) = delete; |
| 29 | JournalBase& operator=(JournalBase&&) = default; |
| 30 | virtual ~JournalBase() = default; |
| 31 | JournalBase(const char* msg, const PropertyIndex& index) : |
| 32 | IndexedCallback(index), message(msg) |
| 33 | { |
| 34 | } |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 35 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 36 | /** @brief Callback interface implementation. */ |
| 37 | void operator()(Context ctx) override; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 38 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 39 | private: |
| 40 | /** @brief Delegate type specific calls to subclasses. */ |
| 41 | virtual void log(const char* message, const std::string& pathMeta, |
| 42 | const std::string& path, const std::string& propertyMeta, |
| 43 | const any_ns::any& value) const = 0; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 44 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 45 | /** @brief The client provided message to be traced. */ |
| 46 | const char* message; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 47 | }; |
| 48 | |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 49 | /** @struct Display |
| 50 | * @brief Convert strings to const char*. |
| 51 | */ |
| 52 | namespace detail |
| 53 | { |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 54 | template <typename T> |
| 55 | struct Display |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 56 | { |
| 57 | static auto op(T&& value) |
| 58 | { |
| 59 | return std::forward<T>(value); |
| 60 | } |
| 61 | }; |
| 62 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 63 | template <> |
| 64 | struct Display<std::string> |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 65 | { |
| 66 | static auto op(const std::string& value) |
| 67 | { |
| 68 | return value.c_str(); |
| 69 | } |
| 70 | }; |
| 71 | } // namespace detail |
| 72 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 73 | /** @class Journal |
| 74 | * @brief C++ type specific logic for the journal callback. |
| 75 | * |
| 76 | * @tparam T - The C++ type of the property values being traced. |
| 77 | * @tparam Severity - The log severity of the log entry. |
| 78 | */ |
| 79 | template <typename T, phosphor::logging::level Severity> |
| 80 | class Journal : public JournalBase |
| 81 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 82 | public: |
| 83 | Journal() = delete; |
| 84 | Journal(const Journal&) = delete; |
| 85 | Journal(Journal&&) = default; |
| 86 | Journal& operator=(const Journal&) = delete; |
| 87 | Journal& operator=(Journal&&) = default; |
| 88 | ~Journal() = default; |
| 89 | Journal(const char* msg, const PropertyIndex& index) : |
| 90 | JournalBase(msg, index) |
| 91 | { |
| 92 | } |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 93 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 94 | 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, |
| 98 | const any_ns::any& value) const override |
| 99 | { |
| 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(), |
| 107 | detail::Display<T>::op(any_ns::any_cast<T>(value)))); |
| 108 | } |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace monitoring |
| 112 | } // namespace dbus |
| 113 | } // namespace phosphor |