Implementing Phosphor-Logging/LG2 logging
This commit introduces changes in the phosphor-debug-collector
repository to implement structured logging using the LG2
framework. The existing log calls in the repository,
have been replaced with LG2 logging, facilitates better
log tracking and troubleshooting by offering improved
detail in JSON object values.
Test:
- Created BMC dump
- Created system dump
Here is a example
{
"_EXE" : "/tmp/phosphor-dump-manager",
"_MACHINE_ID" : "f9ca96d99c7b4ba688556f632ffeff5d",
"_CAP_EFFECTIVE" : "1ffffffffff",
"LOG2_FMTMSG" : "Invalid Dump file name, FILENAME: {FILENAME}",
"__CURSOR" : "s=721ae7a5b6ed43ec904f2bb03e3c0403;i=2a8d0;\
b=b372b9a5989e46bb8e62b33310e181ea;m=2c05fda2b;t=5fdf0684c1e70;x=1592f60584d6486c",
"CODE_FUNC" : "void phosphor::dump::bmc::Manager::\
createEntry(const std::filesystem::__cxx11::path&)",
"_SYSTEMD_SLICE" : "system-dropbear.slice",
"CODE_LINE" : "174",
"__REALTIME_TIMESTAMP" : "1686583867350640",
"PRIORITY" : "3",
"_SYSTEMD_UNIT" : "dropbear@1-9.3.29.238:22-9.3.84.138:45432.service",
"_PID" : "16209",
"_BOOT_ID" : "b372b9a5989e46bb8e62b33310e181ea",
"_SOURCE_REALTIME_TIMESTAMP" : "1686583867350580",
"_TRANSPORT" : "journal",
"_HOSTNAME" : "openbmc",
"SYSLOG_IDENTIFIER" : "phosphor-dump-manager",
"MESSAGE" : "Invalid Dump file name, \
FILENAME: BMCDUMP.XXXXXXX.0000006.20230612153010",
"CODE_FILE" : \
"/usr/src/debug/phosphor-debug-collector/1.0+gitAUTOINC+a17f1c92ce-r1\
/dump_manager_bmc.cpp",
"_UID" : "0",
"_CMDLINE" : "/tmp/phosphor-dump-manager",
"_RUNTIME_SCOPE" : "system",
"FILENAME" : "BMCDUMP.XXXXXXXX.0000006.20230612153010",
"_SYSTEMD_INVOCATION_ID" : "64a11629aade4c96ab62154cbc4be8b7",
"__MONOTONIC_TIMESTAMP" : "11817441835",
"_SYSTEMD_CGROUP" : "/system.slice/system-dropbear.slice/\
dropbear@1-9.3.29.238:22-9.3.84.138:45432.service",
"_COMM" : "phosphor-dump-m",
"_GID" : "0"
}
Change-Id: I3a52b812b059b64d945493de2e2cc68a43f6d72a
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/dump_offload.cpp b/dump_offload.cpp
index c590614..bb647e5 100644
--- a/dump_offload.cpp
+++ b/dump_offload.cpp
@@ -2,7 +2,6 @@
#include "dump_offload.hpp"
-#include <fmt/core.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
@@ -11,6 +10,7 @@
#include <dump_utils.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Common/File/error.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
@@ -55,10 +55,8 @@
NULL, &timeVal);
if (retVal <= 0)
{
- log<level::ERR>(
- fmt::format("writeOnUnixSocket: select() failed, errno({})",
- errno)
- .c_str());
+ lg2::error("writeOnUnixSocket: select() failed, errno: {ERRNO}",
+ "ERRNO", errno);
std::string msg = "select() failed " + std::string(strerror(errno));
throw std::runtime_error(msg);
}
@@ -72,10 +70,8 @@
numOfBytesWrote = 0;
continue;
}
- log<level::ERR>(
- fmt::format("writeOnUnixSocket: write() failed, errno({})",
- errno)
- .c_str());
+ lg2::error("writeOnUnixSocket: write() failed, errno: {ERRNO}",
+ "ERRNO", errno);
std::string msg = "write() on socket failed " +
std::string(strerror(errno));
throw std::runtime_error(msg);
@@ -101,7 +97,7 @@
if (strnlen(sockPath.c_str(), sizeof(socketAddr.sun_path)) ==
sizeof(socketAddr.sun_path))
{
- log<level::ERR>("UNIX socket path too long");
+ lg2::error("UNIX socket path too long");
std::string msg = "UNIX socket path is too long " +
std::string(strerror(errno));
throw std::length_error(msg);
@@ -110,26 +106,23 @@
sizeof(socketAddr.sun_path) - 1);
if ((unixSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1)
{
- log<level::ERR>(
- fmt::format("socketInit: socket() failed, errno({})", errno)
- .c_str());
+ lg2::error("socketInit: socket() failed, errno: {ERRNO}", "ERRNO",
+ errno);
std::string msg = "socket() failed " + std::string(strerror(errno));
throw std::runtime_error(msg);
}
if (bind(unixSocket, (struct sockaddr*)&socketAddr, sizeof(socketAddr)) ==
-1)
{
- log<level::ERR>(
- fmt::format("socketInit: bind() failed, errno({})", errno).c_str());
+ lg2::error("socketInit: bind() failed, errno: {ERRNO}", "ERRNO", errno);
close(unixSocket);
std::string msg = "socket bind failed " + std::string(strerror(errno));
throw std::runtime_error(msg);
}
if (listen(unixSocket, 1) == -1)
{
- log<level::ERR>(
- fmt::format("socketInit: listen() failed, errno({})", errno)
- .c_str());
+ lg2::error("socketInit: listen() failed, errno: {ERRNO}", "ERRNO",
+ errno);
close(unixSocket);
std::string msg = "listen() failed " + std::string(strerror(errno));
throw std::runtime_error(msg);
@@ -162,10 +155,8 @@
int retVal = select(numOfFDs, &readFD, NULL, NULL, &timeVal);
if (retVal <= 0)
{
- log<level::ERR>(
- fmt::format("select() failed, errno({}), DUMP_ID({})", errno,
- dumpId)
- .c_str());
+ lg2::error("select() failed, errno: {ERRNO}, DUMP_ID: {DUMP_ID}",
+ "ERRNO", errno, "DUMP_ID", dumpId);
std::string msg = "select() failed " + std::string(strerror(errno));
throw std::runtime_error(msg);
}
@@ -174,10 +165,9 @@
CustomFd socketFD = accept(unixSocket(), NULL, NULL);
if (socketFD() < 0)
{
- log<level::ERR>(
- fmt::format("accept() failed, errno({}), DUMP_ID({})",
- errno, dumpId)
- .c_str());
+ lg2::error(
+ "accept() failed, errno: {ERRNO}, DUMP_ID: {DUMP_ID}",
+ "ERRNO", errno, "DUMP_ID", dumpId);
std::string msg = "accept() failed " +
std::string(strerror(errno));
throw std::runtime_error(msg);
@@ -187,20 +177,18 @@
if (!infile.good())
{
// Unable to open the dump file
- log<level::ERR>(
- fmt::format("Failed to open the dump from file, errno({}), "
- "DUMPFILE({}), DUMP_ID({})",
- errno, file.c_str(), dumpId)
- .c_str());
+ lg2::error("Failed to open the dump from file, errno: {ERRNO}, "
+ "DUMPFILE: {DUMP_FILE}, DUMP_ID: {DUMP_ID}",
+ "ERRNO", errno, "DUMP_FILE", file, "DUMP_ID",
+ dumpId);
elog<Open>(ErrnoOpen(errno), PathOpen(file.c_str()));
}
infile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
std::ifstream::eofbit);
- log<level::INFO>(fmt::format("Opening File for RW, FILENAME({})",
- file.filename().c_str())
- .c_str());
+ lg2::info("Opening File for RW, FILENAME: {FILENAME}", "FILENAME",
+ file.filename().c_str());
std::filebuf* pbuf = infile.rdbuf();
@@ -221,21 +209,18 @@
{
std::remove(writePath.c_str());
auto err = errno;
- log<level::ERR>(
- fmt::format(
- "Failed to open, errormsg({}), OPENINTERFACE({}), DUMP_ID({})",
- oe.what(), file.c_str(), dumpId)
- .c_str());
+ lg2::error("Failed to open, errormsg: {ERROR}, "
+ "OPENINTERFACE: {OPEN_INTERFACE}, DUMP_ID: {DUMP_ID}",
+ "ERROR", oe, "OPEN_INTERFACE", file, "DUMP_ID", dumpId);
elog<Open>(ErrnoOpen(err), PathOpen(file.c_str()));
}
catch (const std::exception& e)
{
std::remove(writePath.c_str());
auto err = errno;
- log<level::ERR>(fmt::format("Failed to offload dump, errormsg({}), "
- "DUMPFILE({}), DUMP_ID({})",
- e.what(), writePath.c_str(), dumpId)
- .c_str());
+ lg2::error("Failed to offload dump, errormsg: {ERROR}, "
+ "DUMPFILE: {DUMP_FILE}, DUMP_ID: {DUMP_ID}",
+ "ERROR", e, "DUMP_FILE", writePath, "DUMP_ID", dumpId);
elog<Write>(ErrnoWrite(err), PathWrite(writePath.c_str()));
}
std::remove(writePath.c_str());