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/extensions/openpower-pels/meson.build b/extensions/openpower-pels/meson.build
index cff4e68..f075004 100644
--- a/extensions/openpower-pels/meson.build
+++ b/extensions/openpower-pels/meson.build
@@ -3,18 +3,10 @@
default_options: ['oem-ibm=enabled'],
)
-nlohmann_json_dep = dependency('nlohmann_json', include_type: 'system')
-
python_inst = import('python').find_installation('python3')
python_ver = python_inst.language_version()
python_dep = python_inst.dependency()
-if cpp.has_header('CLI/CLI.hpp')
- CLI11_dep = declare_dependency()
-else
- CLI11_dep = dependency('CLI11')
-endif
-
extra_sources = []
extra_dependencies = []
extra_args = []
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));
+}
diff --git a/meson.build b/meson.build
index ed2ea44..c4263c1 100644
--- a/meson.build
+++ b/meson.build
@@ -60,6 +60,15 @@
cereal_dep = cereal_proj.dependency('cereal')
endif
+# Get CLI11 dependency
+if cpp.has_header('CLI/CLI.hpp')
+ CLI11_dep = declare_dependency()
+else
+ CLI11_dep = dependency('CLI11')
+endif
+
+nlohmann_json_dep = dependency('nlohmann_json', include_type: 'system')
+
# Generate sdbus++ files.
generated_sources = []
generated_others = []
@@ -152,6 +161,19 @@
],
install: true,
)
+
+executable('log-create',
+ 'log_create_main.cpp',
+ dependencies: [
+ CLI11_dep,
+ nlohmann_json_dep,
+ pdi_dep,
+ phosphor_logging_dep,
+ sdbusplus_dep,
+ ],
+ install: true,
+)
+
# Generate test executables which run in obmc env (qemu, real hardware).
executable('logging-test',
'logging_test.cpp',