clean_logs.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import glob, re, os.path, textwrap
  2. from colorama import Fore, Back, Style
  3. def target_prefix(dir):
  4. if dir.startswith("net"):
  5. return "net"
  6. elif dir.startswith("serial"):
  7. return "serial"
  8. elif dir.startswith("util"):
  9. return "util"
  10. elif dir.startswith("runtime"):
  11. return "runtime"
  12. elif dir.startswith("zk"):
  13. return "zk"
  14. elif dir.startswith("raft"):
  15. return "raft"
  16. elif dir.startswith("sdk/src/crypto"):
  17. return "sdk::crypto"
  18. elif dir.startswith("sdk"):
  19. return "sdk"
  20. elif dir.startswith("contract/dao"):
  21. return "dao"
  22. elif dir.startswith("contract/money"):
  23. return "money"
  24. elif dir.startswith("rpc"):
  25. return "rpc"
  26. elif dir.startswith("system"):
  27. return "system"
  28. elif dir.startswith("dht"):
  29. return "dht"
  30. elif dir.startswith("consensus"):
  31. return "consensus"
  32. elif dir.startswith("zkas"):
  33. return "zkas"
  34. elif dir.startswith("blockchain"):
  35. return "blockchain"
  36. elif dir.startswith("wallet"):
  37. return "wallet"
  38. else:
  39. assert not dir or dir == "tx"
  40. return ""
  41. def target_suffix(prefix, base):
  42. if prefix in ("dao", "money"):
  43. # Just shorten the target to simply "dao" or "money"
  44. # We don't need the fine grained details
  45. return ""
  46. elif base in ("mod.rs", "lib.rs"):
  47. # Just use the module name as the target with these files
  48. return ""
  49. # Otherwise just use the filename as the target suffix
  50. return base.removesuffix(".rs")
  51. def log_target(fname):
  52. dir, base = os.path.dirname(fname), os.path.basename(fname)
  53. prefix = target_prefix(dir)
  54. suffix = target_suffix(prefix, base)
  55. # you don't need :: when the suffix is empty
  56. if not suffix and not prefix:
  57. return ""
  58. if not suffix:
  59. return prefix
  60. if not prefix:
  61. return suffix
  62. return f"{prefix}::{suffix}"
  63. def replace(fname, contents):
  64. target = log_target(fname)
  65. # You can debug like this:
  66. #if target != "consensus::proposal":
  67. # return ""
  68. print(f"Replacing {target}" + " "*(40 - len(target)) + f"[{fname}]")
  69. result = ""
  70. lines = contents.split("\n")
  71. i = 0
  72. while i < len(lines):
  73. line = lines[i]
  74. # only used for debug output
  75. old_text = None
  76. new_text = None
  77. # This is used as a debug goto
  78. is_modified = False
  79. log_level = None
  80. if "trace!(" in line:
  81. log_level = "trace"
  82. elif "debug!(" in line:
  83. log_level = "debug"
  84. elif "info!(" in line:
  85. log_level = "info"
  86. elif "warn!(" in line:
  87. log_level = "warn"
  88. elif "error!(" in line:
  89. log_level = "error"
  90. if log_level is not None:
  91. # No target exists for this file at all. Just ignore these
  92. # We would normally delete any target set for these files
  93. # but so far we have none of them, so just ignore them.
  94. if not target:
  95. print(
  96. " "
  97. + Back.RED + "Skip [no target]:" + Style.RESET_ALL
  98. + f" {line}"
  99. )
  100. # Single lines with a target that's a constant or string
  101. elif re.search(f'{log_level}!\\(target: ([A-Z_]+|"[a-zA-Z:_-]+"),', line):
  102. old_text = f"{i}: {line}"
  103. line = re.sub(
  104. 'target: ([A-Z_]+|"[a-zA-Z:_-]+"),',
  105. f'target: "{target}",',
  106. line
  107. )
  108. is_modified = True
  109. new_text = f"{i}: {line}"
  110. # Normal single lines with no target set
  111. elif f'{log_level}!("' in line:
  112. old_text = f"{i}: {line}"
  113. #print(f" No target: {line}")
  114. line = line.replace(f'{log_level}!(',
  115. f'{log_level}!(target: "{target}", ')
  116. is_modified = True
  117. new_text = f"{i}: {line}"
  118. # Multiline logs
  119. # We read the next line and check if there's a target set or not
  120. else:
  121. old_text = f"{i}: {line}"
  122. new_text = f"{i}: {line}"
  123. result += line + "\n"
  124. i += 1
  125. assert i < len(lines)
  126. line = lines[i]
  127. old_text += f"\n{i}: {line}"
  128. # Constants or target strings set
  129. if re.search('target: ([A-Z_]+|"[a-zA-Z:_-]+"),', line):
  130. line = re.sub(
  131. 'target: ([A-Z_]+|"[a-zA-Z:_-]+"),',
  132. f'target: "{target}",',
  133. line
  134. )
  135. new_text += f"\n{i}: {line}"
  136. # Multi-line logs with no target set
  137. # Insert an extra line with the target
  138. else:
  139. leading_space = lambda line: len(line) - len(line.lstrip())
  140. added_line = (" "*leading_space(line)
  141. + f'target: "{target}",')
  142. result += f"{added_line}\n"
  143. new_text += f"\n{i}: {added_line}\n{i + 1}: {line}"
  144. is_modified = True
  145. if is_modified:
  146. assert old_text is not None and new_text is not None
  147. print(
  148. Fore.RED
  149. + textwrap.indent(old_text, " < ")
  150. + Style.RESET_ALL
  151. )
  152. print(
  153. Fore.GREEN
  154. + textwrap.indent(new_text, " > ")
  155. + Style.RESET_ALL
  156. )
  157. print()
  158. result += f"{line}\n"
  159. i += 1
  160. return result
  161. def main():
  162. for fname in glob.glob("**/*.rs", root_dir="src/", recursive=True):
  163. with open(f"src/{fname}", "r") as f:
  164. contents = f.read()
  165. contents = replace(fname, contents)
  166. # Doesn't write anything yet
  167. #with open(fname, "w") as f:
  168. # f.write(contents)
  169. if __name__ == "__main__":
  170. main()