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/dbus_monitor.hpp b/include/dbus_monitor.hpp
index 4ecc436..2682717 100644
--- a/include/dbus_monitor.hpp
+++ b/include/dbus_monitor.hpp
@@ -36,7 +36,7 @@
{
if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
{
- BMCWEB_LOG_ERROR << "Got sdbus error on match";
+ BMCWEB_LOG_ERROR("Got sdbus error on match");
return 0;
}
crow::websocket::Connection* connection =
@@ -44,7 +44,8 @@
auto thisSession = sessions.find(connection);
if (thisSession == sessions.end())
{
- BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
+ BMCWEB_LOG_ERROR("Couldn't find dbus connection {}",
+ logPtr(connection));
return 0;
}
sdbusplus::message_t message(m);
@@ -57,12 +58,12 @@
int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
if (r < 0)
{
- BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
+ BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
return 0;
}
if (!data.is_array())
{
- BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
+ BMCWEB_LOG_ERROR("No data in PropertiesChanged signal");
return 0;
}
@@ -76,13 +77,13 @@
int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
if (r < 0)
{
- BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
+ BMCWEB_LOG_ERROR("convertDBusToJSON failed with {}", r);
return 0;
}
if (!data.is_array())
{
- BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
+ BMCWEB_LOG_ERROR("No data in InterfacesAdded signal");
return 0;
}
@@ -98,8 +99,7 @@
}
else
{
- BMCWEB_LOG_CRITICAL << "message " << message.get_member()
- << " was unexpected";
+ BMCWEB_LOG_CRITICAL("message {} was unexpected", message.get_member());
return 0;
}
@@ -114,140 +114,140 @@
.privileges({{"Login"}})
.websocket()
.onopen([&](crow::websocket::Connection& conn) {
- BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
+ BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
sessions.try_emplace(&conn);
})
.onclose([&](crow::websocket::Connection& conn, const std::string&) {
sessions.erase(&conn);
})
- .onmessage(
- [&](crow::websocket::Connection& conn, const std::string& data,
- bool) {
- const auto sessionPair = sessions.find(&conn);
- if (sessionPair == sessions.end())
- {
- conn.close("Internal error");
- }
- DbusWebsocketSession& thisSession = sessionPair->second;
- BMCWEB_LOG_DEBUG << "Connection " << &conn << " received " << data;
- nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
- if (j.is_discarded())
- {
- BMCWEB_LOG_ERROR << "Unable to parse json data for monitor";
- conn.close("Unable to parse json request");
- return;
- }
- nlohmann::json::iterator interfaces = j.find("interfaces");
- if (interfaces != j.end())
- {
- thisSession.interfaces.reserve(interfaces->size());
- for (auto& interface : *interfaces)
+ .onmessage([&](crow::websocket::Connection& conn,
+ const std::string& data, bool) {
+ const auto sessionPair = sessions.find(&conn);
+ if (sessionPair == sessions.end())
{
- const std::string* str =
- interface.get_ptr<const std::string*>();
- if (str != nullptr)
+ conn.close("Internal error");
+ }
+ DbusWebsocketSession& thisSession = sessionPair->second;
+ BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data);
+ nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
+ if (j.is_discarded())
+ {
+ BMCWEB_LOG_ERROR("Unable to parse json data for monitor");
+ conn.close("Unable to parse json request");
+ return;
+ }
+ nlohmann::json::iterator interfaces = j.find("interfaces");
+ if (interfaces != j.end())
+ {
+ thisSession.interfaces.reserve(interfaces->size());
+ for (auto& interface : *interfaces)
{
- thisSession.interfaces.insert(*str);
+ const std::string* str =
+ interface.get_ptr<const std::string*>();
+ if (str != nullptr)
+ {
+ thisSession.interfaces.insert(*str);
+ }
}
}
- }
- nlohmann::json::iterator paths = j.find("paths");
- if (paths == j.end())
- {
- BMCWEB_LOG_ERROR << "Unable to find paths in json data";
- conn.close("Unable to find paths in json data");
- return;
- }
-
- size_t interfaceCount = thisSession.interfaces.size();
- if (interfaceCount == 0)
- {
- interfaceCount = 1;
- }
- // Reserve our matches upfront. For each path there is 1 for
- // interfacesAdded, and InterfaceCount number for
- // PropertiesChanged
- thisSession.matches.reserve(thisSession.matches.size() +
- paths->size() * (1U + interfaceCount));
-
- // These regexes derived on the rules here:
- // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
- static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
- static std::regex validInterface(
- "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
-
- for (const auto& thisPath : *paths)
- {
- const std::string* thisPathString =
- thisPath.get_ptr<const std::string*>();
- if (thisPathString == nullptr)
+ nlohmann::json::iterator paths = j.find("paths");
+ if (paths == j.end())
{
- BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
- conn.close();
+ BMCWEB_LOG_ERROR("Unable to find paths in json data");
+ conn.close("Unable to find paths in json data");
return;
}
- if (!std::regex_match(*thisPathString, validPath))
- {
- BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
- conn.close();
- return;
- }
- std::string propertiesMatchString =
- ("type='signal',"
- "interface='org.freedesktop.DBus.Properties',"
- "path_namespace='" +
- *thisPathString +
- "',"
- "member='PropertiesChanged'");
- // If interfaces weren't specified, add a single match for all
- // interfaces
- if (thisSession.interfaces.empty())
- {
- BMCWEB_LOG_DEBUG << "Creating match " << propertiesMatchString;
- thisSession.matches.emplace_back(
- std::make_unique<sdbusplus::bus::match_t>(
- *crow::connections::systemBus, propertiesMatchString,
- onPropertyUpdate, &conn));
- }
- else
+ size_t interfaceCount = thisSession.interfaces.size();
+ if (interfaceCount == 0)
{
- // If interfaces were specified, add a match for each
- // interface
- for (const std::string& interface : thisSession.interfaces)
+ interfaceCount = 1;
+ }
+ // Reserve our matches upfront. For each path there is 1 for
+ // interfacesAdded, and InterfaceCount number for
+ // PropertiesChanged
+ thisSession.matches.reserve(thisSession.matches.size() +
+ paths->size() * (1U + interfaceCount));
+
+ // These regexes derived on the rules here:
+ // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
+ static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
+ static std::regex validInterface(
+ "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
+
+ for (const auto& thisPath : *paths)
+ {
+ const std::string* thisPathString =
+ thisPath.get_ptr<const std::string*>();
+ if (thisPathString == nullptr)
{
- if (!std::regex_match(interface, validInterface))
- {
- BMCWEB_LOG_ERROR << "Invalid interface name "
- << interface;
- conn.close();
- return;
- }
- std::string ifaceMatchString = propertiesMatchString;
- ifaceMatchString += ",arg0='";
- ifaceMatchString += interface;
- ifaceMatchString += "'";
- BMCWEB_LOG_DEBUG << "Creating match " << ifaceMatchString;
+ BMCWEB_LOG_ERROR("subscribe path isn't a string?");
+ conn.close();
+ return;
+ }
+ if (!std::regex_match(*thisPathString, validPath))
+ {
+ BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString);
+ conn.close();
+ return;
+ }
+ std::string propertiesMatchString =
+ ("type='signal',"
+ "interface='org.freedesktop.DBus.Properties',"
+ "path_namespace='" +
+ *thisPathString +
+ "',"
+ "member='PropertiesChanged'");
+ // If interfaces weren't specified, add a single match for all
+ // interfaces
+ if (thisSession.interfaces.empty())
+ {
+ BMCWEB_LOG_DEBUG("Creating match {}",
+ propertiesMatchString);
+
thisSession.matches.emplace_back(
std::make_unique<sdbusplus::bus::match_t>(
- *crow::connections::systemBus, ifaceMatchString,
- onPropertyUpdate, &conn));
+ *crow::connections::systemBus,
+ propertiesMatchString, onPropertyUpdate, &conn));
}
+ else
+ {
+ // If interfaces were specified, add a match for each
+ // interface
+ for (const std::string& interface : thisSession.interfaces)
+ {
+ if (!std::regex_match(interface, validInterface))
+ {
+ BMCWEB_LOG_ERROR("Invalid interface name {}",
+ interface);
+ conn.close();
+ return;
+ }
+ std::string ifaceMatchString = propertiesMatchString;
+ ifaceMatchString += ",arg0='";
+ ifaceMatchString += interface;
+ ifaceMatchString += "'";
+ BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString);
+ thisSession.matches.emplace_back(
+ std::make_unique<sdbusplus::bus::match_t>(
+ *crow::connections::systemBus, ifaceMatchString,
+ onPropertyUpdate, &conn));
+ }
+ }
+ std::string objectManagerMatchString =
+ ("type='signal',"
+ "interface='org.freedesktop.DBus.ObjectManager',"
+ "path_namespace='" +
+ *thisPathString +
+ "',"
+ "member='InterfacesAdded'");
+ BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString);
+ thisSession.matches.emplace_back(
+ std::make_unique<sdbusplus::bus::match_t>(
+ *crow::connections::systemBus, objectManagerMatchString,
+ onPropertyUpdate, &conn));
}
- std::string objectManagerMatchString =
- ("type='signal',"
- "interface='org.freedesktop.DBus.ObjectManager',"
- "path_namespace='" +
- *thisPathString +
- "',"
- "member='InterfacesAdded'");
- BMCWEB_LOG_DEBUG << "Creating match " << objectManagerMatchString;
- thisSession.matches.emplace_back(
- std::make_unique<sdbusplus::bus::match_t>(
- *crow::connections::systemBus, objectManagerMatchString,
- onPropertyUpdate, &conn));
- }
});
}
} // namespace dbus_monitor