Skip to content

Commit

Permalink
Improve handling of non-UTF-8 log messages
Browse files Browse the repository at this point in the history
Leverage our existing `_try_to_decode_string` function which uses
`errors="replace"` for decoding, guaranteeing that log messages won't be
lost entirely if they contain non-UTF-8 data.

Also, pass the log message around as a C++ string rather than
a C string, which is more efficient anyway (since the size is known and
doesn't need to be computed).

Signed-off-by: Matt Wozniski <[email protected]>
  • Loading branch information
godlygeek authored and pablogsal committed Nov 22, 2023
1 parent 60da939 commit 3e7d72d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/pystack/_pystack.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class NativeReportingMode(enum.Enum):
ALL = 1000


cdef api void log_with_python(const char* message, int level) noexcept:
with contextlib.suppress(UnicodeDecodeError):
LOGGER.log(level, message)
cdef api void log_with_python(const cppstring *message, int level) noexcept:
pymessage = _try_to_decode_string(message)
LOGGER.log(level, pymessage)

T = TypeVar("T", bound=Callable[..., Any])

Expand Down
2 changes: 1 addition & 1 deletion src/pystack/_pystack/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ logWithPython(const std::string& message, int level)
throw std::runtime_error("Logger is not initialized");
}
if (!PyErr_Occurred()) {
log_with_python(message.c_str(), level);
log_with_python(&message, level);
}
}

Expand Down

0 comments on commit 3e7d72d

Please sign in to comment.