summaryrefslogtreecommitdiff
path: root/http/logging.hpp
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2023-07-25 03:01:38 +0300
committerEd Tanous <ed@tanous.net>2023-07-27 20:23:04 +0300
commite7245fe847e7282522a7979f816fd76f925348d3 (patch)
tree86a14497e2c6990e3609d6a4af9c12932f0d5b53 /http/logging.hpp
parent630adcdc7e705d020dd75de2bd335821863d20e9 (diff)
downloadbmcweb-e7245fe847e7282522a7979f816fd76f925348d3.tar.xz
Fix logging
The recent change to logging has caused a couple of bugs. First, when building within yocto, the complete path is now returned on log messages. This is wasteful of speed, and not super helpful to developers to have a full path. Per the discussion on the original patchset, drop this down to just the filename. 2, because of it's use as a pseudo log level, "enabled" is in the list of strings. This causes an index mismatch, which causes logs to be logged at the wrong level beyond debug. Move the entry to the end to fix this. Third, move the logging of level to upper case, to follow the old convention. Tested: Enabled meson option for logging, observed logs like: ``` Jul 25 18:39:20 qemux86-64 bmcweb[209]: [DEBUG query.hpp:121] setup redfish route Jul 25 18:39:20 qemux86-64 bmcweb[209]: [DEBUG http_response.hpp:248] 0x561bc11a7a40 releasing ce Jul 25 18:39:20 qemux86-64 bmcweb[209]: [DEBUG http_response.hpp:238] 0x561bc11a7a40 setting comr Jul 25 18:39:20 qemux86-64 bmcweb[209]: [DEBUG http_response.hpp:223] 0x561bc11a7a40 calling comr Jul 25 18:39:20 qemux86-64 bmcweb[209]: [DEBUG http_response.hpp:226] 0x561bc11a7a40 completion d Jul 25 18:39:20 qemux86-64 bmcweb[209]: [DEBUG query_param.hpp:1019] Processing query params ``` Change-Id: I4ac506c623a17f81ae83545e59291d2729dc82cb Signed-off-by: Ed Tanous <edtanous@google.com>
Diffstat (limited to 'http/logging.hpp')
-rw-r--r--http/logging.hpp42
1 files changed, 15 insertions, 27 deletions
diff --git a/http/logging.hpp b/http/logging.hpp
index bf5e42eb79..1679777b7b 100644
--- a/http/logging.hpp
+++ b/http/logging.hpp
@@ -121,31 +121,24 @@ namespace crow
enum class LogLevel
{
Disabled = 0,
- Debug,
- Info,
- Warning,
- Error,
Critical,
+ Error,
+ Warning,
+ Info,
+ Debug,
+ Enabled,
};
// Mapping of the external loglvl name to internal loglvl
-constexpr std::array<std::pair<std::string_view, crow::LogLevel>, 7>
- mapLogLevelFromName{{{"disabled", crow::LogLevel::Disabled},
- {"enabled", crow::LogLevel::Debug},
- {"debug", crow::LogLevel::Debug},
- {"info", crow::LogLevel::Info},
- {"warning", crow::LogLevel::Warning},
- {"error", crow::LogLevel::Error},
- {"critical", crow::LogLevel::Critical}}};
+constexpr std::array<std::string_view, 7> mapLogLevelFromName{
+ "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"};
constexpr crow::LogLevel getLogLevelFromName(std::string_view name)
{
- const auto* iter =
- std::find_if(begin(mapLogLevelFromName), end(mapLogLevelFromName),
- [&name](const auto& v) { return v.first == name; });
- if (iter != end(mapLogLevelFromName))
+ const auto* iter = std::ranges::find(mapLogLevelFromName, name);
+ if (iter != mapLogLevelFromName.end())
{
- return iter->second;
+ return static_cast<LogLevel>(iter - mapLogLevelFromName.begin());
}
return crow::LogLevel::Disabled;
}
@@ -178,24 +171,19 @@ const void* logPtr(T p)
template <LogLevel level>
inline void vlog(const FormatString& format, std::format_args&& args)
{
- if constexpr (bmcwebCurrentLoggingLevel > level)
+ if constexpr (bmcwebCurrentLoggingLevel <= level)
{
return;
}
constexpr size_t stringIndex = static_cast<size_t>(level);
static_assert(stringIndex < mapLogLevelFromName.size(),
"Missing string for level");
- constexpr std::string_view levelString =
- mapLogLevelFromName[stringIndex].first;
+ constexpr std::string_view levelString = mapLogLevelFromName[stringIndex];
std::string_view filename = format.loc.file_name();
- if (filename.starts_with("../"))
- {
- filename = filename.substr(3);
- }
+ filename = filename.substr(filename.rfind('/') + 1);
std::cout << std::format("[{} {}:{}] ", levelString, filename,
- format.loc.line());
- std::cout << std::vformat(format.str, args);
- std::putc('\n', stdout);
+ format.loc.line())
+ << std::vformat(format.str, args) << '\n';
}
} // namespace crow