summaryrefslogtreecommitdiffstats
path: root/scripts/lib/scriptutils.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/scriptutils.py')
-rw-r--r--scripts/lib/scriptutils.py43
1 files changed, 41 insertions, 2 deletions
diff --git a/scripts/lib/scriptutils.py b/scripts/lib/scriptutils.py
index e7e7021c24..c573dc7f67 100644
--- a/scripts/lib/scriptutils.py
+++ b/scripts/lib/scriptutils.py
@@ -16,12 +16,51 @@ import string
16import subprocess 16import subprocess
17import sys 17import sys
18import tempfile 18import tempfile
19import threading
19import importlib 20import importlib
20from importlib import machinery 21from importlib import machinery
21 22
22def logger_create(name, stream=None): 23class KeepAliveStreamHandler(logging.StreamHandler):
24 def __init__(self, keepalive=True, **kwargs):
25 super().__init__(**kwargs)
26 if keepalive is True:
27 keepalive = 5000 # default timeout
28 self._timeout = threading.Condition()
29 self._stop = False
30
31 # background thread waits on condition, if the condition does not
32 # happen emit a keep alive message
33 def thread():
34 while not self._stop:
35 with self._timeout:
36 if not self._timeout.wait(keepalive):
37 self.emit(logging.LogRecord("keepalive", logging.INFO,
38 None, None, "Keepalive message", None, None))
39
40 self._thread = threading.Thread(target = thread, daemon = True)
41 self._thread.start()
42
43 def close(self):
44 # mark the thread to stop and notify it
45 self._stop = True
46 with self._timeout:
47 self._timeout.notify()
48 # wait for it to join
49 self._thread.join()
50 super().close()
51
52 def emit(self, record):
53 super().emit(record)
54 # trigger timer reset
55 with self._timeout:
56 self._timeout.notify()
57
58def logger_create(name, stream=None, keepalive=None):
23 logger = logging.getLogger(name) 59 logger = logging.getLogger(name)
24 loggerhandler = logging.StreamHandler(stream=stream) 60 if keepalive is not None:
61 loggerhandler = KeepAliveStreamHandler(stream=stream, keepalive=keepalive)
62 else:
63 loggerhandler = logging.StreamHandler(stream=stream)
25 loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) 64 loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
26 logger.addHandler(loggerhandler) 65 logger.addHandler(loggerhandler)
27 logger.setLevel(logging.INFO) 66 logger.setLevel(logging.INFO)