MCPcopy Create free account
hub / github.com/OpenBMB/MiniCPM-V / StreamToLogger

Class StreamToLogger

omnilmm/utils.py:60–91  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

58
59
60class StreamToLogger(object):
61 """
62 Fake file-like stream object that redirects writes to a logger instance.
63 """
64
65 def __init__(self, logger, log_level=logging.INFO):
66 self.terminal = sys.stdout
67 self.logger = logger
68 self.log_level = log_level
69 self.linebuf = ''
70
71 def __getattr__(self, attr):
72 return getattr(self.terminal, attr)
73
74 def write(self, buf):
75 temp_linebuf = self.linebuf + buf
76 self.linebuf = ''
77 for line in temp_linebuf.splitlines(True):
78 # From the io.TextIOWrapper docs:
79 # On output, if newline is None, any '\n' characters written
80 # are translated to the system default line separator.
81 # By default sys.stdout.write() expects '\n' newlines and then
82 # translates them so this is still cross platform.
83 if line[-1] == '\n':
84 self.logger.log(self.log_level, line.rstrip())
85 else:
86 self.linebuf += line
87
88 def flush(self):
89 if self.linebuf != '':
90 self.logger.log(self.log_level, self.linebuf.rstrip())
91 self.linebuf = ''
92
93
94def disable_torch_init():

Callers 1

build_loggerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected