MCPcopy Create free account
hub / github.com/apple/ml-streambridge / StreamToLogger

Class StreamToLogger

streambridge/utils.py:83–114  ·  view source on GitHub ↗

Fake file-like stream object that redirects writes to a logger instance.

Source from the content-addressed store, hash-verified

81
82
83class StreamToLogger(object):
84 """
85 Fake file-like stream object that redirects writes to a logger instance.
86 """
87
88 def __init__(self, logger, log_level=logging.INFO):
89 self.terminal = sys.stdout
90 self.logger = logger
91 self.log_level = log_level
92 self.linebuf = ""
93
94 def __getattr__(self, attr):
95 return getattr(self.terminal, attr)
96
97 def write(self, buf):
98 temp_linebuf = self.linebuf + buf
99 self.linebuf = ""
100 for line in temp_linebuf.splitlines(True):
101 # From the io.TextIOWrapper docs:
102 # On output, if newline is None, any '\n' characters written
103 # are translated to the system default line separator.
104 # By default sys.stdout.write() expects '\n' newlines and then
105 # translates them so this is still cross platform.
106 if line[-1] == "\n":
107 self.logger.log(self.log_level, line.rstrip())
108 else:
109 self.linebuf += line
110
111 def flush(self):
112 if self.linebuf != "":
113 self.logger.log(self.log_level, self.linebuf.rstrip())
114 self.linebuf = ""
115
116
117def disable_torch_init():

Callers 1

build_loggerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected