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/http/http_client.hpp b/http/http_client.hpp
index 07fa85d..d82c566 100644
--- a/http/http_client.hpp
+++ b/http/http_client.hpp
@@ -84,7 +84,7 @@
defaultRetryHandler(unsigned int respCode)
{
// As a default, assume 200X is alright
- BMCWEB_LOG_DEBUG << "Using default check for response code validity";
+ BMCWEB_LOG_DEBUG("Using default check for response code validity");
if ((respCode < 200) || (respCode >= 300))
{
return boost::system::errc::make_error_code(
@@ -165,9 +165,8 @@
void doResolve()
{
state = ConnState::resolveInProgress;
- BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":"
- << std::to_string(port)
- << ", id: " << std::to_string(connId);
+ BMCWEB_LOG_DEBUG("Trying to resolve: {}:{}, id: {}", host,
+ std::to_string(port), std::to_string(connId));
resolver.async_resolve(host, std::to_string(port),
std::bind_front(&ConnectionInfo::afterResolve,
@@ -180,19 +179,18 @@
{
if (ec || (endpointList.empty()))
{
- BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message() << " "
- << host << ":" << std::to_string(port);
+ BMCWEB_LOG_ERROR("Resolve failed: {} {}:{}", ec.message(), host,
+ std::to_string(port));
state = ConnState::resolveFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG << "Resolved " << host << ":" << std::to_string(port)
- << ", id: " << std::to_string(connId);
+ BMCWEB_LOG_DEBUG("Resolved {}:{}, id: {}", host, std::to_string(port),
+ std::to_string(connId));
state = ConnState::connectInProgress;
- BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":"
- << std::to_string(port)
- << ", id: " << std::to_string(connId);
+ BMCWEB_LOG_DEBUG("Trying to connect to: {}:{}, id: {}", host,
+ std::to_string(port), std::to_string(connId));
timer.expires_after(std::chrono::seconds(30));
timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
@@ -217,17 +215,17 @@
timer.cancel();
if (ec)
{
- BMCWEB_LOG_ERROR << "Connect " << endpoint.address().to_string()
- << ":" << std::to_string(endpoint.port())
- << ", id: " << std::to_string(connId)
- << " failed: " << ec.message();
+ BMCWEB_LOG_ERROR("Connect {}:{}, id: {} failed: {}",
+ endpoint.address().to_string(),
+ std::to_string(endpoint.port()),
+ std::to_string(connId), ec.message());
state = ConnState::connectFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG << "Connected to: " << endpoint.address().to_string()
- << ":" << std::to_string(endpoint.port())
- << ", id: " << std::to_string(connId);
+ BMCWEB_LOG_DEBUG(
+ "Connected to: {}:{}, id: {}", endpoint.address().to_string(),
+ std::to_string(endpoint.port()), std::to_string(connId));
if (sslConn)
{
doSslHandshake();
@@ -265,15 +263,14 @@
timer.cancel();
if (ec)
{
- BMCWEB_LOG_ERROR << "SSL Handshake failed -"
- << " id: " << std::to_string(connId)
- << " error: " << ec.message();
+ BMCWEB_LOG_ERROR("SSL Handshake failed - id: {} error: {}",
+ std::to_string(connId), ec.message());
state = ConnState::handshakeFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG << "SSL Handshake successful -"
- << " id: " << std::to_string(connId);
+ BMCWEB_LOG_DEBUG("SSL Handshake successful - id: {}",
+ std::to_string(connId));
state = ConnState::connected;
sendMessage();
}
@@ -316,14 +313,14 @@
timer.cancel();
if (ec)
{
- BMCWEB_LOG_ERROR << "sendMessage() failed: " << ec.message() << " "
- << host << ":" << std::to_string(port);
+ BMCWEB_LOG_ERROR("sendMessage() failed: {} {}:{}", ec.message(),
+ host, std::to_string(port));
state = ConnState::sendFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: "
- << bytesTransferred;
+ BMCWEB_LOG_DEBUG("sendMessage() bytes transferred: {}",
+ bytesTransferred);
recvMessage();
}
@@ -370,28 +367,28 @@
timer.cancel();
if (ec && ec != boost::asio::ssl::error::stream_truncated)
{
- BMCWEB_LOG_ERROR << "recvMessage() failed: " << ec.message()
- << " from " << host << ":" << std::to_string(port);
+ BMCWEB_LOG_ERROR("recvMessage() failed: {} from {}:{}",
+ ec.message(), host, std::to_string(port));
state = ConnState::recvFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: "
- << bytesTransferred;
- BMCWEB_LOG_DEBUG << "recvMessage() data: " << parser->get().body();
+ BMCWEB_LOG_DEBUG("recvMessage() bytes transferred: {}",
+ bytesTransferred);
+ BMCWEB_LOG_DEBUG("recvMessage() data: {}", parser->get().body());
unsigned int respCode = parser->get().result_int();
- BMCWEB_LOG_DEBUG << "recvMessage() Header Response Code: " << respCode;
+ BMCWEB_LOG_DEBUG("recvMessage() Header Response Code: {}", respCode);
// Make sure the received response code is valid as defined by
// the associated retry policy
if (connPolicy->invalidResp(respCode))
{
// The listener failed to receive the Sent-Event
- BMCWEB_LOG_ERROR << "recvMessage() Listener Failed to "
- "receive Sent-Event. Header Response Code: "
- << respCode << " from " << host << ":"
- << std::to_string(port);
+ BMCWEB_LOG_ERROR(
+ "recvMessage() Listener Failed to "
+ "receive Sent-Event. Header Response Code: {} from {}:{}",
+ respCode, host, std::to_string(port));
state = ConnState::recvFailed;
waitAndRetry();
return;
@@ -403,8 +400,7 @@
// Keep the connection alive if server supports it
// Else close the connection
- BMCWEB_LOG_DEBUG << "recvMessage() keepalive : "
- << parser->keep_alive();
+ BMCWEB_LOG_DEBUG("recvMessage() keepalive : {}", parser->keep_alive());
// Copy the response into a Response object so that it can be
// processed by the callback function.
@@ -418,13 +414,13 @@
{
if (ec == boost::asio::error::operation_aborted)
{
- BMCWEB_LOG_DEBUG
- << "async_wait failed since the operation is aborted";
+ BMCWEB_LOG_DEBUG(
+ "async_wait failed since the operation is aborted");
return;
}
if (ec)
{
- BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message();
+ BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message());
// If the timer fails, we need to close the socket anyway, same as
// if it expired.
}
@@ -441,10 +437,9 @@
if ((retryCount >= connPolicy->maxRetryAttempts) ||
(state == ConnState::sslInitFailed))
{
- BMCWEB_LOG_ERROR << "Maximum number of retries reached."
- << " " << host << ":" << std::to_string(port);
- BMCWEB_LOG_DEBUG << "Retry policy: "
- << connPolicy->retryPolicyAction;
+ BMCWEB_LOG_ERROR("Maximum number of retries reached. {}:{}", host,
+ std::to_string(port));
+ BMCWEB_LOG_DEBUG("Retry policy: {}", connPolicy->retryPolicyAction);
if (connPolicy->retryPolicyAction == "TerminateAfterRetries")
{
@@ -470,10 +465,9 @@
retryCount++;
- BMCWEB_LOG_DEBUG << "Attempt retry after "
- << std::to_string(
- connPolicy->retryIntervalSecs.count())
- << " seconds. RetryCount = " << retryCount;
+ BMCWEB_LOG_DEBUG("Attempt retry after {} seconds. RetryCount = {}",
+ std::to_string(connPolicy->retryIntervalSecs.count()),
+ retryCount);
timer.expires_after(connPolicy->retryIntervalSecs);
timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this,
shared_from_this()));
@@ -484,13 +478,13 @@
{
if (ec == boost::asio::error::operation_aborted)
{
- BMCWEB_LOG_DEBUG
- << "async_wait failed since the operation is aborted"
- << ec.message();
+ BMCWEB_LOG_DEBUG(
+ "async_wait failed since the operation is aborted{}",
+ ec.message());
}
else if (ec)
{
- BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message();
+ BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message());
// Ignore the error and continue the retry loop to attempt
// sending the event as per the retry policy
}
@@ -508,15 +502,14 @@
// not_connected happens sometimes so don't bother reporting it.
if (ec && ec != boost::beast::errc::not_connected)
{
- BMCWEB_LOG_ERROR << host << ":" << std::to_string(port)
- << ", id: " << std::to_string(connId)
- << " shutdown failed: " << ec.message();
+ BMCWEB_LOG_ERROR("{}:{}, id: {} shutdown failed: {}", host,
+ std::to_string(port), std::to_string(connId),
+ ec.message());
}
else
{
- BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port)
- << ", id: " << std::to_string(connId)
- << " closed gracefully";
+ BMCWEB_LOG_DEBUG("{}:{}, id: {} closed gracefully", host,
+ std::to_string(port), std::to_string(connId));
}
if (retry)
@@ -549,15 +542,14 @@
{
if (ec)
{
- BMCWEB_LOG_ERROR << host << ":" << std::to_string(port)
- << ", id: " << std::to_string(connId)
- << " shutdown failed: " << ec.message();
+ BMCWEB_LOG_ERROR("{}:{}, id: {} shutdown failed: {}", host,
+ std::to_string(port), std::to_string(connId),
+ ec.message());
}
else
{
- BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port)
- << ", id: " << std::to_string(connId)
- << " closed gracefully";
+ BMCWEB_LOG_DEBUG("{}:{}, id: {} closed gracefully", host,
+ std::to_string(port), std::to_string(connId));
}
shutdownConn(retry);
}
@@ -583,9 +575,9 @@
boost::beast::error_code ec{static_cast<int>(::ERR_get_error()),
boost::asio::error::get_ssl_category()};
- BMCWEB_LOG_ERROR << "SSL_set_tlsext_host_name " << host << ":"
- << port << ", id: " << std::to_string(connId)
- << " failed: " << ec.message();
+ BMCWEB_LOG_ERROR(
+ "SSL_set_tlsext_host_name {}:{}, id: {} failed: {}", host, port,
+ std::to_string(connId), ec.message());
// Set state as sslInit failed so that we close the connection
// and take appropriate action as per retry configuration.
state = ConnState::sslInitFailed;
@@ -611,8 +603,8 @@
if (!sslCtx)
{
- BMCWEB_LOG_ERROR << "prepareSSLContext failed - " << host << ":"
- << port << ", id: " << std::to_string(connId);
+ BMCWEB_LOG_ERROR("prepareSSLContext failed - {}:{}, id: {}",
+ host, port, std::to_string(connId));
// Don't retry if failure occurs while preparing SSL context
// such as certificate is invalid or set cipher failure or set
// host name failure etc... Setting conn state to sslInitFailed
@@ -648,8 +640,8 @@
{
if (requestQueue.empty())
{
- BMCWEB_LOG_ERROR
- << "setConnProps() should not have been called when requestQueue is empty";
+ BMCWEB_LOG_ERROR(
+ "setConnProps() should not have been called when requestQueue is empty");
return;
}
@@ -657,9 +649,9 @@
conn.req = std::move(nextReq.req);
conn.callback = std::move(nextReq.callback);
- BMCWEB_LOG_DEBUG << "Setting properties for connection " << conn.host
- << ":" << std::to_string(conn.port)
- << ", id: " << std::to_string(conn.connId);
+ BMCWEB_LOG_DEBUG("Setting properties for connection {}:{}, id: {}",
+ conn.host, std::to_string(conn.port),
+ std::to_string(conn.connId));
// We can remove the request from the queue at this point
requestQueue.pop_front();
@@ -680,11 +672,10 @@
// Reuse the connection to send the next request in the queue
if (!requestQueue.empty())
{
- BMCWEB_LOG_DEBUG << std::to_string(requestQueue.size())
- << " requests remaining in queue for " << destIP
- << ":" << std::to_string(destPort)
- << ", reusing connnection "
- << std::to_string(connId);
+ BMCWEB_LOG_DEBUG(
+ "{} requests remaining in queue for {}:{}, reusing connnection {}",
+ std::to_string(requestQueue.size()), destIP,
+ std::to_string(destPort), std::to_string(connId));
setConnProps(*conn);
@@ -744,14 +735,13 @@
if (conn->state == ConnState::idle)
{
- BMCWEB_LOG_DEBUG << "Grabbing idle connection "
- << commonMsg;
+ BMCWEB_LOG_DEBUG("Grabbing idle connection {}", commonMsg);
conn->sendMessage();
}
else
{
- BMCWEB_LOG_DEBUG << "Reusing existing connection "
- << commonMsg;
+ BMCWEB_LOG_DEBUG("Reusing existing connection {}",
+ commonMsg);
conn->doResolve();
}
return;
@@ -762,8 +752,8 @@
// the queue
if (connections.size() < connPolicy->maxConnections)
{
- BMCWEB_LOG_DEBUG << "Adding new connection to pool " << destIP
- << ":" << std::to_string(destPort);
+ BMCWEB_LOG_DEBUG("Adding new connection to pool {}:{}", destIP,
+ std::to_string(destPort));
auto conn = addConnection();
conn->req = std::move(thisReq);
conn->callback = std::move(cb);
@@ -771,16 +761,17 @@
}
else if (requestQueue.size() < maxRequestQueueSize)
{
- BMCWEB_LOG_ERROR << "Max pool size reached. Adding data to queue."
- << destIP << ":" << std::to_string(destPort);
+ BMCWEB_LOG_ERROR(
+ "Max pool size reached. Adding data to queue.{}:{}", destIP,
+ std::to_string(destPort));
requestQueue.emplace_back(std::move(thisReq), std::move(cb));
}
else
{
// If we can't buffer the request then we should let the callback
// handle a 429 Too Many Requests dummy response
- BMCWEB_LOG_ERROR << destIP << ":" << std::to_string(destPort)
- << " request queue full. Dropping request.";
+ BMCWEB_LOG_ERROR("{}:{} request queue full. Dropping request.",
+ destIP, std::to_string(destPort));
Response dummyRes;
dummyRes.result(boost::beast::http::status::too_many_requests);
resHandler(dummyRes);
@@ -801,7 +792,8 @@
std::shared_ptr<ConnectionPool> self = weakSelf.lock();
if (!self)
{
- BMCWEB_LOG_CRITICAL << self << " Failed to capture connection";
+ BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
+ logPtr(self.get()));
return;
}
@@ -815,10 +807,9 @@
auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>(
ioc, id, connPolicy, destIP, destPort, useSSL, newId));
- BMCWEB_LOG_DEBUG << "Added connection "
- << std::to_string(connections.size() - 1)
- << " to pool " << destIP << ":"
- << std::to_string(destPort);
+ BMCWEB_LOG_DEBUG("Added connection {} to pool {}:{}",
+ std::to_string(connections.size() - 1), destIP,
+ std::to_string(destPort));
return ret;
}
@@ -832,8 +823,8 @@
id(idIn), connPolicy(connPolicyIn), destIP(destIPIn),
destPort(destPortIn), useSSL(useSSLIn)
{
- BMCWEB_LOG_DEBUG << "Initializing connection pool for " << destIP << ":"
- << std::to_string(destPort);
+ BMCWEB_LOG_DEBUG("Initializing connection pool for {}:{}", destIP,
+ std::to_string(destPort));
// Initialize the pool with a single connection
addConnection();
@@ -852,8 +843,8 @@
// sendDataWithCallback()
static void genericResHandler(const Response& res)
{
- BMCWEB_LOG_DEBUG << "Response handled with return code: "
- << std::to_string(res.resultInt());
+ BMCWEB_LOG_DEBUG("Response handled with return code: {}",
+ std::to_string(res.resultInt()));
}
public: