blob: cc3b09e9ee104b2fb2b67f53d6fc63680e737a4c [file] [log] [blame]
Patrick Williamsdc35e302024-11-05 23:35:02 -05001#include <CLI/CLI.hpp>
2#include <nlohmann/json.hpp>
3#include <phosphor-logging/commit.hpp>
4#include <sdbusplus/exception.hpp>
5
6// We don't actually use the Logging events, but we need to include the
7// header in order to force linking against the PDI library.
8#include <xyz/openbmc_project/Logging/event.hpp>
9
10#include <iostream>
11#include <string>
12
13void list_all()
14{
15 std::cout << "Known events:" << std::endl;
16 for (const auto& e : sdbusplus::exception::known_events())
17 {
18 std::cout << " " << e << std::endl;
19 }
20}
21
22int generate_event(const std::string& eventId, const nlohmann::json& data)
23{
24 if (eventId.empty())
25 {
26 std::cerr << "event required" << std::endl;
27 return 1;
28 }
29
30 nlohmann::json j = {{eventId, data}};
31
32 try
33 {
34 sdbusplus::exception::throw_via_json(j);
35 }
36 catch (sdbusplus::exception::generated_event_base& e)
37 {
Patrick Williamsfc148672024-11-06 21:19:43 -050038 auto path = lg2::commit(std::move(e));
Patrick Williamsdc35e302024-11-05 23:35:02 -050039 std::cout << path.str << std::endl;
40 return 0;
41 }
42
43 std::cerr << "Unknown event: " << eventId << std::endl;
44 return 1;
45}
46
47int main(int argc, char** argv)
48{
49 CLI::App app{"log-create"};
50
51 std::string jsonStr;
52 app.add_option("-j,--json", jsonStr, "Event data as a JSON object")
53 ->default_val("{}");
54
55 std::string event{};
56 auto event_option = app.add_option("event", event, "Event name");
57
Alexander Hansen3f766af2024-11-22 15:00:46 +010058 bool listOnly = false;
Patrick Williamsdc35e302024-11-05 23:35:02 -050059 app.add_flag("-l,--list", listOnly, "List all events")
60 ->excludes(event_option);
61
62 CLI11_PARSE(app, argc, argv);
63
64 if (listOnly)
65 {
66 list_all();
67 return 0;
68 }
69
70 return generate_event(event, nlohmann::json::parse(jsonStr));
71}