Repository navigation
Write the error log in UTF-8 - #546
Merged
Merged
Conversation
utility::s2us assumes UTF-8, throws std::range_error otherwise. Log messages carry arbitrary narrow text, e.g. std::exception::what(), with no such guarantee - on Windows typically the ANSI code page. Throw lands on the async logging worker thread, where slog's message_service::run catches only stopped_exception, so it escapes the thread function and terminates the process. Add utility::s2us_lenient: UTF-8 fast path, Windows ANSI code page fallback, Latin-1 otherwise. Never throws, never discards a message. s2us contract unchanged. Validation uses conversions::to_utf16string, the same decoder s2us uses on a wide build, so the fast path accepts exactly what s2us accepts. Note that decoder is not a strict UTF-8 validator - it tolerates surrogates, overlongs and code points above U+10FFFF - so a hand-rolled check would have diverged. Converts s rather than the decoded value, so the result is identical to s2us wherever s2us succeeded, on narrow builds too. Skips the ANSI code page when it is itself UTF-8 (Windows 10 and later), where it would repeat the failed decode and substitute U+FFFD, losing the characters at issue. Applied in json_from_message (message, file, function, categories) and at the what() boundaries in the API error responses. Guard log_gate::service as a last resort, so no logging failure can terminate the application. The invalid bytes also reached the JSON on narrow builds, i.e. the Logging API emitted invalid UTF-8; testInsertLogEventNonUtf8 covers that on all platforms and fails against the unfixed code. Built and tested on darwin only. The ANSI code page branch is untested.
When a narrow string is not valid UTF-8 and the Windows ANSI code page does not apply, s2us_lenient now decodes it as UTF-8 and replaces each invalid byte with U+FFFD, rather than decoding the whole string as Latin-1. One bad byte no longer turns the valid UTF-8 around it into mojibake. The decoder is boost::locale::utf, which is header-only and available in every supported Boost version. Boost's decode consumes the byte that cuts a sequence short, so decoding resumes after the replaced byte, which keeps, for example, the space in "caf\xE9 au lait". On a wide build, return the result of the validating decode rather than decoding a second time. Define s2us_lenient in basic_utils.cpp, so the fallback it calls is not declared in the header. Log categories are constants defined in code, not text from the platform, so convert them with s2us again. Omit error_description from WWW-Authenticate when the message contains characters outside the set RFC 6749 section 5.2 allows, rather than putting a converted message in the header. The JSON error body still carries the message. Signed-off-by: Gareth Sylvester-Bradley <garethsb@nvidia.com>
The log events and the access log are UTF-8, but the error log wrote each message's bytes unchanged, so it could mix the Windows ANSI code page with UTF-8. Convert the message with s2us_lenient and us2s, like the log events, so the error log is UTF-8 whatever the encoding of the message. On Windows, the example applications set LC_CTYPE to ".UTF-8", so that the C runtime converts the UTF-8 output to the console code page when writing to the console. Files are written unchanged. Signed-off-by: Gareth Sylvester-Bradley <garethsb@nvidia.com>
On Windows, the C runtime converts the narrow command-line arguments to the ANSI code page, which may lose characters, and utility::s2us throws on ANSI bytes that are not valid UTF-8. So a configuration file path, or settings JSON, containing non-ASCII characters failed. Use wmain on Windows, so argv[1] is already a utility::string_t, and open the file by its wide name. Signed-off-by: Gareth Sylvester-Bradley <garethsb@nvidia.com>
Contributor
Author
|
@lo-simon No clear reason why the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #545, which this branch is based on. Until #545 is merged, this PR also shows its two commits. Only the last two commits are new.
The log events and the access log are UTF-8, but the error log wrote each message's bytes unchanged, so it could mix the Windows ANSI code page with UTF-8.
error_log_formatnow converts the message withs2us_lenientandus2s, like the log events, so the error log is UTF-8 whatever the encoding of the message.testErrorLogFormatNonUtf8covers this, and fails against #545 alone.On Windows, the example applications now call
std::setlocale(LC_CTYPE, ".UTF-8"). With that locale, the C runtime converts UTF-8 output to the console code page when writing to the console, so accented text shows correctly where the console code page can represent it, and as?where it can't. Files are written unchanged. This takes the approach suggested in #109: the application is switched to UTF-8, rather than its output being converted back to the ANSI code page.UTF-8 locales need Windows 10 version 1803 or later (
setlocale, UTF-8 support). On earlier versions,setlocalefails and the console output is as before. In UTF-8 mode, the C runtime also interprets narrow file paths as UTF-8. The library opens files withutility::string_tpaths, which are wide on Windows, so it is unaffected.The example applications also now use
wmainon Windows, so the settings on the command line, or the path of a configuration file, are read as UTF-16. Previously, the C runtime converted them to the ANSI code page, which may lose characters, andutility::s2usthrew on non-ASCII ANSI text. The configuration file is opened by its wide name, so the UTF-8 locale does not affect it.Built and tested on Linux (Ubuntu 24.04). The Windows code has not been built or run yet, and needs checking in Windows CI and on a Windows console.
Co-authored-by: Gareth's 🤖 buddy