From 4e825407300e98c753177e9c45bc6f240c5aa259 Mon Sep 17 00:00:00 2001 From: He Yin Ling Date: Thu, 12 Nov 2020 20:44:44 +0800 Subject: [PATCH] ttfw: save console log to file --- tools/ci/python_packages/tiny_test_fw/Env.py | 3 +++ .../tiny_test_fw/Utility/__init__.py | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tools/ci/python_packages/tiny_test_fw/Env.py b/tools/ci/python_packages/tiny_test_fw/Env.py index 90d84e3405..9752c29ffa 100644 --- a/tools/ci/python_packages/tiny_test_fw/Env.py +++ b/tools/ci/python_packages/tiny_test_fw/Env.py @@ -44,6 +44,7 @@ class Env(object): :keyword env_config_file: test env config file path :keyword test_name: test suite name, used when generate log folder name """ + CURRENT_LOG_FOLDER = "" def __init__(self, app=None, @@ -59,6 +60,8 @@ class Env(object): if not os.path.exists(self.log_path): os.makedirs(self.log_path) + Env.CURRENT_LOG_FOLDER = self.log_path + self.allocated_duts = dict() self.lock = threading.RLock() diff --git a/tools/ci/python_packages/tiny_test_fw/Utility/__init__.py b/tools/ci/python_packages/tiny_test_fw/Utility/__init__.py index 75b85f6a28..026674d5c5 100644 --- a/tools/ci/python_packages/tiny_test_fw/Utility/__init__.py +++ b/tools/ci/python_packages/tiny_test_fw/Utility/__init__.py @@ -1,7 +1,9 @@ from __future__ import print_function import os.path import sys +import time +from .. import Env _COLOR_CODES = { "white": u'\033[0m', @@ -19,6 +21,19 @@ _COLOR_CODES = { } +def _get_log_file_name(): + if Env.Env.CURRENT_LOG_FOLDER: + file_name = os.path.join(Env.Env.CURRENT_LOG_FOLDER, "console.log") + else: + raise OSError("env log folder does not exist, will not save to log file") + return file_name + + +def format_timestamp(): + ts = time.time() + return "{}:{}".format(time.strftime("%m-%d %H:%M:%S", time.localtime(ts)), str(ts % 1)[2:5]) + + def console_log(data, color="white", end="\n"): """ log data to console. @@ -37,6 +52,13 @@ def console_log(data, color="white", end="\n"): # reset color to white for later logs print(_COLOR_CODES["white"] + u"\r") sys.stdout.flush() + log_data = "[{}] ".format(format_timestamp()) + data + try: + log_file = _get_log_file_name() + with open(log_file, "a+") as f: + f.write(log_data + end) + except OSError: + pass __LOADED_MODULES = dict()