sdbus++: events: add source location and pid
Add the source code location and process ID to the generated
events, which are useful for debug.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Iefaab6becc13292e1cd7835d97c6b3cc61a52776
diff --git a/tools/sdbusplus/templates/event.cpp.mako b/tools/sdbusplus/templates/event.cpp.mako
index b956fa4..7613e85 100644
--- a/tools/sdbusplus/templates/event.cpp.mako
+++ b/tools/sdbusplus/templates/event.cpp.mako
@@ -24,5 +24,15 @@
% for m in event.metadata:
j["${m.SNAKE_CASE}"] = ${m.camelCase};
% endfor
+
+ // Add common source and pid info.
+ nlohmann::json source_info = {};
+ source_info["FILE"] = source.file_name();
+ source_info["LINE"] = source.line();
+ source_info["COLUMN"] = source.column();
+ source_info["FUNCTION"] = source.function_name();
+ source_info["PID"] = pid;
+ j["_SOURCE"] = source_info;
+
return nlohmann::json{ { errName, std::move(j) } };
}
diff --git a/tools/sdbusplus/templates/event.hpp.mako b/tools/sdbusplus/templates/event.hpp.mako
index 1ae8d9d..382f67c 100644
--- a/tools/sdbusplus/templates/event.hpp.mako
+++ b/tools/sdbusplus/templates/event.hpp.mako
@@ -1,14 +1,20 @@
struct ${event.CamelCase} final :
public sdbusplus::exception::generated_event<${event.CamelCase}>
{
-%if len(event.metadata) > 0:
- ${event.CamelCase}() = delete;
+%if len(event.metadata) == 0:
+ ${event.CamelCase}(
+ std::source_location source = std::source_location::current()) :
+ source(source), pid(getpid())
+ {}
+%else:
${event.CamelCase}(
${", ".join([
f"metadata_t<\"{m.SNAKE_CASE}\">, {m.cppTypeParam(events.name)} {m.camelCase}_"
- for m in event.metadata ])}) :
+ for m in event.metadata ])},
+ std::source_location source = std::source_location::current()) :
${", ".join([
- f"{m.camelCase}({m.camelCase}_)" for m in event.metadata ])}
+ f"{m.camelCase}({m.camelCase}_)" for m in event.metadata ])},
+ source(source), pid(getpid())
{}
%for m in event.metadata:
@@ -20,6 +26,9 @@
int set_error(SdBusInterface*, sd_bus_error*) const override;
auto to_json() const -> nlohmann::json override;
+ std::source_location source;
+ pid_t pid;
+
static constexpr auto errName =
"${events.name}.${event.name}";
static constexpr auto errDesc =
diff --git a/tools/sdbusplus/templates/events.cpp.mako b/tools/sdbusplus/templates/events.cpp.mako
index 91eb5c5..40105be 100644
--- a/tools/sdbusplus/templates/events.cpp.mako
+++ b/tools/sdbusplus/templates/events.cpp.mako
@@ -1,6 +1,6 @@
#include <${events.headerFile("event")}>
#include <nlohmann/json.hpp>
-
+#
%if events.errors:
namespace sdbusplus::error::${events.cppNamespacedClass()}
diff --git a/tools/sdbusplus/templates/events.hpp.mako b/tools/sdbusplus/templates/events.hpp.mako
index 97f10c9..bb9eb85 100644
--- a/tools/sdbusplus/templates/events.hpp.mako
+++ b/tools/sdbusplus/templates/events.hpp.mako
@@ -2,10 +2,13 @@
* Version: ${events.version}
*/
#pragma once
+#include <sys/syslog.h>
+#include <unistd.h>
+
#include <sdbusplus/exception.hpp>
#include <cerrno>
-#include <sys/syslog.h>
+#include <source_location>
% for h in events.cpp_includes():
#include <${h}>