-def logtail(logcollection, logger, header, maxlen=25):
- logtail = deque([], maxlen*len(logcollection))
- containersapi = ("crunch-run.txt" in logcollection)
-
- for log in logcollection.keys():
- if not containersapi or log in ("crunch-run.txt", "stdout.txt", "stderr.txt"):
- logname = log[:-4]
- with logcollection.open(log) as f:
- for l in f:
- if containersapi:
- g = timestamp_re.match(l)
- logtail.append("%s %s %s" % (g.group(1), logname, g.group(2)))
- elif not crunchstat_re.match(l):
- logtail.append(l)
- if len(logcollection) > 1:
- logtail = sorted(logtail)[-maxlen:]
- logtxt = "\n ".join(l.strip() for l in logtail)
- logger.info(header)
- logger.info("\n %s", logtxt)
+def logtail(logcollection, logfunc, header, maxlen=25, include_crunchrun=True):
+ if len(logcollection) == 0:
+ logfunc("%s\n%s", header, " ** log is empty **")
+ return
+
+ mergelogs = {}
+ logfiles = ["stdout.txt", "stderr.txt"]
+
+ if include_crunchrun:
+ logfiles.append("crunch-run.txt")
+
+ for log in logfiles:
+ if log not in logcollection:
+ continue
+ logname = log[:-4] # trim off the .txt
+ logt = deque([], maxlen)
+ mergelogs[logname] = logt
+ with logcollection.open(log, encoding="utf-8") as f:
+ for l in f:
+ g = timestamp_re.match(l)
+ logt.append((g.group(1), g.group(2)))
+
+ keys = list(mergelogs)
+ loglines = []
+
+ # we assume the log lines are all in order so this this is a
+ # straight linear merge where we look at the next timestamp of
+ # each log and take whichever one is earliest.
+ while True:
+ earliest = None
+ for k in keys:
+ if mergelogs[k]:
+ if earliest is None or mergelogs[k][0][0] < mergelogs[earliest][0][0]:
+ earliest = k
+ if earliest is None:
+ break
+ ts, msg = mergelogs[earliest].popleft()
+ loglines.append("%s %s %s" % (ts, earliest, msg))
+ loglines = loglines[-maxlen:]
+
+ logtxt = "\n ".join(l.strip() for l in loglines)
+ logfunc("%s\n\n %s\n", header, logtxt)