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