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