Review at  https://gerrit.osmocom.org/2508

log.py: add FileLogTarget

Will be used in a subsequent commit.

Change-Id: Id3dfdeea236eb8ade5e6c80e64d5c3ce4de96b81
---
M src/osmo_gsm_tester/log.py
1 file changed, 25 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/08/2508/1

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 time
 import traceback
 import contextlib
+import atexit
 from inspect import getframeinfo, stack
 
 from .util import is_dict
@@ -508,6 +509,30 @@
         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)

-- 
To view, visit https://gerrit.osmocom.org/2508
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id3dfdeea236eb8ade5e6c80e64d5c3ce4de96b81
Gerrit-PatchSet: 1
Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofm...@sysmocom.de>

Reply via email to