Skip to content

Commit

Permalink
Merge pull request #918 from Chilledheart/core_drop_utf8_setlocale_calls
Browse files Browse the repository at this point in the history
core: drop SetUTF8Locale
  • Loading branch information
Chilledheart authored Apr 26, 2024
2 parents edd2506 + 4fb04ee commit bd82dd2
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 107 deletions.
1 change: 0 additions & 1 deletion src/android/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobject g_activity_obj = nullptr;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
g_jvm = vm;
absl::SetFlag(&FLAGS_logtostderr, true);

return JNI_VERSION_1_6;
}
Expand Down
5 changes: 2 additions & 3 deletions src/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ int main(int argc, const char* argv[]) {
return -1;
}
#endif
if (!SetUTF8Locale()) {
LOG(WARNING) << "Failed to set up utf-8 locale";
}

setlocale(LC_ALL, "");

// Major routine
// - Read config from ss config file
Expand Down
9 changes: 8 additions & 1 deletion src/core/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,14 @@ ABSL_FLAG(std::string, log_prefix, std::string(), "Prepend the log prefix to the
#endif

ABSL_FLAG(bool, tick_counts_in_logfile_name, true, "put a tick_counts at the end of the log file name");
ABSL_FLAG(bool, logtostderr, false, "log messages go to stderr instead of logfiles");
ABSL_FLAG(bool,
logtostderr,
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_OHOS) || BUILDFLAG(IS_IOS)
true,
#else
false,
#endif
"log messages go to stderr instead of logfiles");
ABSL_FLAG(bool, alsologtostderr, false, "log messages go to stderr in addition to logfiles");
ABSL_FLAG(bool, colorlogtostderr, false, "color messages logged to stderr (if supported by terminal)");

Expand Down
2 changes: 0 additions & 2 deletions src/core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ using fd_t = int;

bool IsProgramConsole(internal::fd_t fd);

bool SetUTF8Locale();

std::optional<int> StringToInteger(std::string_view value);
std::optional<unsigned> StringToIntegerU(std::string_view value);
std::optional<int64_t> StringToInteger64(std::string_view value);
Expand Down
8 changes: 0 additions & 8 deletions src/core/utils_freebsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,4 @@ uint64_t GetMonotonicTime() {
return static_cast<double>(ts.tv_sec - start_ts.tv_sec) * NS_PER_SECOND + ts.tv_nsec - start_ts.tv_nsec;
}

bool SetUTF8Locale() {
if (setlocale(LC_ALL, "C.UTF-8") == nullptr)
return false;
if (strcmp(setlocale(LC_ALL, nullptr), "C.UTF-8") != 0)
return false;
return true;
}

#endif // BUILDFLAG(IS_FREEBSD)
8 changes: 0 additions & 8 deletions src/core/utils_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,4 @@ uint64_t GetMonotonicTime() {
return static_cast<double>(ts.tv_sec - start_ts.tv_sec) * NS_PER_SECOND + ts.tv_nsec - start_ts.tv_nsec;
}

bool SetUTF8Locale() {
if (setlocale(LC_ALL, "C.UTF-8") == nullptr)
return false;
if (strcmp(setlocale(LC_ALL, nullptr), "C.UTF-8") != 0)
return false;
return true;
}

#endif // __linux__
9 changes: 0 additions & 9 deletions src/core/utils_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ uint64_t GetMonotonicTime() {
return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
}

bool SetUTF8Locale() {
// C.UTF-8 doesn't exists on macOS
if (setlocale(LC_ALL, "en_US.UTF-8") == nullptr)
return false;
if (strcmp(setlocale(LC_ALL, nullptr), "en_US.UTF-8") != 0)
return false;
return true;
}

static constexpr std::string_view kDefaultExePath = "UNKNOWN";
static std::string main_exe_path = std::string(kDefaultExePath);

Expand Down
52 changes: 0 additions & 52 deletions src/core/utils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,58 +212,6 @@ bool IsProgramConsole(HANDLE handle) {
return IsHandleConsole(handle);
}

#ifndef CP_UTF8
#define CP_UTF8 65001
#endif // CP_UTF8

bool SetUTF8Locale() {
bool success = false;

bool is_console = false /* IsProgramConsole() */;

if (is_console) {
// Calling SetConsoleCP
// this setting is permanent and we need to restore CP value after exit,
// otherwise it might affects the all programs in the same console
//
// anyway using WriteConsoleW should be sufficient to handle with UTF-8
// characters in console, better to leave this code path aside.
#if 0
// Use UTF-8 mode on Windows 10 1903 or later.
if (IsWindowsVersionBNOrGreater(10, 0, 18362)) {
static UINT previous_cp, previous_output_cp;
previous_cp = GetConsoleCP();
previous_output_cp = GetConsoleOutputCP();
if (SetConsoleCP(CP_UTF8) && SetConsoleOutputCP(CP_UTF8)) {
success = true;
} else {
PLOG(WARNING) << "Interal error: setup utf-8 console cp failed";
}
std::atexit([]() {
if (!SetConsoleCP(previous_cp) ||
!SetConsoleOutputCP(previous_output_cp)) {
PLOG(WARNING) << "Interal error: restore console cp failed";
}
});
}
#else
success = true;
#endif
} else {
success = true;
}

// Starting in Windows 10 version 1803 (10.0.17134.0), the Universal C Runtime
// supports using a UTF-8 code page.
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-170
if (IsWindowsVersionBNOrGreater(10, 0, 17134)) {
setlocale(LC_ALL, ".UTF8");
} else if (!is_console) {
success = false;
}
return success;
}

static const wchar_t* kDllWhiteList[] = {
#ifndef _LIBCPP_MSVCRT
// msvc runtime, still searched current directory
Expand Down
3 changes: 0 additions & 3 deletions src/gtk/yass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ int main(int argc, const char** argv) {
return -1;
}

if (!SetUTF8Locale()) {
LOG(WARNING) << "Failed to set up utf-8 locale";
}
constexpr std::string_view kDefaultLocalePath = "../share/locale";
std::string locale_path = std::string(kDefaultLocalePath);
size_t rpos = exec_path.rfind('/');
Expand Down
5 changes: 2 additions & 3 deletions src/gtk4/yass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ int main(int argc, const char** argv) {
return -1;
}

if (!SetUTF8Locale()) {
LOG(WARNING) << "Failed to set up utf-8 locale";
}
setlocale(LC_ALL, "");

constexpr std::string_view kDefaultLocalePath = "../share/locale";
std::string locale_path = std::string(kDefaultLocalePath);
size_t rpos = exec_path.rfind('/');
Expand Down
1 change: 0 additions & 1 deletion src/harmony/yass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,6 @@ static napi_module yassModule = {
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) {
absl::SetFlag(&FLAGS_logtostderr, true);
napi_module_register(&yassModule);
}

Expand Down
1 change: 0 additions & 1 deletion src/ios/extensions/YassPacketTunnelProvider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ @implementation YassPacketTunnelProvider {
- (void)startTunnelWithOptions:(NSDictionary*)options completionHandler:(void (^)(NSError*))completionHandler {
stopped_ = false;
SetExecutablePath("UNKNOWN.ext");
absl::SetFlag(&FLAGS_logtostderr, true);

NETunnelProviderProtocol* protocolConfiguration = (NETunnelProviderProtocol*)self.protocolConfiguration;
NSDictionary* dict = protocolConfiguration.providerConfiguration;
Expand Down
6 changes: 1 addition & 5 deletions src/ios/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ int main(int argc, const char** argv) {
return -1;
}

absl::SetFlag(&FLAGS_logtostderr, true);

if (!SetUTF8Locale()) {
LOG(WARNING) << "Failed to set up utf-8 locale";
}
setlocale(LC_ALL, "");

absl::InitializeSymbolizer(exec_path.c_str());
#ifdef HAVE_CRASHPAD
Expand Down
6 changes: 3 additions & 3 deletions src/mac/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ int main(int argc, const char** argv) {
return -1;
}

if (!SetUTF8Locale()) {
LOG(WARNING) << "Failed to set up utf-8 locale";
}
// Sets the locale to the default,
// which is the user-default ANSI code page obtained from the operating system.
setlocale(LC_ALL, "");

absl::InitializeSymbolizer(exec_path.c_str());
#ifdef HAVE_CRASHPAD
Expand Down
5 changes: 2 additions & 3 deletions src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ int main(int argc, const char* argv[]) {
return -1;
}
#endif
if (!SetUTF8Locale()) {
LOG(WARNING) << "Failed to set up utf-8 locale";
}

setlocale(LC_ALL, "");

// Major routine
// - Read config from ss config file
Expand Down
7 changes: 3 additions & 4 deletions src/win32/yass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
fflush(stderr);
}

if (!SetUTF8Locale()) {
LOG(WARNING) << "Failed to set up utf-8 locale";
}
// Sets the locale to the default,
// which is the user-default ANSI code page obtained from the operating system.
setlocale(LC_ALL, "");

absl::InitializeSymbolizer(exec_path.c_str());
#ifdef HAVE_CRASHPAD
Expand All @@ -95,7 +95,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
argv_store[i] = SysWideToUTF8(wargv[i]);
argv[i] = argv_store[i].data();
}
absl::SetFlag(&FLAGS_logtostderr, false);
argv[0] = exec_path.data();

config::SetClientUsageMessage(exec_path);
Expand Down

0 comments on commit bd82dd2

Please sign in to comment.