blob: 6f11a4c6cbde089753b4f0c2f1f6f76546bbff33 [file] [log] [blame]
Patrick Williams9ca4d132024-10-31 17:02:47 -04001#include "lg2_commit.hpp"
2
Patrick Williamsf0af3582024-10-10 16:32:32 -04003#include <sys/syslog.h>
4
5#include <nlohmann/json.hpp>
Patrick Williamse001cd72024-10-07 10:59:30 -04006#include <phosphor-logging/lg2/commit.hpp>
7#include <sdbusplus/async.hpp>
8#include <sdbusplus/exception.hpp>
Patrick Williamsf0af3582024-10-10 16:32:32 -04009#include <xyz/openbmc_project/Logging/Create/client.hpp>
10#include <xyz/openbmc_project/Logging/Entry/client.hpp>
Patrick Williamse001cd72024-10-07 10:59:30 -040011
12namespace lg2::details
13{
14
Patrick Williamsf0af3582024-10-10 16:32:32 -040015using Create = sdbusplus::client::xyz::openbmc_project::logging::Create<>;
16using Entry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>;
17
18/* Convert syslog severity to Entry::Level */
19static auto severity_from_syslog(int s) -> Entry::Level
Patrick Williamse001cd72024-10-07 10:59:30 -040020{
Patrick Williamsf0af3582024-10-10 16:32:32 -040021 switch (s)
22 {
23 case LOG_DEBUG:
24 return Entry::Level::Debug;
25
26 case LOG_INFO:
27 return Entry::Level::Informational;
28
29 case LOG_NOTICE:
30 return Entry::Level::Notice;
31
32 case LOG_WARNING:
33 return Entry::Level::Warning;
34
35 case LOG_ERR:
36 return Entry::Level::Error;
37
38 case LOG_CRIT:
39 return Entry::Level::Critical;
40
41 case LOG_ALERT:
42 return Entry::Level::Alert;
43
44 case LOG_EMERG:
45 return Entry::Level::Emergency;
46 }
47 return Entry::Level::Emergency;
Patrick Williamse001cd72024-10-07 10:59:30 -040048}
49
Patrick Williamsf0af3582024-10-10 16:32:32 -040050using AdditionalData_t = std::map<std::string, std::string>;
51
52/* Create AdditionalData from the sdbusplus event json. */
53static auto data_from_json(sdbusplus::exception::generated_event_base& t)
54 -> AdditionalData_t
55{
56 AdditionalData_t result{};
57
58 auto j = t.to_json()[t.name()];
59 for (const auto& item : j.items())
60 {
Patrick Williams247fed62024-10-31 15:12:02 -040061 // Special cases for the "_SOURCE" fields, which contain debug
62 // information about the origin of the event.
63 if (item.key() == "_SOURCE")
64 {
65 for (const auto& source_item : item.value().items())
66 {
67 if (source_item.key() == "PID")
68 {
69 result.emplace("_PID", source_item.value().dump());
70 continue;
71 }
72 if (source_item.key() == "FILE")
73 {
74 result.emplace("_CODE_FILE", source_item.value());
75 continue;
76 }
77 if (source_item.key() == "FUNCTION")
78 {
79 result.emplace("_CODE_FUNC", source_item.value());
80 continue;
81 }
82 if (source_item.key() == "LINE")
83 {
84 result.emplace("_CODE_LINE", source_item.value().dump());
85 continue;
86 }
87 }
88 continue;
89 }
90
Patrick Williamsf0af3582024-10-10 16:32:32 -040091 result.emplace(item.key(), item.value().dump());
92 }
93
94 return result;
95}
96
97auto commit(sdbusplus::exception::generated_event_base&& t)
98 -> sdbusplus::message::object_path
99{
100 auto b = sdbusplus::bus::new_default();
101 auto m = b.new_method_call(Create::default_service, Create::instance_path,
102 Create::interface, "Create");
103
104 m.append(t.name(), severity_from_syslog(t.severity()), data_from_json(t));
105
106 auto reply = b.call(m);
107
108 return reply.unpack<sdbusplus::message::object_path>();
109}
110
111auto commit(sdbusplus::async::context& ctx,
112 sdbusplus::exception::generated_event_base&& t)
Patrick Williamse001cd72024-10-07 10:59:30 -0400113 -> sdbusplus::async::task<sdbusplus::message::object_path>
114{
Patrick Williamsf0af3582024-10-10 16:32:32 -0400115 co_return co_await Create(ctx)
116 .service(Create::default_service)
117 .path(Create::instance_path)
118 .create(t.name(), severity_from_syslog(t.severity()),
119 data_from_json(t));
Patrick Williamse001cd72024-10-07 10:59:30 -0400120}
121
Patrick Williams9ca4d132024-10-31 17:02:47 -0400122auto extractEvent(sdbusplus::exception::generated_event_base&& t)
123 -> std::tuple<std::string, Entry::Level, std::vector<std::string>>
124{
125 auto data = data_from_json(t);
126 std::vector<std::string> additional_data = {};
127
128 for (auto& [key, data] : data)
129 {
130 additional_data.emplace_back(key + "=" + data);
131 }
132
133 return {t.name(), severity_from_syslog(t.severity()),
134 std::move(additional_data)};
135}
136
Patrick Williamse001cd72024-10-07 10:59:30 -0400137} // namespace lg2::details