summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorEd Tanous <ed@tanous.net>2023-07-18 03:06:25 +0300
committerEd Tanous <ed@tanous.net>2023-07-20 01:38:41 +0300
commit62598e31d0988d589506d5091bd38f72d61faf5e (patch)
treee3e548632da934083c21cc1262f8b9d8f255f2a9 /scripts
parent609ba4c9ffb9c6b83861ff557108c89007ca369a (diff)
downloadbmcweb-62598e31d0988d589506d5091bd38f72d61faf5e.tar.xz
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
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/replace_logs.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/scripts/replace_logs.py b/scripts/replace_logs.py
new file mode 100755
index 0000000000..cc7f87ba67
--- /dev/null
+++ b/scripts/replace_logs.py
@@ -0,0 +1,100 @@
+#!/usr/bin/python3
+import os
+import re
+from subprocess import check_output
+
+"""
+This script is intended to aid in the replacement of bmcweb log lines from
+BMCWEB_LOG_CRITICAL << "foo" << arg;
+to the new fmt style form
+BMCWEB_LOG_CRITICAL("foo{}", arg);
+
+It is intended to be a 99% correct tool, and in some cases, some amount of
+manual intervention is required once the conversion has been done. Some
+things it doesn't handle:
+
+ptrs need to be wrapped in logPtr(). This script tries to do the common
+cases, but can't handle every one.
+
+types of boost::ip::tcp::endpoint/address needs .to_string() called on it
+in the args section
+
+arguments that were previously build by in-line operator+ construction of
+strings (for example "foo" + bar), need to be formatted in the arguments
+brace replacement language, (for example "foo{}", bar)
+
+After the script has been run, remember to reformat with clang-format
+
+This script will be removed Q2 2024
+"""
+
+
+SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
+
+files = check_output(
+ ["git", "-C", os.path.join(SCRIPT_DIR, ".."), "grep", "-l", "BMCWEB_LOG_"]
+)
+filenames = files.split(b"\n")
+
+
+for filename in filenames:
+ if not filename:
+ continue
+ with open(filename, "r") as filehandle:
+ contents = filehandle.read()
+
+ # Normalize all the log statements to a single line
+ new_contents = ""
+ replacing = False
+ for line in contents.splitlines():
+ match = re.match(r"\s+BMCWEB_LOG_", line)
+ if match:
+ replacing = True
+
+ if replacing and not match:
+ line = " " + line.lstrip()
+ if line.endswith(";"):
+ replacing = False
+ new_contents += line
+ if not replacing:
+ new_contents += "\n"
+ contents = new_contents
+
+ new_contents = ""
+ for line in contents.splitlines():
+ match = re.match(r"(\s+BMCWEB_LOG_[A-Z]+) <<", line)
+ if match:
+ logstring = ""
+ line = line.lstrip()
+ chevron_split = line.split("<<")
+ arguments = []
+ for log_piece in chevron_split[1:]:
+ if log_piece.endswith(";"):
+ log_piece = log_piece[:-1]
+ log_piece = log_piece.strip()
+ if (log_piece.startswith('"') and log_piece.endswith('"')) or (
+ log_piece.startswith("'") and log_piece.endswith("'")
+ ):
+ logstring += log_piece[1:-1]
+ else:
+ if log_piece == "this" or log_piece.startswith("&"):
+ log_piece = "logPtr({})".format(log_piece)
+ if log_piece == "self":
+ log_piece = "logPtr(self.get())"
+ arguments.append(log_piece)
+ logstring += "{}"
+ if logstring.endswith("\\n"):
+ logstring = logstring[:-2]
+
+ arguments.insert(0, '"' + logstring + '"')
+ argjoin = ", ".join(arguments)
+
+ line = match.group(1) + "(" + argjoin + ");"
+ new_contents += line + "\n"
+
+ contents = new_contents
+
+ with open(filename, "w") as filehandle:
+ filehandle.write(new_contents)
+
+print()