emilk / loguru

A lightweight C++ logging library
The Unlicense
1.76k stars 256 forks source link

Logging to files fails on Windows if the path contains Unicode characters #232

Open lethal-guitar opened 1 year ago

lethal-guitar commented 1 year ago

For example, trying to save a log file to a path which contains Greek, Russian etc. letters. The problem is that _fsopen and mkdir on Windows treat the file path argument as encoded in the platform codepage, which can in theory be set to UTF-8 but in practice is most often a language-specific codepage like Latin-1.

To fix this, it's necessary to use _wfsopen and _wmkdir and convert the paths to wide char before calling the functions. Something along those lines:

const auto needed = MultiByteToWideChar(CP_UTF8, 0, file_path, -1, nullptr, 0);
auto w_file_path = std::unique_ptr<wchar_t[]>(new wchar_t[needed]);
MultiByteToWideChar(CP_UTF8, 0, file_path, -1, w_file_path.get(), needed);

if (_wmkdir(w_file_path.get()) == -1) {