MCPcopy Index your code
hub / github.com/lm-sys/FastChat / StreamToLogger

Class StreamToLogger

fastchat/utils.py:83–116  ·  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 encoded_message = line.encode("utf-8", "ignore").decode("utf-8")
108 self.logger.log(self.log_level, encoded_message.rstrip())
109 else:
110 self.linebuf += line
111
112 def flush(self):
113 if self.linebuf != "":
114 encoded_message = self.linebuf.encode("utf-8", "ignore").decode("utf-8")
115 self.logger.log(self.log_level, encoded_message.rstrip())
116 self.linebuf = ""
117
118
119def disable_torch_init():

Callers 1

build_loggerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…