Debian Codegraph @

misc/scripts/logging/fix-logs

raw
1 #!/usr/bin/perl -w
2 #
3 # Usage: fix-log [MODULE-NAME] < INFILE > OUTFILE
4 #
5 # Fixes logging macros and messages in the SER source file INFILE
6 # to match the recent updates in the logging API.
7 #
8 # Specify MODULE_NAME if INFILE source file is a part of a SER module.
9 #
10 # See doc/logging-api.txt for details.
11 #
12 # $Id$
13
14 #
15 # What *exactly* does this script do?
16 #
17 # - replaces LOG(L_*, ...) with the short macro corresponding to L_* level
18 #
19 # - replaces DEBUG() with DBG() macro
20 #
21 # - removes MODULE and the level string prefixes from FMT arguments of macros
22 # where FMT looks like "X:...", "X:Y:..." or "X:Y:Z:...", white spaces are
23 # ignored and preserved, string matching is case-insensitive
24 #
25 # In addition, if the level string found in FMT argument doesn't match the actual
26 # level of the macro, the macro level is fixed.
27 #
28 # Examples:
29 # ERR("ERROR:tm: blah\n") becomes ERR("blah\n")
30 # DBG("Debug: blah\n") becomes DBG("blah\n")
31 # LOG(L_ERR, "tm: INFO: blah\n") becomes INFO("blah\n")
32 #
33 # - removes 'MODULE_NAME ":' string from the beggining of FMT arguments of macros
34 # in module source files (a common special case)
35 #
36 # Example:
37 # LOG(L_ERR, MODULE_NAME ": tm:Info:blah\n") becomes INFO("blah\n")
38 #
39
40 # Map a text string to L_* log level macro
41 my %text2level = (
42 "BUG" => "L_CRIT",
43 "CRIT" => "L_CRIT",
44 "CRITICAL" => "L_CRIT",
45
46 "ALERT" => "L_ALERT",
47
48 "ERR" => "L_ERR",
49 "ERROR" => "L_ERR",
50
51 "WARN" => "L_WARN",
52 "WARNING" => "L_WARN",
53
54 "NOTICE" => "L_NOTICE",
55
56 "INFO" => "L_INFO",
57
58 "DBG" => "L_DBG",
59 "DEBUG" => "L_DBG",
60 );
61
62 #
63 short2level
64
65
66 # Strip the leading and trailing whitespaces and upper-case a text
67 sub norm {
68 my $text = ($_[0] || "");
69 $text =~ s/^\s*//;
70 $text =~ s/\s*$//;
71 uc($text);
72 }
73
74 my $module_name = norm($ARGV[0]);
75
76 sub fix_log_prefix {
77 my ($prefix, $level) = ($_[0], $_[1]);
78
79 # delete prefix if it contains module name
80 if ($module_name) {
81 if (!$text || (norm($text) eq $module_name)) {
82 return ("", $level);
83 }
84 }
85
86 # delete prefix if it contains text level
87 my $prefix_level = $text2level{norm($prefix)};
88 if ($prefix_level) {
89 $prefix = "";
90
91 # change level if does not match prefix level
92 if ($level =~ /^L_(DBG|INFO|NOTICE|WARN|ERR|CRIT|ALERT)$/ &&
93 $level ne $prefix_level) {
94 return ("", $prefix_level);
95 }
96 }
97
98 return ($prefix . ":", $level);
99 }
100
101 sub fix_log {
102 my $level = $_[0];
103 my $prefix1 = $_[1];
104 my $prefix2 = $_[2];
105 my $prefix3 = $_[3];
106 my $space = $_[4];
107
108 ($prefix1, $level) = fix_log_prefix($prefix1, $level) if $prefix1;
109 ($prefix2, $level) = fix_log_prefix($prefix2, $level) if $prefix2;
110 ($prefix3, $level) = fix_log_prefix($prefix3, $level) if $prefix3;
111
112 my $prefix = $prefix1 . $prefix2 . $prefix3 . $space;
113 $prefix =~ s/^\s*//;
114
115 "LOG($level, \"$prefix";
116 }
117
118 while (<STDIN>) {
119 AGAIN:
120 # replace DEBUG() by DBG()
121 s/DEBUG\(/DBG\(/g;
122
123 # ...in case the statement spans more lines
124 if (/(DBG|INFO|NOTICE|WARN|ERR|BUG|ALERT)\(\s*$/ || /LOG\(([^,]*,)?\s*$/) {
125 $_ .= <STDIN>;
126 goto AGAIN;
127 }
128
129 # one common special case used in several modules
130 if ($module_name) {
131 s/LOG\(\s*([^,]+)\s*,\s*MODULE_NAME\s*"\s*:\s*/LOG($1, "/g;
132 s/(DBG|INFO|NOTICE|WARN|ERR|BUG|ALERT)\(\s*MODULE_NAME\s*"\s*:\s*/$1("/g;
133 }
134
135 # module name and level prefix removing magic, may change macro level
136 # if different one found in the message
137 $id='\s*[a-zA-Z0-9_]+\s*';
138
139 s/LOG\(\s*(([A-Z_]+)|[^,]+)\s*,\s*"($id):(($id):(($id):)?)?(\s*)/
140 fix_log($1, $3, $5, $7, $8);
141 /eg;
142
143 s/(DBG|INFO|NOTICE|WARN|ERR|BUG|ALERT)\(\s*"($id):(($id):(($id):)?)?(\s*)/
144 $1 = "CRIT" if $1 eq "BUG";
145 fix_log("L_$1", $2, $4, $6, $7);
146 /eg;
147
148 # prefer shorter static-level macros
149 s/LOG\(\s*L_(DBG|INFO|NOTICE|WARN|ERR|CRIT|ALERT)\s*,\s*/
150 $1 = "BUG" if $1 eq "CRIT";
151 "$1\(";
152 /eg;
153
154 print;
155 }