Patrick Williams | 6eb96bf | 2024-11-05 14:59:52 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Patrick Williams | 9ca4d13 | 2024-10-31 17:02:47 -0400 | [diff] [blame] | 3 | #include "lg2_commit.hpp" |
| 4 | |
Patrick Williams | f0af358 | 2024-10-10 16:32:32 -0400 | [diff] [blame] | 5 | #include <sys/syslog.h> |
| 6 | |
| 7 | #include <nlohmann/json.hpp> |
Patrick Williams | fc14867 | 2024-11-06 21:19:43 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/commit.hpp> |
Patrick Williams | 6eb96bf | 2024-11-05 14:59:52 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
Patrick Williams | e001cd7 | 2024-10-07 10:59:30 -0400 | [diff] [blame] | 10 | #include <sdbusplus/async.hpp> |
| 11 | #include <sdbusplus/exception.hpp> |
Patrick Williams | f0af358 | 2024-10-10 16:32:32 -0400 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Logging/Create/client.hpp> |
| 13 | #include <xyz/openbmc_project/Logging/Entry/client.hpp> |
Patrick Williams | e001cd7 | 2024-10-07 10:59:30 -0400 | [diff] [blame] | 14 | |
Patrick Williams | fc14867 | 2024-11-06 21:19:43 -0500 | [diff] [blame] | 15 | namespace lg2 |
| 16 | { |
| 17 | namespace details |
Patrick Williams | e001cd7 | 2024-10-07 10:59:30 -0400 | [diff] [blame] | 18 | { |
| 19 | |
Patrick Williams | f0af358 | 2024-10-10 16:32:32 -0400 | [diff] [blame] | 20 | using Create = sdbusplus::client::xyz::openbmc_project::logging::Create<>; |
| 21 | using Entry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>; |
| 22 | |
| 23 | /* Convert syslog severity to Entry::Level */ |
| 24 | static auto severity_from_syslog(int s) -> Entry::Level |
Patrick Williams | e001cd7 | 2024-10-07 10:59:30 -0400 | [diff] [blame] | 25 | { |
Patrick Williams | f0af358 | 2024-10-10 16:32:32 -0400 | [diff] [blame] | 26 | switch (s) |
| 27 | { |
| 28 | case LOG_DEBUG: |
| 29 | return Entry::Level::Debug; |
| 30 | |
| 31 | case LOG_INFO: |
| 32 | return Entry::Level::Informational; |
| 33 | |
| 34 | case LOG_NOTICE: |
| 35 | return Entry::Level::Notice; |
| 36 | |
| 37 | case LOG_WARNING: |
| 38 | return Entry::Level::Warning; |
| 39 | |
| 40 | case LOG_ERR: |
| 41 | return Entry::Level::Error; |
| 42 | |
| 43 | case LOG_CRIT: |
| 44 | return Entry::Level::Critical; |
| 45 | |
| 46 | case LOG_ALERT: |
| 47 | return Entry::Level::Alert; |
| 48 | |
| 49 | case LOG_EMERG: |
| 50 | return Entry::Level::Emergency; |
| 51 | } |
| 52 | return Entry::Level::Emergency; |
Patrick Williams | e001cd7 | 2024-10-07 10:59:30 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Patrick Williams | f0af358 | 2024-10-10 16:32:32 -0400 | [diff] [blame] | 55 | using AdditionalData_t = std::map<std::string, std::string>; |
| 56 | |
| 57 | /* Create AdditionalData from the sdbusplus event json. */ |
| 58 | static auto data_from_json(sdbusplus::exception::generated_event_base& t) |
| 59 | -> AdditionalData_t |
| 60 | { |
| 61 | AdditionalData_t result{}; |
| 62 | |
| 63 | auto j = t.to_json()[t.name()]; |
| 64 | for (const auto& item : j.items()) |
| 65 | { |
Patrick Williams | 247fed6 | 2024-10-31 15:12:02 -0400 | [diff] [blame] | 66 | // Special cases for the "_SOURCE" fields, which contain debug |
| 67 | // information about the origin of the event. |
| 68 | if (item.key() == "_SOURCE") |
| 69 | { |
| 70 | for (const auto& source_item : item.value().items()) |
| 71 | { |
| 72 | if (source_item.key() == "PID") |
| 73 | { |
| 74 | result.emplace("_PID", source_item.value().dump()); |
| 75 | continue; |
| 76 | } |
| 77 | if (source_item.key() == "FILE") |
| 78 | { |
| 79 | result.emplace("_CODE_FILE", source_item.value()); |
| 80 | continue; |
| 81 | } |
| 82 | if (source_item.key() == "FUNCTION") |
| 83 | { |
| 84 | result.emplace("_CODE_FUNC", source_item.value()); |
| 85 | continue; |
| 86 | } |
| 87 | if (source_item.key() == "LINE") |
| 88 | { |
| 89 | result.emplace("_CODE_LINE", source_item.value().dump()); |
| 90 | continue; |
| 91 | } |
| 92 | } |
| 93 | continue; |
| 94 | } |
| 95 | |
Patrick Williams | dd9694a | 2024-11-21 10:58:24 -0500 | [diff] [blame] | 96 | if (item.value().type() == nlohmann::json::value_t::string) |
| 97 | { |
| 98 | result.emplace(item.key(), item.value()); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | result.emplace(item.key(), item.value().dump()); |
| 103 | } |
Patrick Williams | f0af358 | 2024-10-10 16:32:32 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
Patrick Williams | 9ca4d13 | 2024-10-31 17:02:47 -0400 | [diff] [blame] | 109 | auto extractEvent(sdbusplus::exception::generated_event_base&& t) |
Patrick Williams | ea21d99 | 2024-11-22 17:06:35 -0500 | [diff] [blame^] | 110 | -> std::tuple<std::string, Entry::Level, std::map<std::string, std::string>> |
Patrick Williams | 9ca4d13 | 2024-10-31 17:02:47 -0400 | [diff] [blame] | 111 | { |
Patrick Williams | ea21d99 | 2024-11-22 17:06:35 -0500 | [diff] [blame^] | 112 | return {t.name(), severity_from_syslog(t.severity()), data_from_json(t)}; |
Patrick Williams | 9ca4d13 | 2024-10-31 17:02:47 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Patrick Williams | fc14867 | 2024-11-06 21:19:43 -0500 | [diff] [blame] | 115 | } // namespace details |
| 116 | |
| 117 | auto commit(sdbusplus::exception::generated_event_base&& t) |
| 118 | -> sdbusplus::message::object_path |
| 119 | { |
| 120 | if constexpr (LG2_COMMIT_JOURNAL) |
| 121 | { |
| 122 | lg2::error("OPENBMC_MESSAGE_ID={DATA}", "DATA", t.to_json().dump()); |
| 123 | } |
| 124 | |
| 125 | if constexpr (LG2_COMMIT_DBUS) |
| 126 | { |
| 127 | using details::Create; |
| 128 | |
| 129 | auto b = sdbusplus::bus::new_default(); |
| 130 | auto m = |
| 131 | b.new_method_call(Create::default_service, Create::instance_path, |
| 132 | Create::interface, "Create"); |
| 133 | |
| 134 | m.append(t.name(), details::severity_from_syslog(t.severity()), |
| 135 | details::data_from_json(t)); |
| 136 | |
| 137 | auto reply = b.call(m); |
| 138 | |
| 139 | return reply.unpack<sdbusplus::message::object_path>(); |
| 140 | } |
| 141 | |
| 142 | return {}; |
| 143 | } |
| 144 | |
| 145 | auto commit(sdbusplus::async::context& ctx, |
| 146 | sdbusplus::exception::generated_event_base&& t) |
| 147 | -> sdbusplus::async::task<sdbusplus::message::object_path> |
| 148 | { |
| 149 | using details::Create; |
| 150 | |
| 151 | if constexpr (LG2_COMMIT_JOURNAL) |
| 152 | { |
| 153 | lg2::error("OPENBMC_MESSAGE_ID={DATA}", "DATA", t.to_json().dump()); |
| 154 | } |
| 155 | |
| 156 | if constexpr (LG2_COMMIT_DBUS) |
| 157 | { |
| 158 | co_return co_await Create(ctx) |
| 159 | .service(Create::default_service) |
| 160 | .path(Create::instance_path) |
| 161 | .create(t.name(), details::severity_from_syslog(t.severity()), |
| 162 | details::data_from_json(t)); |
| 163 | } |
| 164 | co_return {}; |
| 165 | } |
| 166 | |
| 167 | } // namespace lg2 |