Replace logging with std::format
std::format is a much more modern logging solution, and gives us a lot
more flexibility, and better compile times when doing logging.
Unfortunately, given its level of compile time checks, it needs to be a
method, instead of the stream style logging we had before. This
requires a pretty substantial change. Fortunately, this change can be
largely automated, via the script included in this commit under
scripts/replace_logs.py. This is to aid people in moving their
patchsets over to the new form in the short period where old patches
will be based on the old logging. The intention is that this script
eventually goes away.
The old style logging (stream based) looked like.
BMCWEB_LOG_DEBUG << "Foo " << foo;
The new equivalent of the above would be:
BMCWEB_LOG_DEBUG("Foo {}", foo);
In the course of doing this, this also cleans up several ignored linter
errors, including macro usage, and array to pointer deconstruction.
Note, This patchset does remove the timestamp from the log message. In
practice, this was duplicated between journald and bmcweb, and there's
no need for both to exist.
One design decision of note is the addition of logPtr. Because the
compiler can't disambiguate between const char* and const MyThing*, it's
necessary to add an explicit cast to void*. This is identical to how
fmt handled it.
Tested: compiled with logging meson_option enabled, and launched bmcweb
Saw the usual logging, similar to what was present before:
```
[Error include/webassets.hpp:60] Unable to find or open /usr/share/www/ static file hosting disabled
[Debug include/persistent_data.hpp:133] Restored Session Timeout: 1800
[Debug redfish-core/include/event_service_manager.hpp:671] Old eventService config not exist
[Info src/webserver_main.cpp:59] Starting webserver on port 18080
[Error redfish-core/include/event_service_manager.hpp:1301] inotify_add_watch failed for redfish log file.
[Info src/webserver_main.cpp:137] Start Hostname Monitor Service...
```
Signed-off-by: Ed Tanous <ed@tanous.net>
Change-Id: I86a46aa2454be7fe80df608cb7e5573ca4029ec8
diff --git a/include/authentication.hpp b/include/authentication.hpp
index 9e3405b..f3246c0 100644
--- a/include/authentication.hpp
+++ b/include/authentication.hpp
@@ -38,7 +38,7 @@
performBasicAuth(const boost::asio::ip::address& clientIp,
std::string_view authHeader)
{
- BMCWEB_LOG_DEBUG << "[AuthMiddleware] Basic authentication";
+ BMCWEB_LOG_DEBUG("[AuthMiddleware] Basic authentication");
if (!authHeader.starts_with("Basic "))
{
@@ -66,9 +66,9 @@
}
std::string pass = authData.substr(separator);
- BMCWEB_LOG_DEBUG << "[AuthMiddleware] Authenticating user: " << user;
- BMCWEB_LOG_DEBUG << "[AuthMiddleware] User IPAddress: "
- << clientIp.to_string();
+ BMCWEB_LOG_DEBUG("[AuthMiddleware] Authenticating user: {}", user);
+ BMCWEB_LOG_DEBUG("[AuthMiddleware] User IPAddress: {}",
+ clientIp.to_string());
int pamrc = pamAuthenticateUser(user, pass);
bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
@@ -93,7 +93,7 @@
static std::shared_ptr<persistent_data::UserSession>
performTokenAuth(std::string_view authHeader)
{
- BMCWEB_LOG_DEBUG << "[AuthMiddleware] Token authentication";
+ BMCWEB_LOG_DEBUG("[AuthMiddleware] Token authentication");
if (!authHeader.starts_with("Token "))
{
return nullptr;
@@ -109,7 +109,7 @@
static std::shared_ptr<persistent_data::UserSession>
performXtokenAuth(const boost::beast::http::header<true>& reqHeader)
{
- BMCWEB_LOG_DEBUG << "[AuthMiddleware] X-Auth-Token authentication";
+ BMCWEB_LOG_DEBUG("[AuthMiddleware] X-Auth-Token authentication");
std::string_view token = reqHeader["X-Auth-Token"];
if (token.empty())
@@ -127,7 +127,7 @@
performCookieAuth(boost::beast::http::verb method [[maybe_unused]],
const boost::beast::http::header<true>& reqHeader)
{
- BMCWEB_LOG_DEBUG << "[AuthMiddleware] Cookie authentication";
+ BMCWEB_LOG_DEBUG("[AuthMiddleware] Cookie authentication");
std::string_view cookieValue = reqHeader["Cookie"];
if (cookieValue.empty())
@@ -195,8 +195,8 @@
// set cookie only if this is req from the browser.
if (reqHeader["User-Agent"].empty())
{
- BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId
- << " will be used for this request.";
+ BMCWEB_LOG_DEBUG(" TLS session: {} will be used for this request.",
+ sp->uniqueId);
return sp;
}
// TODO: change this to not switch to cookie auth
@@ -208,8 +208,9 @@
"; SameSite=Strict; Secure; HttpOnly");
res.addHeader(boost::beast::http::field::set_cookie,
"IsAuthenticated=true; Secure");
- BMCWEB_LOG_DEBUG << " TLS session: " << sp->uniqueId
- << " with cookie will be used for this request.";
+ BMCWEB_LOG_DEBUG(
+ " TLS session: {} with cookie will be used for this request.",
+ sp->uniqueId);
return sp;
}
return nullptr;
@@ -284,7 +285,7 @@
}
#endif
std::string_view authHeader = reqHeader["Authorization"];
- BMCWEB_LOG_DEBUG << "authHeader=" << authHeader;
+ BMCWEB_LOG_DEBUG("authHeader={}", authHeader);
if (sessionOut == nullptr && authMethodsConfig.sessionToken)
{