log-create: add CLI utility to create events
Add utility to allow creation of events from the command line and
shell scripts. The utility ensures that the correct data arguments
are passed, or else fails the creation.
Tested:
```
$ ./builddir/log-create xyz.openbmc_project.Sensor.Threshold.SensorFailure --json '{ "SENSOR_NAME": "Example-Sensor" }'
<3> OPENBMC_MESSAGE_ID={"xyz.openbmc_project.Sensor.Threshold.SensorFailure":{"SENSOR_NAME":"Example-Sensor","_SOURCE":{"COLUMN":45,"FILE":"../log_create_main.cpp","FUNCTION":"int generate_event(const std::string&, const nlohmann::json_abi_v3_11_2::json&)","LINE":34,"PID":264326}}}
/xyz/openbmc_project/logging/entry/1
$ busctl --user introspect xyz.openbmc_project.Logging /xyz/openbmc_project/logging/entry/1 -l | cat
xyz.openbmc_project.Logging.Entry interface - - -
.AdditionalData property as 5 "SENSOR_NAME=\"Example-Sensor\"" "_CODE_FILE=../log_create_main.cpp" "_CODE_FUNC=int generate_event(const std::string&, const nlohmann::json_abi_v3_11_2::json&)" "_CODE_LINE=34" "_PID=264326" emits-change writable
.Id property u 1 emits-change writable
.Message property s "xyz.openbmc_project.Sensor.Threshold.SensorFailure" emits-change writable
.Severity property s "xyz.openbmc_project.Logging.Entry.Level.Critical" emits-change writable
$ ./builddir/log-create xyz.openbmc_project.Sensor.Threshold.SensorFailure
terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::out_of_range'
what(): [json.exception.out_of_range.403] key 'SENSOR_NAME' not found
$ ./builddir/log-create xyz.openbmc_project.Invalid.Event.Name
Unknown event: xyz.openbmc_project.Invalid.Event.Name
```
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I357b453f8fecc9d224aa412ad7f3cc6b8c2a4ad8
diff --git a/log_create_main.cpp b/log_create_main.cpp
new file mode 100644
index 0000000..4347152
--- /dev/null
+++ b/log_create_main.cpp
@@ -0,0 +1,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::details::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;
+ 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));
+}