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

Method py_log_handler

imperative/python/src/utils.cpp:53–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51private:
52 static py::object logger;
53 static void py_log_handler(
54 mgb::LogLevel level, const char* file, const char* func, int line,
55 const char* fmt, va_list ap) {
56 using mgb::LogLevel;
57
58 MGB_MARK_USED_VAR(file);
59 MGB_MARK_USED_VAR(func);
60 MGB_MARK_USED_VAR(line);
61
62 if (g_global_finalized)
63 return;
64
65 const char* py_type;
66 switch (level) {
67 case LogLevel::DEBUG:
68 py_type = "debug";
69 break;
70 case LogLevel::INFO:
71 py_type = "info";
72 break;
73 case LogLevel::WARN:
74 py_type = "warning";
75 break;
76 case LogLevel::ERROR:
77 py_type = "error";
78 break;
79 default:
80 throw std::runtime_error("bad log level");
81 }
82
83 std::string msg = mgb::svsprintf(fmt, ap);
84 auto do_log = [msg = msg, py_type]() {
85 if (logger.is_none())
86 return;
87 py::object _call = logger.attr(py_type);
88 _call(py::str(msg));
89 };
90 if (PyGILState_Check()) {
91 do_log();
92 } else {
93 py_task_q.add_task(do_log);
94 }
95 }
96};
97py::object LoggerWrapper::logger = py::none{};
98

Callers

nothing calls this directly

Calls 4

strFunction · 0.85
attrMethod · 0.80
svsprintfFunction · 0.50
add_taskMethod · 0.45

Tested by

no test coverage detected