format: Include format lib and use on errors opening JSON files
Included the format library used to add more details to the journal
message without needing the verbose output and updated the journal
logging when loading a JSON file. When loading a JSON file, now any
errors will produce a journal message atleast containing the JSON file
that failed to be loaded.
Tested:
Removed JSON configuration file and attempted to load it
Journal msg shows which JSON configuration file is loaded now
Failure to parse JSON shows file and exception in journal msg
Change-Id: I6bec9bb01d8e95c3dced467ea96163129c59619b
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/configure.ac b/configure.ac
index 4e89854..3d79076 100644
--- a/configure.ac
+++ b/configure.ac
@@ -33,6 +33,7 @@
PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces])
PKG_CHECK_MODULES([LIBEVDEV], [libevdev])
PKG_CHECK_MODULES([STDPLUS], [stdplus])
+PKG_CHECK_MODULES([FMT], [fmt])
# Checks for library functions.
LT_INIT # Required for systemd linking
diff --git a/control/Makefile.am b/control/Makefile.am
index 723b101..8812b58 100644
--- a/control/Makefile.am
+++ b/control/Makefile.am
@@ -23,7 +23,8 @@
$(SDBUSPLUS_LIBS) \
$(SDEVENTPLUS_LIBS) \
$(PHOSPHOR_LOGGING_LIBS) \
- ${PHOSPHOR_DBUS_INTERFACES_LIBS}
+ ${PHOSPHOR_DBUS_INTERFACES_LIBS} \
+ $(FMT_LIBS)
phosphor_fan_control_CXXFLAGS = \
$(SDBUSPLUS_CFLAGS) \
diff --git a/json_config.hpp b/json_config.hpp
index b0acddc..0c2721d 100644
--- a/json_config.hpp
+++ b/json_config.hpp
@@ -17,6 +17,8 @@
#include "sdbusplus.hpp"
+#include <fmt/format.h>
+
#include <nlohmann/json.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
@@ -108,9 +110,14 @@
{
if (!isOptional)
{
- log<level::ERR>("No JSON config file found",
- entry("DEFAULT_FILE=%s", confFile.c_str()));
- throw std::runtime_error("No JSON config file found");
+ log<level::ERR>(
+ fmt::format("No JSON config file found. Default file: {}",
+ confFile.string())
+ .c_str());
+ throw std::runtime_error(
+ fmt::format("No JSON config file found. Default file: {}",
+ confFile.string())
+ .c_str());
}
else
{
@@ -136,8 +143,9 @@
if (!confFile.empty() && fs::exists(confFile))
{
- log<level::INFO>("Loading configuration",
- entry("JSON_FILE=%s", confFile.c_str()));
+ log<level::INFO>(
+ fmt::format("Loading configuration from {}", confFile.string())
+ .c_str());
file.open(confFile);
try
{
@@ -145,17 +153,27 @@
}
catch (std::exception& e)
{
- log<level::ERR>("Failed to parse JSON config file",
- entry("JSON_FILE=%s", confFile.c_str()),
- entry("JSON_ERROR=%s", e.what()));
- throw std::runtime_error("Failed to parse JSON config file");
+ log<level::ERR>(
+ fmt::format(
+ "Failed to parse JSON config file: {}, error: {}",
+ confFile.string(), e.what())
+ .c_str());
+ throw std::runtime_error(
+ fmt::format(
+ "Failed to parse JSON config file: {}, error: {}",
+ confFile.string(), e.what())
+ .c_str());
}
}
else
{
- log<level::ERR>("Unable to open JSON config file",
- entry("JSON_FILE=%s", confFile.c_str()));
- throw std::runtime_error("Unable to open JSON config file");
+ log<level::ERR>(fmt::format("Unable to open JSON config file: {}",
+ confFile.string())
+ .c_str());
+ throw std::runtime_error(
+ fmt::format("Unable to open JSON config file: {}",
+ confFile.string())
+ .c_str());
}
return jsonConf;
diff --git a/monitor/Makefile.am b/monitor/Makefile.am
index f488859..e1bfcc0 100644
--- a/monitor/Makefile.am
+++ b/monitor/Makefile.am
@@ -18,7 +18,8 @@
$(PHOSPHOR_LOGGING_LIBS) \
${PHOSPHOR_DBUS_INTERFACES_LIBS} \
-lstdc++fs \
- $(STDPLUS_LIBS)
+ $(STDPLUS_LIBS) \
+ $(FMT_LIBS)
phosphor_fan_monitor_CXXFLAGS = \
$(SDBUSPLUS_CFLAGS) \
diff --git a/presence/Makefile.am b/presence/Makefile.am
index d67f287..2f1d787 100644
--- a/presence/Makefile.am
+++ b/presence/Makefile.am
@@ -20,7 +20,9 @@
$(PHOSPHOR_LOGGING_LIBS) \
${PHOSPHOR_DBUS_INTERFACES_LIBS} \
$(LIBEVDEV_LIBS) \
- $(STDPLUS_LIBS)
+ $(STDPLUS_LIBS) \
+ $(FMT_LIBS)
+
phosphor_fan_presence_tach_CXXFLAGS = \
$(SDBUSPLUS_CFLAGS) \
$(SDEVENTPLUS_CFLAGS) \