Merge #15841: [test] combine_logs: append node stderr and stdout if it exists

fa90a89eee [test] combine_logs: append node stderr and stdout if it exists (MarcoFalke)

Pull request description:

  See issue:

  * tests: bitcoind stdout and error should be passed to the logger #13519

ACKs for commit fa90a8:
  laanwj:
    utACK fa90a89eeefcc362970216d95973ad01a21366ed

Tree-SHA512: 39c4596e2e133c9011ab01bc4dc24e884d0a8cce7a67d3765f17c288d3ffbd438e1ff6016d0f817a981b27fce17fa77a1ff56787ddb1ea55123ce9ecffb44c08
This commit is contained in:
MarcoFalke 2019-05-01 08:37:19 -04:00 committed by Dzutte
parent 772dad995c
commit 0d58cdabb7

View File

@ -12,6 +12,7 @@ import glob
import heapq import heapq
import itertools import itertools
import os import os
import pathlib
import re import re
import sys import sys
import tempfile import tempfile
@ -52,9 +53,23 @@ def main():
if not args.testdir: if not args.testdir:
print("Opening latest test directory: {}".format(testdir), file=sys.stderr) print("Opening latest test directory: {}".format(testdir), file=sys.stderr)
colors = defaultdict(lambda: '')
if args.color:
colors["test"] = "\033[0;36m" # CYAN
colors["node0"] = "\033[0;34m" # BLUE
colors["node1"] = "\033[0;32m" # GREEN
colors["node2"] = "\033[0;31m" # RED
colors["node3"] = "\033[0;33m" # YELLOW
colors["reset"] = "\033[0m" # Reset font color
log_events = read_logs(testdir) log_events = read_logs(testdir)
print_logs(log_events, color=args.color, html=args.html) if args.html:
print_logs_html(log_events)
else:
print_logs_plain(log_events, colors)
print_node_warnings(testdir, colors)
def read_logs(tmp_dir): def read_logs(tmp_dir):
"""Reads log files. """Reads log files.
@ -80,6 +95,26 @@ def read_logs(tmp_dir):
return heapq.merge(*[get_log_events(source, f) for source, f in files]) return heapq.merge(*[get_log_events(source, f) for source, f in files])
def print_node_warnings(tmp_dir, colors):
"""Print nodes' errors and warnings"""
warnings = []
for stream in ['stdout', 'stderr']:
for i in itertools.count():
folder = "{}/node{}/{}".format(tmp_dir, i, stream)
if not os.path.isdir(folder):
break
for (_, _, fns) in os.walk(folder):
for fn in fns:
warning = pathlib.Path('{}/{}'.format(folder, fn)).read_text().strip()
if warning:
warnings.append(("node{} {}".format(i, stream), warning))
print()
for w in warnings:
print("{} {} {} {}".format(colors[w[0].split()[0]], w[0], w[1], colors["reset"]))
def find_latest_test_dir(): def find_latest_test_dir():
"""Returns the latest tmpfile test directory prefix.""" """Returns the latest tmpfile test directory prefix."""
tmpdir = tempfile.gettempdir() tmpdir = tempfile.gettempdir()
@ -136,18 +171,9 @@ def get_log_events(source, logfile):
except FileNotFoundError: except FileNotFoundError:
print("File %s could not be opened. Continuing without it." % logfile, file=sys.stderr) print("File %s could not be opened. Continuing without it." % logfile, file=sys.stderr)
def print_logs(log_events, color=False, html=False):
"""Renders the iterator of log events into text or html."""
if not html:
colors = defaultdict(lambda: '')
if color:
colors["test"] = "\033[0;36m" # CYAN
colors["node0"] = "\033[0;34m" # BLUE
colors["node1"] = "\033[0;32m" # GREEN
colors["node2"] = "\033[0;31m" # RED
colors["node3"] = "\033[0;33m" # YELLOW
colors["reset"] = "\033[0m" # Reset font color
def print_logs_plain(log_events, colors):
"""Renders the iterator of log events into text."""
for event in log_events: for event in log_events:
lines = event.event.splitlines() lines = event.event.splitlines()
print("{0} {1: <5} {2} {3}".format(colors[event.source.rstrip()], event.source, lines[0], colors["reset"])) print("{0} {1: <5} {2} {3}".format(colors[event.source.rstrip()], event.source, lines[0], colors["reset"]))
@ -155,7 +181,9 @@ def print_logs(log_events, color=False, html=False):
for line in lines[1:]: for line in lines[1:]:
print("{0}{1}{2}".format(colors[event.source.rstrip()], line, colors["reset"])) print("{0}{1}{2}".format(colors[event.source.rstrip()], line, colors["reset"]))
else:
def print_logs_html(log_events):
"""Renders the iterator of log events into html."""
try: try:
import jinja2 import jinja2
except ImportError: except ImportError: