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/ibm/management_console_rest.hpp b/include/ibm/management_console_rest.hpp
index 579d46c..b87cb1e 100644
--- a/include/ibm/management_console_rest.hpp
+++ b/include/ibm/management_console_rest.hpp
@@ -55,11 +55,11 @@
asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg;
return;
}
- BMCWEB_LOG_DEBUG
- << "File upload in application/octet-stream format. Continue..";
+ BMCWEB_LOG_DEBUG(
+ "File upload in application/octet-stream format. Continue..");
- BMCWEB_LOG_DEBUG
- << "handleIbmPut: Request to create/update the save-area file";
+ BMCWEB_LOG_DEBUG(
+ "handleIbmPut: Request to create/update the save-area file");
std::string_view path =
"/var/lib/bmcweb/ibm-management-console/configfiles";
if (!crow::ibm_utils::createDirectory(path))
@@ -80,9 +80,9 @@
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] = internalServerError;
- BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to prepare save-area "
- "directory iterator. ec : "
- << ec;
+ BMCWEB_LOG_DEBUG("handleIbmPut: Failed to prepare save-area "
+ "directory iterator. ec : {}",
+ ec.message());
return;
}
std::uintmax_t saveAreaDirSize = 0;
@@ -95,9 +95,9 @@
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] = internalServerError;
- BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
- "directory . ec : "
- << ec;
+ BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find save-area "
+ "directory . ec : {}",
+ ec.message());
return;
}
std::uintmax_t fileSize = std::filesystem::file_size(it, ec);
@@ -106,19 +106,19 @@
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] = internalServerError;
- BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find save-area "
- "file size inside the directory . ec : "
- << ec;
+ BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find save-area "
+ "file size inside the directory . ec : {}",
+ ec.message());
return;
}
saveAreaDirSize += fileSize;
}
}
- BMCWEB_LOG_DEBUG << "saveAreaDirSize: " << saveAreaDirSize;
+ BMCWEB_LOG_DEBUG("saveAreaDirSize: {}", saveAreaDirSize);
// Get the file size getting uploaded
const std::string& data = req.body();
- BMCWEB_LOG_DEBUG << "data length: " << data.length();
+ BMCWEB_LOG_DEBUG("data length: {}", data.length());
if (data.length() < minSaveareaFileSize)
{
@@ -137,7 +137,7 @@
// Form the file path
loc /= fileID;
- BMCWEB_LOG_DEBUG << "Writing to the file: " << loc.string();
+ BMCWEB_LOG_DEBUG("Writing to the file: {}", loc.string());
// Check if the same file exists in the directory
bool fileExists = std::filesystem::exists(loc, ec);
@@ -146,8 +146,8 @@
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] = internalServerError;
- BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find if file exists. ec : "
- << ec;
+ BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find if file exists. ec : {}",
+ ec.message());
return;
}
@@ -161,8 +161,8 @@
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] = internalServerError;
- BMCWEB_LOG_DEBUG << "handleIbmPut: Failed to find file size. ec : "
- << ec;
+ BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find file size. ec : {}",
+ ec.message());
return;
}
// Calculate the difference in the file size.
@@ -175,7 +175,7 @@
{
newSizeToWrite = data.length() - currentFileSize;
}
- BMCWEB_LOG_DEBUG << "newSizeToWrite: " << newSizeToWrite;
+ BMCWEB_LOG_DEBUG("newSizeToWrite: {}", newSizeToWrite);
}
else
{
@@ -184,7 +184,7 @@
}
// Calculate the total dir size before writing the new file
- BMCWEB_LOG_DEBUG << "total new size: " << saveAreaDirSize + newSizeToWrite;
+ BMCWEB_LOG_DEBUG("total new size: {}", saveAreaDirSize + newSizeToWrite);
if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize)
{
@@ -204,7 +204,7 @@
if (file.fail())
{
- BMCWEB_LOG_DEBUG << "Error while opening the file for writing";
+ BMCWEB_LOG_DEBUG("Error while opening the file for writing");
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] =
@@ -217,12 +217,12 @@
// Push an event
if (fileExists)
{
- BMCWEB_LOG_DEBUG << "config file is updated";
+ BMCWEB_LOG_DEBUG("config file is updated");
asyncResp->res.jsonValue["Description"] = "File Updated";
}
else
{
- BMCWEB_LOG_DEBUG << "config file is created";
+ BMCWEB_LOG_DEBUG("config file is created");
asyncResp->res.jsonValue["Description"] = "File Created";
}
}
@@ -271,9 +271,9 @@
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] = internalServerError;
- BMCWEB_LOG_DEBUG << "deleteConfigFiles: Failed to delete the "
- "config files directory. ec : "
- << ec;
+ BMCWEB_LOG_DEBUG("deleteConfigFiles: Failed to delete the "
+ "config files directory. ec : {}",
+ ec.message());
}
}
}
@@ -297,12 +297,12 @@
inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& fileID)
{
- BMCWEB_LOG_DEBUG << "HandleGet on SaveArea files on path: " << fileID;
+ BMCWEB_LOG_DEBUG("HandleGet on SaveArea files on path: {}", fileID);
std::filesystem::path loc(
"/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID);
if (!std::filesystem::exists(loc) || !std::filesystem::is_regular_file(loc))
{
- BMCWEB_LOG_WARNING << loc.string() << " Not found";
+ BMCWEB_LOG_WARNING("{} Not found", loc.string());
asyncResp->res.result(boost::beast::http::status::not_found);
asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
return;
@@ -311,7 +311,7 @@
std::ifstream readfile(loc.string());
if (!readfile)
{
- BMCWEB_LOG_WARNING << loc.string() << " Not found";
+ BMCWEB_LOG_WARNING("{} Not found", loc.string());
asyncResp->res.result(boost::beast::http::status::not_found);
asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
return;
@@ -333,18 +333,18 @@
{
std::string filePath("/var/lib/bmcweb/ibm-management-console/configfiles/" +
fileID);
- BMCWEB_LOG_DEBUG << "Removing the file : " << filePath << "\n";
+ BMCWEB_LOG_DEBUG("Removing the file : {}", filePath);
std::ifstream fileOpen(filePath.c_str());
if (static_cast<bool>(fileOpen))
{
if (remove(filePath.c_str()) == 0)
{
- BMCWEB_LOG_DEBUG << "File removed!\n";
+ BMCWEB_LOG_DEBUG("File removed!");
asyncResp->res.jsonValue["Description"] = "File Deleted";
}
else
{
- BMCWEB_LOG_ERROR << "File not removed!\n";
+ BMCWEB_LOG_ERROR("File not removed!");
asyncResp->res.result(
boost::beast::http::status::internal_server_error);
asyncResp->res.jsonValue["Description"] = internalServerError;
@@ -352,7 +352,7 @@
}
else
{
- BMCWEB_LOG_WARNING << "File not found!\n";
+ BMCWEB_LOG_WARNING("File not found!");
asyncResp->res.result(boost::beast::http::status::not_found);
asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg;
}
@@ -367,13 +367,13 @@
if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Message",
broadcastMsg))
{
- BMCWEB_LOG_DEBUG << "Not a Valid JSON";
+ BMCWEB_LOG_DEBUG("Not a Valid JSON");
asyncResp->res.result(boost::beast::http::status::bad_request);
return;
}
if (broadcastMsg.size() > maxBroadcastMsgSize)
{
- BMCWEB_LOG_ERROR << "Message size exceeds maximum allowed size[1KB]";
+ BMCWEB_LOG_ERROR("Message size exceeds maximum allowed size[1KB]");
asyncResp->res.result(boost::beast::http::status::bad_request);
return;
}
@@ -418,14 +418,14 @@
lockType, "ResourceID", resourceId,
"SegmentFlags", segmentFlags))
{
- BMCWEB_LOG_DEBUG << "Not a Valid JSON";
+ BMCWEB_LOG_DEBUG("Not a Valid JSON");
asyncResp->res.result(boost::beast::http::status::bad_request);
return;
}
- BMCWEB_LOG_DEBUG << lockType;
- BMCWEB_LOG_DEBUG << resourceId;
+ BMCWEB_LOG_DEBUG("{}", lockType);
+ BMCWEB_LOG_DEBUG("{}", resourceId);
- BMCWEB_LOG_DEBUG << "Segment Flags are present";
+ BMCWEB_LOG_DEBUG("Segment Flags are present");
for (auto& e : segmentFlags)
{
@@ -440,8 +440,8 @@
return;
}
- BMCWEB_LOG_DEBUG << "Lockflag : " << lockFlags;
- BMCWEB_LOG_DEBUG << "SegmentLength : " << segmentLength;
+ BMCWEB_LOG_DEBUG("Lockflag : {}", lockFlags);
+ BMCWEB_LOG_DEBUG("SegmentLength : {}", segmentLength);
segInfo.emplace_back(std::make_pair(lockFlags, segmentLength));
}
@@ -455,14 +455,14 @@
for (auto& i : lockRequestStructure)
{
- BMCWEB_LOG_DEBUG << std::get<0>(i);
- BMCWEB_LOG_DEBUG << std::get<1>(i);
- BMCWEB_LOG_DEBUG << std::get<2>(i);
- BMCWEB_LOG_DEBUG << std::get<3>(i);
+ BMCWEB_LOG_DEBUG("{}", std::get<0>(i));
+ BMCWEB_LOG_DEBUG("{}", std::get<1>(i));
+ BMCWEB_LOG_DEBUG("{}", std::get<2>(i));
+ BMCWEB_LOG_DEBUG("{}", std::get<3>(i));
for (const auto& p : std::get<4>(i))
{
- BMCWEB_LOG_DEBUG << p.first << ", " << p.second;
+ BMCWEB_LOG_DEBUG("{}, {}", p.first, p.second);
}
}
@@ -479,14 +479,14 @@
if ((!validityStatus.first) && (validityStatus.second == 0))
{
- BMCWEB_LOG_DEBUG << "Not a Valid record";
- BMCWEB_LOG_DEBUG << "Bad json in request";
+ BMCWEB_LOG_DEBUG("Not a Valid record");
+ BMCWEB_LOG_DEBUG("Bad json in request");
asyncResp->res.result(boost::beast::http::status::bad_request);
return;
}
if (validityStatus.first && (validityStatus.second == 1))
{
- BMCWEB_LOG_ERROR << "There is a conflict within itself";
+ BMCWEB_LOG_ERROR("There is a conflict within itself");
asyncResp->res.result(boost::beast::http::status::conflict);
return;
}
@@ -497,7 +497,7 @@
std::get<crow::ibm_mc_lock::Rc>(varAcquireLock.second);
if (!conflictStatus.first)
{
- BMCWEB_LOG_DEBUG << "There is no conflict with the locktable";
+ BMCWEB_LOG_DEBUG("There is no conflict with the locktable");
asyncResp->res.result(boost::beast::http::status::ok);
auto var = std::get<uint32_t>(conflictStatus.second);
@@ -506,7 +506,7 @@
asyncResp->res.jsonValue["TransactionID"] = var;
return;
}
- BMCWEB_LOG_DEBUG << "There is a conflict with the lock table";
+ BMCWEB_LOG_DEBUG("There is a conflict with the lock table");
asyncResp->res.result(boost::beast::http::status::conflict);
auto var =
std::get<std::pair<uint32_t, LockRequest>>(conflictStatus.second);
@@ -527,7 +527,7 @@
}
returnJson["SegmentFlags"] = myarray;
- BMCWEB_LOG_ERROR << "Conflicting lock record: " << returnJson;
+ BMCWEB_LOG_ERROR("Conflicting lock record: {}", returnJson);
asyncResp->res.jsonValue["Record"] = returnJson;
return;
}
@@ -545,11 +545,11 @@
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::vector<uint32_t>& listTransactionIds)
{
- BMCWEB_LOG_DEBUG << listTransactionIds.size();
- BMCWEB_LOG_DEBUG << "Data is present";
+ BMCWEB_LOG_DEBUG("{}", listTransactionIds.size());
+ BMCWEB_LOG_DEBUG("Data is present");
for (unsigned int listTransactionId : listTransactionIds)
{
- BMCWEB_LOG_DEBUG << listTransactionId;
+ BMCWEB_LOG_DEBUG("{}", listTransactionId);
}
// validate the request ids
@@ -561,7 +561,7 @@
if (!varReleaselock.first)
{
// validation Failed
- BMCWEB_LOG_ERROR << "handleReleaseLockAPI: validation failed";
+ BMCWEB_LOG_ERROR("handleReleaseLockAPI: validation failed");
asyncResp->res.result(boost::beast::http::status::bad_request);
return;
}
@@ -575,7 +575,7 @@
}
// valid rid, but the current hmc does not own all the locks
- BMCWEB_LOG_DEBUG << "Current HMC does not own all the locks";
+ BMCWEB_LOG_DEBUG("Current HMC does not own all the locks");
asyncResp->res.result(boost::beast::http::status::unauthorized);
auto var = statusRelease.second;
@@ -596,7 +596,7 @@
}
returnJson["SegmentFlags"] = myArray;
- BMCWEB_LOG_DEBUG << "handleReleaseLockAPI: lockrecord: " << returnJson;
+ BMCWEB_LOG_DEBUG("handleReleaseLockAPI: lockrecord: {}", returnJson);
asyncResp->res.jsonValue["Record"] = returnJson;
}
@@ -604,7 +604,7 @@
handleGetLockListAPI(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const ListOfSessionIds& listSessionIds)
{
- BMCWEB_LOG_DEBUG << listSessionIds.size();
+ BMCWEB_LOG_DEBUG("{}", listSessionIds.size());
auto status =
crow::ibm_mc_lock::Lock::getInstance().getLockList(listSessionIds);
@@ -647,7 +647,7 @@
{
if (fileName.empty())
{
- BMCWEB_LOG_ERROR << "Empty filename";
+ BMCWEB_LOG_ERROR("Empty filename");
res.jsonValue["Description"] = "Empty file path in the url";
return false;
}
@@ -658,7 +658,7 @@
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
if (found != std::string::npos)
{
- BMCWEB_LOG_ERROR << "Unsupported character in filename: " << fileName;
+ BMCWEB_LOG_ERROR("Unsupported character in filename: {}", fileName);
res.jsonValue["Description"] = "Unsupported character in filename";
return false;
}
@@ -666,9 +666,9 @@
// Check the filename length
if (fileName.length() > 20)
{
- BMCWEB_LOG_ERROR << "Name must be maximum 20 characters. "
- "Input filename length is: "
- << fileName.length();
+ BMCWEB_LOG_ERROR("Name must be maximum 20 characters. "
+ "Input filename length is: {}",
+ fileName.length());
res.jsonValue["Description"] = "Filename must be maximum 20 characters";
return false;
}
@@ -721,7 +721,7 @@
[](const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& fileName) {
- BMCWEB_LOG_DEBUG << "ConfigFile : " << fileName;
+ BMCWEB_LOG_DEBUG("ConfigFile : {}", fileName);
// Validate the incoming fileName
if (!isValidConfigFileName(fileName, asyncResp->res))
{
@@ -748,7 +748,7 @@
if (!redfish::json_util::readJsonAction(req, asyncResp->res, "Request",
body))
{
- BMCWEB_LOG_DEBUG << "Not a Valid JSON";
+ BMCWEB_LOG_DEBUG("Not a Valid JSON");
asyncResp->res.result(boost::beast::http::status::bad_request);
return;
}
@@ -779,8 +779,7 @@
}
else
{
- BMCWEB_LOG_DEBUG << " Value of Type : " << type
- << "is Not a Valid key";
+ BMCWEB_LOG_DEBUG(" Value of Type : {}is Not a Valid key", type);
redfish::messages::propertyValueNotInList(asyncResp->res, type,
"Type");
}