- 实现EPUB阅读器搜索功能及选中注释功能 - 优化CFI模块,修复代码审查发现的11个问题 - 实现大书远距目录跳转与后台补全优化方案 - 优化设置面板与章节运行时联动 - 重构及大量改进优化
51 lines
1.4 KiB
Swift
51 lines
1.4 KiB
Swift
import Foundation
|
|
|
|
enum RDEPUBBackgroundTrace {
|
|
|
|
static func log(_ scope: String, _ message: String) {
|
|
|
|
let threadRole = Thread.isMainThread ? "main" : "bg"
|
|
|
|
let threadName = resolvedThreadName()
|
|
|
|
let queueLabel = resolvedQueueLabel()
|
|
print("[EPUB][\(scope)][\(threadRole)][queue=\(queueLabel)][thread=\(threadName)] \(message)")
|
|
}
|
|
|
|
static func measure<T>(_ scope: String, _ message: String, work: () throws -> T) rethrows -> T {
|
|
|
|
let startedAt = CFAbsoluteTimeGetCurrent()
|
|
log(scope, "START \(message)")
|
|
do {
|
|
|
|
let result = try work()
|
|
|
|
let elapsedMs = Int((CFAbsoluteTimeGetCurrent() - startedAt) * 1000)
|
|
log(scope, "END \(message) elapsedMs=\(elapsedMs)")
|
|
return result
|
|
} catch {
|
|
|
|
let elapsedMs = Int((CFAbsoluteTimeGetCurrent() - startedAt) * 1000)
|
|
log(scope, "FAIL \(message) elapsedMs=\(elapsedMs) error=\(error)")
|
|
throw error
|
|
}
|
|
}
|
|
|
|
private static func resolvedThreadName() -> String {
|
|
|
|
if let name = Thread.current.name, !name.isEmpty {
|
|
return name
|
|
}
|
|
|
|
if Thread.isMainThread {
|
|
return "main"
|
|
}
|
|
|
|
return String(describing: Unmanaged.passUnretained(Thread.current).toOpaque())
|
|
}
|
|
|
|
private static func resolvedQueueLabel() -> String {
|
|
String(validatingUTF8: __dispatch_queue_get_label(nil)) ?? "unknown"
|
|
}
|
|
}
|