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> |
Andrew Geissler | ae4c95c | 2020-05-16 13:58:53 -0500 | [diff] [blame] | 7 | #include <string> |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 8 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 9 | namespace phosphor |
| 10 | { |
| 11 | namespace dbus |
| 12 | { |
| 13 | namespace 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 | */ |
| 23 | class JournalBase : public IndexedCallback |
| 24 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 25 | 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 Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 36 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 37 | /** @brief Callback interface implementation. */ |
| 38 | void operator()(Context ctx) override; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 39 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 40 | 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 Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame^] | 44 | const std::any& value) const = 0; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 45 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 46 | /** @brief The client provided message to be traced. */ |
| 47 | const char* message; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 48 | }; |
| 49 | |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 50 | /** @struct Display |
| 51 | * @brief Convert strings to const char*. |
| 52 | */ |
| 53 | namespace detail |
| 54 | { |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 55 | template <typename T> |
| 56 | struct Display |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 57 | { |
| 58 | static auto op(T&& value) |
| 59 | { |
| 60 | return std::forward<T>(value); |
| 61 | } |
| 62 | }; |
| 63 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 64 | template <> |
| 65 | struct Display<std::string> |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 66 | { |
| 67 | static auto op(const std::string& value) |
| 68 | { |
| 69 | return value.c_str(); |
| 70 | } |
| 71 | }; |
| 72 | } // namespace detail |
| 73 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 74 | /** @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 | */ |
| 80 | template <typename T, phosphor::logging::level Severity> |
| 81 | class Journal : public JournalBase |
| 82 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 83 | 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 Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 94 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 95 | 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, |
Patrick Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame^] | 99 | const std::any& value) const override |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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(), |
Patrick Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame^] | 108 | detail::Display<T>::op(std::any_cast<T>(value)))); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 109 | } |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace monitoring |
| 113 | } // namespace dbus |
| 114 | } // namespace phosphor |