Skip to content

Write the error log in UTF-8 - #546

Merged
lo-simon merged 4 commits into
sony:masterfrom
garethsb:bugfix/utf8-error-log
Nov 19, 2026
Merged

lo-simon merged 4 commits into
sony:masterfrom
garethsb:bugfix/utf8-error-log

Conversation

@garethsb

@garethsb garethsb commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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_format now converts the message with s2us_lenient and us2s, like the log events, so the error log is UTF-8 whatever the encoding of the message. testErrorLogFormatNonUtf8 covers 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, setlocale fails 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 with utility::string_t paths, which are wide on Windows, so it is unaffected.

The example applications also now use wmain on 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, and utility::s2us threw 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

marcusfreisleben and others added 4 commits November 18, 2026 10:54
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>
@garethsb

Copy link
Copy Markdown
Contributor Author

@lo-simon No clear reason why the actions/upload-artifact steps would fail in one job here... GitHub struggling, unrelated to this PR, presumably!

@lo-simon lo-simon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lo-simon
lo-simon merged commit f922ef9 into sony:master Nov 19, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants