MCPcopy Index your code
hub / github.com/bugy/script-server / ScriptOutputLogger

Class ScriptOutputLogger

src/execution/logging.py:25–97  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23
24
25class ScriptOutputLogger:
26 def __init__(self, log_file_path, output_stream):
27 self.opened = False
28 self.closed = False
29 self.output_stream = output_stream
30
31 self.log_file_path = log_file_path
32 self.log_file = None
33 self.close_callback = None
34
35 def start(self):
36 self._ensure_file_open()
37
38 self.output_stream.subscribe(self)
39
40 def _ensure_file_open(self):
41 if self.opened:
42 return
43
44 try:
45 self.log_file = open(self.log_file_path, 'wb')
46 except:
47 LOGGER.exception("Couldn't create a log file")
48
49 self.opened = True
50
51 def __log(self, text):
52 if not self.opened:
53 LOGGER.exception('Attempt to write to not opened logger')
54 return
55
56 if not self.log_file:
57 return
58
59 try:
60 if text is not None:
61 self.log_file.write(text.encode(ENCODING))
62 self.log_file.flush()
63 except:
64 LOGGER.exception("Couldn't write to the log file")
65
66 def _close(self):
67 try:
68 if self.log_file:
69 self.log_file.close()
70 except:
71 LOGGER.exception("Couldn't close the log file")
72
73 self.closed = True
74
75 if self.close_callback:
76 self.close_callback()
77
78 def on_next(self, output):
79 self.__log(output)
80
81 def on_close(self):
82 self._close()

Callers 2

create_loggerMethod · 0.90
start_loggingMethod · 0.85

Calls

no outgoing calls

Tested by 1

create_loggerMethod · 0.72