blob: 5a9c90f3b3d5781e0319e4579caea2edc7aa7198 [file] [log] [blame]
Arthur She5fc74272021-03-26 21:24:34 -07001#!/usr/bin/python3
2#
3# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import os
9import sys
10import yaml
11
12"""
13The whole messages will go into 'lava.log' and if there are 'feedback' level message,
14depends on the message namespace 'ns', the corresponding message will go into separated files
15 Message Separated log file
16 level, ns
17--------------------------------------------------------
18all lava.log
19lvl='target' lava-uart0.log
Arthur She5fc74272021-03-26 21:24:34 -070020lvl='feedback', ns='terminal_1' lava-uart1.log
21lvl='feedback', ns='terminal_2' lava-uart2.log
22lvl='feedback', ns='terminal_3' lava-uart3.log
Arthur She808ff552021-04-02 16:27:22 -070023anything else in lvl='feedback' feedback.log
24messages
Arthur She5fc74272021-03-26 21:24:34 -070025--------------------------------------------------------
26"""
27
28USAGE = f"Usage: {sys.argv[0]} /path/to/lava-job-plain-log.log"
29
30separated_log_file = {"all": "lava.log", \
31 "target": "lava-uart0.log", \
32 "feedback-terminal_1": "lava-uart1.log", \
33 "feedback-terminal_2": "lava-uart2.log", \
34 "feedback-terminal_3": "lava-uart3.log", \
35 "feedback": "feedback.log"}
36opened_logfile = dict()
37
38if __name__ == "__main__":
39 args = sys.argv[1:]
40 if not args:
41 raise SystemExit(USAGE)
42
43 plain_log = args[0]
44 des_dir = os.path.dirname(plain_log)
45 if len(des_dir) == 0:
46 des_dir = "."
47
48 if not os.path.exists(plain_log):
49 raise SystemExit("The file '{}' is not exist!!".format(plain_log))
50
51 with open(plain_log, "r") as job_log:
Arthur She2586b5c2021-05-11 21:00:20 -070052 try:
53 log_list = yaml.load(job_log, Loader=yaml.SafeLoader)
54 except yaml.YAMLError as exc:
55 print ("Error while parsing YAML file:")
56 if hasattr(exc, 'problem_mark'):
57 if exc.context != None:
58 print (' parser says\n' + str(exc.problem_mark) + '\n ' +
59 str(exc.problem) + ' ' + str(exc.context))
60 else:
61 print (' parser says\n' + str(exc.problem_mark) + '\n ' +
62 str(exc.problem))
63 else:
64 print ("Something went wrong while parsing yaml file")
65 sys.exit(1)
Arthur She5fc74272021-03-26 21:24:34 -070066 try:
67 full_test_log = "{}/{}".format(des_dir, separated_log_file["all"])
68 opened_logfile["all"] = open(full_test_log, "w")
69 for line in log_list:
70 level = line["lvl"]
71 msg = line["msg"]
72 dt = line["dt"]
73 if (level == "target") or (level == "feedback"):
74 log_file_id = level
75 if "ns" in line:
76 log_file_id = "{}-{}".format(log_file_id, line["ns"])
Arthur She808ff552021-04-02 16:27:22 -070077 if log_file_id not in separated_log_file:
78 log_file_id = "feedback"
Arthur She5fc74272021-03-26 21:24:34 -070079 if log_file_id not in opened_logfile:
80 des_log_file = "{}/{}".format(des_dir, separated_log_file[log_file_id])
81 opened_logfile[log_file_id] = open(des_log_file, "w")
82 try:
83 opened_logfile[log_file_id].write("{}\n".format(msg))
84 except UnicodeEncodeError:
85 msg = (
86 msg
87 .encode("ascii", errors="replace")
88 .decode("ascii")
89 )
90 opened_logfile[log_file_id].write("{}\n".format(msg))
91 # log to 'lava.log'
92 opened_logfile["all"].write("{} {}\n".format(dt.split(".")[0], msg))
93 except IOError as err:
94 print("File Error: " + str(err))
95
96 finally:
97 for log_file in opened_logfile:
98 opened_logfile[log_file].close()