Add PDBG callback function for logging with log level
Signed-off-by: austinfcui <austinfcui@gmail.com>
Change-Id: I8cac38dec0d2f48ef20d89c95e77f73079fd31d6
diff --git a/main_nl.cpp b/main_nl.cpp
index 227c6a6..d5a501f 100644
--- a/main_nl.cpp
+++ b/main_nl.cpp
@@ -8,6 +8,7 @@
#include <attn/attn_main.hpp>
#include <buildinfo.hpp>
#include <cli.hpp>
+#include <util/pdbg_callback.hpp>
/**
* @brief Attention handler application main()
@@ -37,6 +38,9 @@
}
else
{
+ // set PDBG log callback function.
+ pdbg_set_logfunc(util::pdbg_log_callback);
+
// Pdbg targets should only be initialized once according to
// libpdbg documentation. Initializing them here will make sure
// they are initialized for the attention handler, invocation of
diff --git a/util/pdbg_callback.hpp b/util/pdbg_callback.hpp
new file mode 100644
index 0000000..78b1ed9
--- /dev/null
+++ b/util/pdbg_callback.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <inttypes.h>
+#include <stdio.h>
+
+#include <phosphor-logging/log.hpp>
+
+#include <cstdarg>
+
+namespace util
+{
+
+/*
+ * @brief callback function for logging with log level
+ *
+ * @param loglevel PDBG log level
+ * @param format format of output function, same as in printf()
+ * @param args variable list
+ *
+ * @return none
+ */
+inline void pdbg_log_callback(const int loglevel, const char* format,
+ va_list args)
+{
+ constexpr size_t MSG_MAX_LEN = 256;
+ char msg[MSG_MAX_LEN];
+ vsnprintf(msg, MSG_MAX_LEN, format, args);
+
+ switch (loglevel)
+ {
+ case PDBG_ERROR:
+ phosphor::logging::log<phosphor::logging::level::ERR>(msg);
+ break;
+ case PDBG_WARNING:
+ phosphor::logging::log<phosphor::logging::level::WARNING>(msg);
+ break;
+ case PDBG_NOTICE:
+ phosphor::logging::log<phosphor::logging::level::NOTICE>(msg);
+ break;
+ case PDBG_INFO:
+ phosphor::logging::log<phosphor::logging::level::INFO>(msg);
+ break;
+ case PDBG_DEBUG:
+ phosphor::logging::log<phosphor::logging::level::DEBUG>(msg);
+ break;
+ default:
+ phosphor::logging::log<phosphor::logging::level::ERR>(msg);
+ break;
+ }
+}
+
+} // namespace util