-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathlog_create_main.cpp
More file actions
109 lines (92 loc) · 2.98 KB
/
Copy pathlog_create_main.cpp
File metadata and controls
109 lines (92 loc) · 2.98 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "log_create_extensions.hpp"
#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>
#include <utility>
#include <vector>
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,
const std::vector<std::pair<std::string, std::string>>& extends)
{
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)
{
for (const auto& [interface, payload] : extends)
{
extendLogEvent(e, interface, nlohmann::json::parse(payload));
}
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");
std::vector<std::pair<std::string, std::string>> extends;
app.add_option(
"--extend", extends,
"Extend the event with data for an extension interface. Takes an "
"interface name and a JSON object. May be repeated.")
->type_size(2)
->allow_extra_args(false);
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;
}
try
{
return generate_event(event, nlohmann::json::parse(jsonStr), severity,
extends);
}
catch (std::exception& e)
{
std::cerr << "Unable to create event: " << e.what() << std::endl;
return 1;
}
}