Skip to content

Commit

Permalink
Merge bitcoin#14051: [Tests] Make combine_logs.py handle multi-line logs
Browse files Browse the repository at this point in the history
16e288a test padding non micro timestamps (John Newbery)
995dd89 [Tests] Make combine_logs.py handle multi-line logs (John Newbery)

Pull request description:

  combine_logs.py currently inserts additional newlines into multi-line
  log messages, and doesn't color them properly. Fix both of those.

Tree-SHA512: dbe2f3ecc7cfbc95ee4350e648d127538c79cb6555257d4aeec12fe3d159366742b68e90e620c8ed7219a44b973395c7e5929ba374fae115fbee25560db645f6
  • Loading branch information
laanwj authored and PastaPastaPasta committed Jun 29, 2021
1 parent de43330 commit c8ffdd2
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions test/functional/combine_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys

# Matches on the date format at the start of the log event
TIMESTAMP_PATTERN = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z")
TIMESTAMP_PATTERN = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{6})?Z")

LogEvent = namedtuple('LogEvent', ['timestamp', 'source', 'event'])

Expand Down Expand Up @@ -84,11 +84,17 @@ def get_log_events(source, logfile):
if time_match:
if event:
yield LogEvent(timestamp=timestamp, source=source, event=event.rstrip())
event = line
timestamp = time_match.group()
if time_match.group(1) is None:
# timestamp does not have microseconds. Add zeroes.
timestamp_micro = timestamp.replace("Z", ".000000Z")
line = line.replace(timestamp, timestamp_micro)
timestamp = timestamp_micro
event = line
# if it doesn't have a timestamp, it's a continuation line of the previous log.
else:
event += "\n" + line
# Add the line. Prefix with space equivalent to the source + timestamp so log lines are aligned
event += " " + line
# Flush the final event
yield LogEvent(timestamp=timestamp, source=source, event=event.rstrip())
except FileNotFoundError:
Expand All @@ -107,7 +113,11 @@ def print_logs(log_events, color=False, html=False):
colors["reset"] = "\033[0m" # Reset font color

for event in log_events:
print("{0} {1: <5} {2} {3}".format(colors[event.source.rstrip()], event.source, event.event, colors["reset"]))
lines = event.event.splitlines()
print("{0} {1: <5} {2} {3}".format(colors[event.source.rstrip()], event.source, lines[0], colors["reset"]))
if len(lines) > 1:
for line in lines[1:]:
print("{0}{1}{2}".format(colors[event.source.rstrip()], line, colors["reset"]))

else:
try:
Expand Down

0 comments on commit c8ffdd2

Please sign in to comment.