summaryrefslogtreecommitdiff
path: root/scripts/replace_logs.py
blob: cc7f87ba67f7f6f5d429ed0fba87d7acb6b89948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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()