1 #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 
list_all()13 void 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 
generate_event(const std::string & eventId,const nlohmann::json & data)22 int 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     {
38         auto path = lg2::commit(std::move(e));
39         std::cout << path.str << std::endl;
40         return 0;
41     }
42 
43     std::cerr << "Unknown event: " << eventId << std::endl;
44     return 1;
45 }
46 
main(int argc,char ** argv)47 int 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 
58     bool listOnly;
59     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 }
72