fix-logs 4.0 KB

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