summaryrefslogtreecommitdiffstats
path: root/src/osmo_gsm_tester/log.py
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-05-07 00:00:14 +0200
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-05-08 10:12:25 +0000
commit8f4f17437d8926b8692b46ac2caac94e86d39f87 (patch)
treec624e7a4bac78b24daec095cebe901dab01f9d76 /src/osmo_gsm_tester/log.py
parent1a2177cd1b84ff4e73f4e3c5775b8c46578831bb (diff)
log.py: add FileLogTarget
Will be used in a subsequent commit. Change-Id: Id3dfdeea236eb8ade5e6c80e64d5c3ce4de96b81
Diffstat (limited to 'src/osmo_gsm_tester/log.py')
-rw-r--r--src/osmo_gsm_tester/log.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/osmo_gsm_tester/log.py b/src/osmo_gsm_tester/log.py
index d3f2ea0..973fc1a 100644
--- a/src/osmo_gsm_tester/log.py
+++ b/src/osmo_gsm_tester/log.py
@@ -22,6 +22,7 @@ import sys
import time
import traceback
import contextlib
+import atexit
from inspect import getframeinfo, stack
from .util import is_dict
@@ -508,6 +509,30 @@ class TestsTarget(LogTarget):
super().__init__(log_write_func)
self.style(time=False, src=False)
+class FileLogTarget(LogTarget):
+ 'LogTarget to log to a file system path'
+ log_file = None
+
+ def __init__(self, log_path):
+ atexit.register(self.at_exit)
+ self.path = log_path
+ self.log_file = open(log_path, 'a')
+ super().__init__(self.write_to_log_and_flush)
+
+ def remove(self):
+ super().remove()
+ self.log_file.close()
+ self.log_file = None
+
+ def write_to_log_and_flush(self, msg):
+ self.log_file.write(msg)
+ self.log_file.flush()
+
+ def at_exit(self):
+ if self.log_file is not None:
+ self.log_file.flush()
+ self.log_file.close()
+
def run_logging_exceptions(func, *func_args, return_on_failure=None, **func_kwargs):
try:
return func(*func_args, **func_kwargs)