blob: 60dd30e8471cc7303c22312f9ce06b7024ad2754 [file] [log] [blame]
Patrick Williamsf0af3582024-10-10 16:32:32 -04001#include <sys/syslog.h>
2
3#include <nlohmann/json.hpp>
Patrick Williamse001cd72024-10-07 10:59:30 -04004#include <phosphor-logging/lg2/commit.hpp>
5#include <sdbusplus/async.hpp>
6#include <sdbusplus/exception.hpp>
Patrick Williamsf0af3582024-10-10 16:32:32 -04007#include <xyz/openbmc_project/Logging/Create/client.hpp>
8#include <xyz/openbmc_project/Logging/Entry/client.hpp>
Patrick Williamse001cd72024-10-07 10:59:30 -04009
10namespace lg2::details
11{
12
Patrick Williamsf0af3582024-10-10 16:32:32 -040013using Create = sdbusplus::client::xyz::openbmc_project::logging::Create<>;
14using Entry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>;
15
16/* Convert syslog severity to Entry::Level */
17static auto severity_from_syslog(int s) -> Entry::Level
Patrick Williamse001cd72024-10-07 10:59:30 -040018{
Patrick Williamsf0af3582024-10-10 16:32:32 -040019 switch (s)
20 {
21 case LOG_DEBUG:
22 return Entry::Level::Debug;
23
24 case LOG_INFO:
25 return Entry::Level::Informational;
26
27 case LOG_NOTICE:
28 return Entry::Level::Notice;
29
30 case LOG_WARNING:
31 return Entry::Level::Warning;
32
33 case LOG_ERR:
34 return Entry::Level::Error;
35
36 case LOG_CRIT:
37 return Entry::Level::Critical;
38
39 case LOG_ALERT:
40 return Entry::Level::Alert;
41
42 case LOG_EMERG:
43 return Entry::Level::Emergency;
44 }
45 return Entry::Level::Emergency;
Patrick Williamse001cd72024-10-07 10:59:30 -040046}
47
Patrick Williamsf0af3582024-10-10 16:32:32 -040048using AdditionalData_t = std::map<std::string, std::string>;
49
50/* Create AdditionalData from the sdbusplus event json. */
51static auto data_from_json(sdbusplus::exception::generated_event_base& t)
52 -> AdditionalData_t
53{
54 AdditionalData_t result{};
55
56 auto j = t.to_json()[t.name()];
57 for (const auto& item : j.items())
58 {
59 result.emplace(item.key(), item.value().dump());
60 }
61
62 return result;
63}
64
65auto commit(sdbusplus::exception::generated_event_base&& t)
66 -> sdbusplus::message::object_path
67{
68 auto b = sdbusplus::bus::new_default();
69 auto m = b.new_method_call(Create::default_service, Create::instance_path,
70 Create::interface, "Create");
71
72 m.append(t.name(), severity_from_syslog(t.severity()), data_from_json(t));
73
74 auto reply = b.call(m);
75
76 return reply.unpack<sdbusplus::message::object_path>();
77}
78
79auto commit(sdbusplus::async::context& ctx,
80 sdbusplus::exception::generated_event_base&& t)
Patrick Williamse001cd72024-10-07 10:59:30 -040081 -> sdbusplus::async::task<sdbusplus::message::object_path>
82{
Patrick Williamsf0af3582024-10-10 16:32:32 -040083 co_return co_await Create(ctx)
84 .service(Create::default_service)
85 .path(Create::instance_path)
86 .create(t.name(), severity_from_syslog(t.severity()),
87 data_from_json(t));
Patrick Williamse001cd72024-10-07 10:59:30 -040088}
89
90} // namespace lg2::details