Skip to content

Commit b1bbcbc

Browse files
GregoryComerfacebook-github-bot
authored andcommitted
Use PAL table override for pybinding log routing
Summary: This PR updates the pybinding log routing to use the new PAL table overrides to reliably route logs. Previously, the PAL is not overridden in optimized bento kernels, preventing users from seeing logs. Differential Revision: D74122422
1 parent df4c12e commit b1bbcbc

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

extension/pybindings/pybindings.cpp

Lines changed: 29 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,35 @@ 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(
1175+
emit_log_message,
1176+
__FILE__
1177+
);
1178+
}
1179+
1180+
// Update PAL to redirect logs.
1181+
ET_UNUSED bool registration_result = runtime::register_pal(build_pal());
1182+
1183+
} // namespace
1184+
11711185
} // namespace pybindings
11721186
} // namespace extension
11731187
} // namespace executorch

0 commit comments

Comments
 (0)