ttfw: save console log to file

pull/6416/head
He Yin Ling 2020-11-12 20:44:44 +08:00
rodzic 16a488c405
commit 4e82540730
2 zmienionych plików z 25 dodań i 0 usunięć

Wyświetl plik

@ -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()

Wyświetl plik

@ -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()