From 5b539c62fd9d2e3173536f9653876eeeec0043ff Mon Sep 17 00:00:00 2001 From: Qijia Liu Date: Fri, 12 Apr 2024 08:17:16 -0400 Subject: [PATCH] Write log and stderr to /tmp/Fcitx5.log; disable OSLog and debug log for release build (#107) --- .gitignore | 1 + README.md | 13 +++++++++++-- logging/CMakeLists.txt | 9 ++++++++- logging/debug.swift.in | 1 + logging/logging.swift | 12 ++++++++++++ src/nativestreambuf.h | 8 ++++++++ src/server.swift | 11 +++++++++++ 7 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 logging/debug.swift.in diff --git a/.gitignore b/.gitignore index 3daa43e..ca42552 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ build assets/en.lproj/Localizable.strings assets/po/base.pot meta.swift +debug.swift *~ diff --git a/README.md b/README.md index b81c1cd..5c3e947 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,15 @@ Public beta: please download [installer](https://github.com/fcitx-contrib/fcitx5 ## Build Native build on Intel and Apple Silicon is supported. +This is NOT an Xcode project, +but Xcode is needed for Swift compiler. + ### Install dependencies You may use [nvm](https://github.com/nvm-sh/nvm) to install node, then ```sh +sudo xcode-select -s /Applications/Xcode.app/Contents/Developer brew install cmake ninja extra-cmake-modules gettext iso-codes xkeyboardconfig nlohmann-json ./install-deps.sh npm i -g pnpm @@ -40,6 +44,10 @@ sudo /usr/bin/codesign --force --sign - --deep /Library/Input\ Methods/Fcitx5.ap * Check `Include Info Messages` and `Include Debug Messages` in `Action` menu. * Put `FcitxLog` in `Search`. +### Log +`/tmp/Fcitx5.log` contains all log in Console.app, +plus those written to stderr by engines, e.g. rime. + ### lldb SSH into the mac from another device, then ```sh @@ -48,8 +56,9 @@ $ /usr/bin/lldb ``` ## Plugins -Fcitx5 only packges keyboard engine. -To install other engines, see [fcitx5-macos-plugins](https://github.com/fcitx-contrib/fcitx5-macos-plugins). +Fcitx5 only packages keyboard engine. +To install other [engines](https://github.com/fcitx-contrib/fcitx5-macos-plugins), +use the built-in Plugin Manager. ## Translation diff --git a/logging/CMakeLists.txt b/logging/CMakeLists.txt index d9eb905..a576976 100644 --- a/logging/CMakeLists.txt +++ b/logging/CMakeLists.txt @@ -1 +1,8 @@ -add_library(Logging logging.swift) +if(CMAKE_BUILD_TYPE MATCHES "Debug") + set(IS_DEBUG true) +else() + set(IS_DEBUG false) +endif() +configure_file(debug.swift.in ${CMAKE_CURRENT_SOURCE_DIR}/debug.swift @ONLY) + +add_library(Logging logging.swift debug.swift) diff --git a/logging/debug.swift.in b/logging/debug.swift.in new file mode 100644 index 0000000..7bd391d --- /dev/null +++ b/logging/debug.swift.in @@ -0,0 +1 @@ +let isDebug = @IS_DEBUG@ diff --git a/logging/logging.swift b/logging/logging.swift index fbe3a09..81c1e5f 100644 --- a/logging/logging.swift +++ b/logging/logging.swift @@ -3,17 +3,29 @@ import OSLog let logger = Logger(subsystem: "org.fcitx.inputmethod.Fcitx5", category: "FcitxLog") public func FCITX_DEBUG(_ message: String) { + if isDebug { logger.debug("\(message, privacy: .public)") + fputs(message + "\n", stderr) + } } public func FCITX_INFO(_ message: String) { + if isDebug { logger.info("\(message, privacy: .public)") + } + fputs(message + "\n", stderr) } public func FCITX_WARN(_ message: String) { + if isDebug { logger.error("\(message, privacy: .public)") + } + fputs(message + "\n", stderr) } public func FCITX_ERROR(_ message: String) { + if isDebug { logger.fault("\(message, privacy: .public)") + } + fputs(message + "\n", stderr) } diff --git a/src/nativestreambuf.h b/src/nativestreambuf.h index 420a17d..f460d80 100644 --- a/src/nativestreambuf.h +++ b/src/nativestreambuf.h @@ -9,6 +9,7 @@ #define FCITX5_MACOS_NATIVESTREAMBUF_H #include +#include #include #include @@ -103,8 +104,15 @@ class native_streambuf : public std::streambuf { } void write_log(const char_type *text) const { +#ifdef NDEBUG + if (prio != OS_LOG_TYPE_DEBUG) { + std::cerr << text; + } +#else os_log_with_type(logger, prio, "%{public}s", text + (should_offset ? 1 : 0)); + std::cerr << text; +#endif } }; diff --git a/src/server.swift b/src/server.swift index 3be0fa6..3067473 100644 --- a/src/server.swift +++ b/src/server.swift @@ -16,12 +16,23 @@ class NSManualApplication: NSApplication { } } +// Redirect stderr to /tmp/Fcitx5.log as it's not captured anyway. +private func redirectStderr() { + let file = fopen("/tmp/Fcitx5.log", "w") + if let file = file { + dup2(fileno(file), STDERR_FILENO) + fclose(file) + } +} + @main class AppDelegate: NSObject, NSApplicationDelegate { static var server = IMKServer() static var notificationDelegate = NotificationDelegate() func applicationDidFinishLaunching(_ notification: Notification) { + redirectStderr() + AppDelegate.server = IMKServer( name: Bundle.main.infoDictionary?["InputMethodConnectionName"] as? String, bundleIdentifier: Bundle.main.bundleIdentifier)