blob: 2f1caab27ae0d75d5c4fd5ccf88ac501b2065a7f [file] [log] [blame]
Patrick Williams6eb96bf2024-11-05 14:59:52 -05001#include "config.h"
2
Patrick Williams9ca4d132024-10-31 17:02:47 -04003#include "lg2_commit.hpp"
4
Patrick Williamsf0af3582024-10-10 16:32:32 -04005#include <sys/syslog.h>
6
7#include <nlohmann/json.hpp>
Patrick Williamsfc148672024-11-06 21:19:43 -05008#include <phosphor-logging/commit.hpp>
Patrick Williams6eb96bf2024-11-05 14:59:52 -05009#include <phosphor-logging/lg2.hpp>
Patrick Williamse001cd72024-10-07 10:59:30 -040010#include <sdbusplus/async.hpp>
11#include <sdbusplus/exception.hpp>
Patrick Williamsf0af3582024-10-10 16:32:32 -040012#include <xyz/openbmc_project/Logging/Create/client.hpp>
13#include <xyz/openbmc_project/Logging/Entry/client.hpp>
Patrick Williamse001cd72024-10-07 10:59:30 -040014
Patrick Williamsfc148672024-11-06 21:19:43 -050015namespace lg2
16{
17namespace details
Patrick Williamse001cd72024-10-07 10:59:30 -040018{
19
Patrick Williamsf0af3582024-10-10 16:32:32 -040020using Create = sdbusplus::client::xyz::openbmc_project::logging::Create<>;
21using Entry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>;
22
23/* Convert syslog severity to Entry::Level */
24static auto severity_from_syslog(int s) -> Entry::Level
Patrick Williamse001cd72024-10-07 10:59:30 -040025{
Patrick Williamsf0af3582024-10-10 16:32:32 -040026 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 Williamse001cd72024-10-07 10:59:30 -040053}
54
Patrick Williamsf0af3582024-10-10 16:32:32 -040055using AdditionalData_t = std::map<std::string, std::string>;
56
57/* Create AdditionalData from the sdbusplus event json. */
58static 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 Williams247fed62024-10-31 15:12:02 -040066 // 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 Williamsdd9694a2024-11-21 10:58:24 -050096 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 Williamsf0af3582024-10-10 16:32:32 -0400104 }
105
106 return result;
107}
108
Patrick Williams9ca4d132024-10-31 17:02:47 -0400109auto extractEvent(sdbusplus::exception::generated_event_base&& t)
110 -> std::tuple<std::string, Entry::Level, std::vector<std::string>>
111{
112 auto data = data_from_json(t);
113 std::vector<std::string> additional_data = {};
114
115 for (auto& [key, data] : data)
116 {
117 additional_data.emplace_back(key + "=" + data);
118 }
119
120 return {t.name(), severity_from_syslog(t.severity()),
121 std::move(additional_data)};
122}
123
Patrick Williamsfc148672024-11-06 21:19:43 -0500124} // namespace details
125
126auto commit(sdbusplus::exception::generated_event_base&& t)
127 -> sdbusplus::message::object_path
128{
129 if constexpr (LG2_COMMIT_JOURNAL)
130 {
131 lg2::error("OPENBMC_MESSAGE_ID={DATA}", "DATA", t.to_json().dump());
132 }
133
134 if constexpr (LG2_COMMIT_DBUS)
135 {
136 using details::Create;
137
138 auto b = sdbusplus::bus::new_default();
139 auto m =
140 b.new_method_call(Create::default_service, Create::instance_path,
141 Create::interface, "Create");
142
143 m.append(t.name(), details::severity_from_syslog(t.severity()),
144 details::data_from_json(t));
145
146 auto reply = b.call(m);
147
148 return reply.unpack<sdbusplus::message::object_path>();
149 }
150
151 return {};
152}
153
154auto commit(sdbusplus::async::context& ctx,
155 sdbusplus::exception::generated_event_base&& t)
156 -> sdbusplus::async::task<sdbusplus::message::object_path>
157{
158 using details::Create;
159
160 if constexpr (LG2_COMMIT_JOURNAL)
161 {
162 lg2::error("OPENBMC_MESSAGE_ID={DATA}", "DATA", t.to_json().dump());
163 }
164
165 if constexpr (LG2_COMMIT_DBUS)
166 {
167 co_return co_await Create(ctx)
168 .service(Create::default_service)
169 .path(Create::instance_path)
170 .create(t.name(), details::severity_from_syslog(t.severity()),
171 details::data_from_json(t));
172 }
173 co_return {};
174}
175
176} // namespace lg2