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