-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
36 lines (27 loc) · 825 Bytes
/
debug.py
File metadata and controls
36 lines (27 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import time
import datetime
from contextlib import contextmanager
from queue import Queue
from threading import Thread
_LOG_THREAD = None
_LOG_QUEUE = Queue()
@contextmanager
def log_time(msg):
start = time.time()
yield
log_msg('elapsed: %.03f msg: %s' % (time.time() - start, msg))
def log_msg(msg):
_LOG_QUEUE.put(msg)
def setup_logging(log_path):
global _LOG_THREAD
if _LOG_THREAD is not None:
return
_LOG_THREAD = Thread(target=log_loop, args=[log_path], daemon=True)
_LOG_THREAD.start()
def log_loop(log_path):
with open(log_path, 'w') as logfile:
while True:
entry = _LOG_QUEUE.get(block=True)
timestamp = datetime.datetime.now().isoformat()[:19]
logfile.write('%s: %s\n' % (timestamp, entry))
logfile.flush()