-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathlog_create_main.cpp
More file actions
82 lines (67 loc) · 2.2 KB
/
log_create_main.cpp
File metadata and controls
82 lines (67 loc) · 2.2 KB
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
72
73
74
75
76
77
78
79
80
81
82
#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,
std::optional<int> severity)
{
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), severity);
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::map<std::string, int> syslogMap = {
{"DEBUG", LOG_DEBUG}, {"INFO", LOG_INFO}, {"NOTICE", LOG_NOTICE},
{"WARNING", LOG_WARNING}, {"ERR", LOG_ERR}, {"CRIT", LOG_CRIT},
{"ALERT", LOG_ALERT}, {"EMERG", LOG_EMERG}};
std::string jsonStr;
app.add_option("-j,--json", jsonStr, "Event data as a JSON object")
->default_val("{}");
std::optional<int> severity;
app.add_option(
"-s,--severity", severity,
"Syslog severity (Overrides the one specified by the message registry")
->transform(CLI::CheckedTransformer(syslogMap, CLI::ignore_case));
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), severity);
}