Skip to content

Commit 9f443ad

Browse files
authored
Use PAL table override for pybinding log routing
Differential Revision: D74122422 Pull Request resolved: #11616
1 parent f1a23fa commit 9f443ad

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

extension/pybindings/pybindings.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,6 @@
6868
} \
6969
})
7070

71-
// Our logs work by writing to stderr. By default this is done through fprintf
72-
// (as defined in posix.cpp) which then does not show up in python environments.
73-
// Here we override the pal to use std::cerr which can be properly redirected by
74-
// scoped_estream_redirect.
75-
void et_pal_emit_log_message(
76-
et_timestamp_t timestamp,
77-
et_pal_log_level_t level,
78-
const char* filename,
79-
ET_UNUSED const char* function,
80-
size_t line,
81-
const char* message,
82-
ET_UNUSED size_t length) {
83-
std::cerr << "[" << filename << ":" << line << "] " << message << std::endl;
84-
}
85-
8671
namespace py = pybind11;
8772
using executorch::BUNDLED_PROGRAM_NAMESPACE::verify_method_outputs;
8873
using ::executorch::ET_RUNTIME_NAMESPACE::BackendInterface;
@@ -1168,6 +1153,32 @@ PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) {
11681153
.def("__repr__", &PyMethodMeta::repr, call_guard);
11691154
}
11701155

1156+
namespace {
1157+
1158+
// Our logs work by writing to stderr. By default this is done through fprintf
1159+
// (as defined in posix.cpp) which then does not show up in python environments.
1160+
// Here we override the pal to use std::cerr which can be properly redirected by
1161+
// scoped_estream_redirect.
1162+
void emit_log_message(
1163+
et_timestamp_t timestamp,
1164+
et_pal_log_level_t level,
1165+
const char* filename,
1166+
ET_UNUSED const char* function,
1167+
size_t line,
1168+
const char* message,
1169+
ET_UNUSED size_t length) {
1170+
std::cerr << "[" << filename << ":" << line << "] " << message << std::endl;
1171+
}
1172+
1173+
runtime::PalImpl build_pal() {
1174+
return runtime::PalImpl::create(emit_log_message, __FILE__);
1175+
}
1176+
1177+
// Update PAL to redirect logs.
1178+
ET_UNUSED bool registration_result = runtime::register_pal(build_pal());
1179+
1180+
} // namespace
1181+
11711182
} // namespace pybindings
11721183
} // namespace extension
11731184
} // namespace executorch

0 commit comments

Comments
 (0)