Skip to content

Commit

Permalink
Write log and stderr to /tmp/Fcitx5.log; disable OSLog and debug log …
Browse files Browse the repository at this point in the history
…for release build (#107)
  • Loading branch information
eagleoflqj authored Apr 12, 2024
1 parent 30c5ee9 commit 5b539c6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ build
assets/en.lproj/Localizable.strings
assets/po/base.pot
meta.swift
debug.swift
*~
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
9 changes: 8 additions & 1 deletion logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions logging/debug.swift.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let isDebug = @IS_DEBUG@
12 changes: 12 additions & 0 deletions logging/logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions src/nativestreambuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define FCITX5_MACOS_NATIVESTREAMBUF_H

#include <array>
#include <iostream>
#include <streambuf>
#include <os/log.h>

Expand Down Expand Up @@ -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
}
};

Expand Down
11 changes: 11 additions & 0 deletions src/server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5b539c6

Please sign in to comment.