MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / format

Method format

imperative/python/megengine/logger.py:62–118  ·  view source on GitHub ↗
(self, record)

Source from the content-addressed store, hash-verified

60 return "\x1b[32m{}\x1b[0m".format(msg)
61
62 def format(self, record):
63 if record.levelno == logging.DEBUG:
64 mcl, mtxt = self._color_dbg, "DBG"
65 elif record.levelno == logging.WARNING:
66 mcl, mtxt = self._color_warn, "WRN"
67 elif record.levelno == logging.ERROR:
68 mcl, mtxt = self._color_err, "ERR"
69 else:
70 mcl, mtxt = self._color_normal, ""
71
72 if mtxt:
73 mtxt += " "
74
75 if self.log_fout:
76 self.__set_fmt(self.date_full + mtxt + self.msg)
77 formatted = super(MegEngineLogFormatter, self).format(record)
78 nr_line = formatted.count("\n") + 1
79 if nr_line >= self.max_lines:
80 head, body = formatted.split("\n", 1)
81 formatted = "\n".join(
82 [
83 head,
84 "BEGIN_LONG_LOG_{}_LINES{{".format(nr_line - 1),
85 body,
86 "}}END_LONG_LOG_{}_LINES".format(nr_line - 1),
87 ]
88 )
89 self.log_fout.write(formatted)
90 self.log_fout.write("\n")
91 self.log_fout.flush()
92
93 self.__set_fmt(self._color_date(self.date) + mcl(mtxt + self.msg))
94 formatted = super(MegEngineLogFormatter, self).format(record)
95
96 if record.exc_text or record.exc_info:
97 # handle exception format
98 b = formatted.find("Traceback ")
99 if b != -1:
100 s = formatted[b:]
101 s = self._color_exc(" " + s.replace("\n", "\n "))
102 formatted = formatted[:b] + s
103
104 nr_line = formatted.count("\n") + 1
105 if nr_line >= self.max_lines:
106 lines = formatted.split("\n")
107 remain = self.max_lines // 2
108 removed = len(lines) - remain * 2
109 if removed > 0:
110 mid_msg = self._color_omitted(
111 "[{} log lines omitted (would be written to output file "
112 "if set_log_file() has been called;\n"
113 " the threshold can be set at "
114 "MegEngineLogFormatter.max_lines)]".format(removed)
115 )
116 formatted = "\n".join(lines[:remain] + [mid_msg] + lines[-remain:])
117
118 return formatted
119

Callers 7

_color_excMethod · 0.45
_color_dbgMethod · 0.45
_color_warnMethod · 0.45
_color_errMethod · 0.45
_color_omittedMethod · 0.45
_color_dateMethod · 0.45
_color_dateMethod · 0.45

Calls 10

__set_fmtMethod · 0.95
_color_dateMethod · 0.95
_color_excMethod · 0.95
_color_omittedMethod · 0.95
joinMethod · 0.80
countMethod · 0.45
writeMethod · 0.45
flushMethod · 0.45
findMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected