Skip to content

Commit 0f9d0d2

Browse files
authored
make log string interpolation public (#87)
1 parent 8d9deeb commit 0f9d0d2

10 files changed

Lines changed: 46 additions & 20 deletions

File tree

common/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ target_include_directories(FcitxCommon PRIVATE
77
)
88
target_link_libraries(FcitxCommon Fcitx5::Core nlohmann_json)
99

10-
add_library(SwiftUtil util.swift locale.swift)
10+
add_library(SwiftUtil util.swift logging.swift locale.swift)
1111
set_target_properties(SwiftUtil PROPERTIES Swift_MODULE_NAME SwiftUtil)

common/locale.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// See fcitx5-macos/src/locale.swift
22

33
import Foundation
4-
import SwiftUtil
54

65
// Return app locale or system locale if called from app.
76
// Return system locale if called from keyboard.
87
public func getLocale() -> String {
98
let locale = Locale.current
10-
logger.info("System locale = \(locale.identifier)")
9+
FCITX_INFO("System locale = \(locale.identifier)")
1110

1211
if let languageCode = locale.language.languageCode?.identifier {
1312
if languageCode == "zh" {

common/logging.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import OSLog
2+
3+
private let logger = Logger(subsystem: "org.fcitx.Fcitx5", category: "FcitxLog")
4+
5+
// swift-format-ignore: AlwaysUseLowerCamelCase
6+
public func FCITX_DEBUG(_ message: String) {
7+
// https://stackoverflow.com/questions/57509909/swift-oslog-os-log-not-showing-up-in-console-app
8+
#if targetEnvironment(simulator)
9+
logger.info("\(message, privacy: .public)")
10+
#else
11+
logger.debug("\(message, privacy: .public)")
12+
#endif
13+
}
14+
15+
// swift-format-ignore: AlwaysUseLowerCamelCase
16+
public func FCITX_INFO(_ message: String) {
17+
logger.info("\(message, privacy: .public)")
18+
}
19+
20+
// swift-format-ignore: AlwaysUseLowerCamelCase
21+
public func FCITX_WARN(_ message: String) {
22+
logger.error("\(message, privacy: .public)")
23+
}
24+
25+
// swift-format-ignore: AlwaysUseLowerCamelCase
26+
public func FCITX_ERROR(_ message: String) {
27+
logger.fault("\(message, privacy: .public)")
28+
}

common/util.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import CryptoKit
22
import Foundation
3-
import OSLog
4-
5-
public let logger = Logger(subsystem: "org.fcitx.Fcitx5", category: "FcitxLog")
63

74
public let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
85
// For SideStore, app group is not available, so fallback to documents.
9-
public let appGroup = FileManager.default.containerURL(
10-
forSecurityApplicationGroupIdentifier: "group.org.fcitx.Fcitx5") ?? documents
6+
public let appGroup =
7+
FileManager.default.containerURL(
8+
forSecurityApplicationGroupIdentifier: "group.org.fcitx.Fcitx5") ?? documents
119
public let appGroupConfig = appGroup.appendingPathComponent("config")
1210
public let appGroupTmp = appGroup.appendingPathComponent("tmp")
1311
public let appGroupData = appGroup.appendingPathComponent("data")
@@ -119,7 +117,8 @@ public func initProfile() {
119117
mkdirP(appGroupConfig.path)
120118
let profileURL = appGroupConfig.appendingPathComponent("profile")
121119
if !profileURL.exists() {
122-
try? FileManager.default.copyItem(at: Bundle.main.bundleURL.appendingPathComponent("profile"), to: profileURL)
120+
try? FileManager.default.copyItem(
121+
at: Bundle.main.bundleURL.appendingPathComponent("profile"), to: profileURL)
123122
}
124123
}
125124

ipc/ipc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ public func requestReload() {
3131
to: appGroupTmp.appendingPathComponent("\(keyboard).reload"), atomically: true,
3232
encoding: .utf8)
3333
}
34-
logger.info("Reload requested")
34+
FCITX_INFO("Reload requested")
3535
}

keyboard/KeyboardViewController.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
4646

4747
override func viewDidLoad() {
4848
id = UInt64(Int(bitPattern: Unmanaged.passUnretained(self).toOpaque()))
49-
logger.info("viewDidLoad \(self.id)")
49+
FCITX_INFO("viewDidLoad \(self.id)")
5050
super.viewDidLoad()
5151
redirectStderr()
5252
initProfile()
@@ -66,7 +66,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
6666
}
6767

6868
override func viewWillAppear(_ animated: Bool) {
69-
logger.info("viewWillAppear \(self.id)")
69+
FCITX_INFO("viewWillAppear \(self.id)")
7070
SwiftFrontend.setClient(self)
7171
KeyboardUI.setClient(self)
7272

@@ -87,7 +87,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
8787
super.viewWillAppear(animated)
8888
let keyboard = Bundle.main.bundleURL.deletingPathExtension().lastPathComponent
8989
if removeFile(appGroupTmp.appendingPathComponent("\(keyboard).reload")) {
90-
logger.info("Reload accepted")
90+
FCITX_INFO("Reload accepted")
9191
reload()
9292
}
9393
vm.setDisplayMode(.initial)
@@ -96,7 +96,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
9696
}
9797

9898
override func viewWillDisappear(_ animated: Bool) {
99-
logger.info("viewWillDisappear \(self.id)")
99+
FCITX_INFO("viewWillDisappear \(self.id)")
100100
super.viewWillDisappear(animated)
101101
focusOut()
102102
hostingController.willMove(toParent: nil)
@@ -105,11 +105,11 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol {
105105
}
106106

107107
deinit {
108-
logger.info("deinit \(self.id)")
108+
FCITX_INFO("deinit \(self.id)")
109109
}
110110

111111
override func viewWillLayoutSubviews() {
112-
logger.info("viewWillLayoutSubviews \(self.id)")
112+
FCITX_INFO("viewWillLayoutSubviews \(self.id)")
113113
super.viewWillLayoutSubviews()
114114
}
115115

scripts/format.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
find common src keyboard iosfrontend iosnotifications uipanel -name '*.cpp' -o -name '*.h' | xargs clang-format -i
2-
swift-format format --in-place $(find src keyboard iosfrontend iosnotifications uipanel protocol ipc -name '*.swift')
2+
swift-format format --in-place $(find common src keyboard iosfrontend iosnotifications uipanel protocol ipc -name '*.swift')

scripts/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set -e
22

33
find common src keyboard iosfrontend iosnotifications uipanel -name '*.cpp' -o -name '*.h' | xargs clang-format -Werror --dry-run
4-
swift-format lint -rs src keyboard iosfrontend iosnotifications uipanel protocol ipc
4+
swift-format lint -rs common src keyboard iosfrontend iosnotifications uipanel protocol ipc
55

66
localizables=$(find assets -name 'Localizable.strings')
77
for localizable in $localizables; do

src/App.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Fcitx5App: App {
1414
SceneDelegate.contentView?.handleURL(url)
1515
}.onChange(of: scenePhase) { newPhase in
1616
if newPhase == .active {
17-
logger.info("App is active")
17+
FCITX_INFO("App is active")
1818
sync(
1919
documents.appendingPathComponent("rime"), appGroupData.appendingPathComponent("rime"))
2020
}

uipanel/KeyModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func executeActions(_ actions: [[String: String]]) {
285285
let code = action["code"] ?? ""
286286
client.keyPressed(key, code)
287287
default:
288-
logger.error("Unknown action type: \(type)")
288+
FCITX_ERROR("Unknown action type: \(type)")
289289
}
290290
}
291291
}

0 commit comments

Comments
 (0)