From 5a833ec002175f86fc197d96ac791ac42d2d1142 Mon Sep 17 00:00:00 2001 From: Keeyou Date: Fri, 26 Apr 2024 13:33:04 +0800 Subject: [PATCH 1/2] mobile: don't keep log files (trivial) --- src/android/jni.cpp | 1 - src/core/logging.cpp | 9 ++++++++- src/harmony/yass.cpp | 1 - src/ios/extensions/YassPacketTunnelProvider.mm | 1 - src/ios/main.mm | 2 -- src/win32/yass.cpp | 1 - 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/android/jni.cpp b/src/android/jni.cpp index 4732a035c..ae4078ee9 100644 --- a/src/android/jni.cpp +++ b/src/android/jni.cpp @@ -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; } diff --git a/src/core/logging.cpp b/src/core/logging.cpp index f371ae484..239dc2768 100644 --- a/src/core/logging.cpp +++ b/src/core/logging.cpp @@ -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)"); diff --git a/src/harmony/yass.cpp b/src/harmony/yass.cpp index e7c8e096a..fed4208b5 100644 --- a/src/harmony/yass.cpp +++ b/src/harmony/yass.cpp @@ -1159,7 +1159,6 @@ static napi_module yassModule = { }; extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { - absl::SetFlag(&FLAGS_logtostderr, true); napi_module_register(&yassModule); } diff --git a/src/ios/extensions/YassPacketTunnelProvider.mm b/src/ios/extensions/YassPacketTunnelProvider.mm index 55b373699..f91975a65 100644 --- a/src/ios/extensions/YassPacketTunnelProvider.mm +++ b/src/ios/extensions/YassPacketTunnelProvider.mm @@ -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; diff --git a/src/ios/main.mm b/src/ios/main.mm index 665bfd987..9eaad49ff 100644 --- a/src/ios/main.mm +++ b/src/ios/main.mm @@ -33,8 +33,6 @@ 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"; } diff --git a/src/win32/yass.cpp b/src/win32/yass.cpp index 0ec4adf91..a93dbacda 100644 --- a/src/win32/yass.cpp +++ b/src/win32/yass.cpp @@ -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); From 4fb04ee01d023c8d4e5c9a7bd847e738c30bc116 Mon Sep 17 00:00:00 2001 From: Keeyou Date: Fri, 26 Apr 2024 13:51:04 +0800 Subject: [PATCH 2/2] core: drop SetUTF8Locale There is no support for locales in the C library / from native code. --- src/cli/cli.cpp | 5 ++-- src/core/utils.hpp | 2 -- src/core/utils_freebsd.cpp | 8 ------ src/core/utils_linux.cpp | 8 ------ src/core/utils_mac.mm | 9 ------- src/core/utils_win.cpp | 52 -------------------------------------- src/gtk/yass.cpp | 3 --- src/gtk4/yass.cpp | 5 ++-- src/ios/main.mm | 4 +-- src/mac/main.mm | 6 ++--- src/server/server.cpp | 5 ++-- src/win32/yass.cpp | 6 ++--- 12 files changed, 13 insertions(+), 100 deletions(-) diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 2876ac8ea..6532c210f 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -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 diff --git a/src/core/utils.hpp b/src/core/utils.hpp index 4a7aeb4df..dde48b50b 100644 --- a/src/core/utils.hpp +++ b/src/core/utils.hpp @@ -61,8 +61,6 @@ using fd_t = int; bool IsProgramConsole(internal::fd_t fd); -bool SetUTF8Locale(); - std::optional StringToInteger(std::string_view value); std::optional StringToIntegerU(std::string_view value); std::optional StringToInteger64(std::string_view value); diff --git a/src/core/utils_freebsd.cpp b/src/core/utils_freebsd.cpp index 8798d2140..ba662c8aa 100644 --- a/src/core/utils_freebsd.cpp +++ b/src/core/utils_freebsd.cpp @@ -46,12 +46,4 @@ uint64_t GetMonotonicTime() { return static_cast(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) diff --git a/src/core/utils_linux.cpp b/src/core/utils_linux.cpp index 3554a391f..68e374303 100644 --- a/src/core/utils_linux.cpp +++ b/src/core/utils_linux.cpp @@ -181,12 +181,4 @@ uint64_t GetMonotonicTime() { return static_cast(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__ diff --git a/src/core/utils_mac.mm b/src/core/utils_mac.mm index dabc23736..b9bd4514a 100644 --- a/src/core/utils_mac.mm +++ b/src/core/utils_mac.mm @@ -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); diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 9ae0e734f..f0efc0a70 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -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 diff --git a/src/gtk/yass.cpp b/src/gtk/yass.cpp index dd6b4ad87..35cab05af 100644 --- a/src/gtk/yass.cpp +++ b/src/gtk/yass.cpp @@ -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('/'); diff --git a/src/gtk4/yass.cpp b/src/gtk4/yass.cpp index f3ec143c6..fff3520ba 100644 --- a/src/gtk4/yass.cpp +++ b/src/gtk4/yass.cpp @@ -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('/'); diff --git a/src/ios/main.mm b/src/ios/main.mm index 9eaad49ff..21338d743 100644 --- a/src/ios/main.mm +++ b/src/ios/main.mm @@ -33,9 +33,7 @@ int main(int argc, const char** argv) { return -1; } - if (!SetUTF8Locale()) { - LOG(WARNING) << "Failed to set up utf-8 locale"; - } + setlocale(LC_ALL, ""); absl::InitializeSymbolizer(exec_path.c_str()); #ifdef HAVE_CRASHPAD diff --git a/src/mac/main.mm b/src/mac/main.mm index 41ab2a21e..1c41c25f5 100644 --- a/src/mac/main.mm +++ b/src/mac/main.mm @@ -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 diff --git a/src/server/server.cpp b/src/server/server.cpp index 15164ad5b..4b10a5f5f 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -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 diff --git a/src/win32/yass.cpp b/src/win32/yass.cpp index a93dbacda..9f739fc53 100644 --- a/src/win32/yass.cpp +++ b/src/win32/yass.cpp @@ -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