Files
uskey/Sources/Logger.swift
loveuer 6285cabdd1
Some checks failed
Build and Release / Build DMG for arm64 (push) Has been cancelled
Build and Release / Build DMG for x86_64 (push) Has been cancelled
Build and Release / Create Release (push) Has been cancelled
Major improvements and bug fixes for v1.1.0
Features:
- Add auto-enable on startup based on config
- Add enabled state persistence in config.json
- Add limitations notice in GUI menu
- Simplify logging to single file (uskey.log)
- Change "Open Logs Folder" to "Open Config Folder"

Bug Fixes:
- Fix GUI status display mismatch on startup
- Fix first toggle click not working issue
- Revert password field changes that caused key blocking

UI Changes:
- Add ⚠️ emoji to "About Limitations" menu item
- Remove button action to fix menu refresh

🤖 Generated with [Qoder][https://qoder.com]
2025-12-05 13:48:15 +08:00

78 lines
2.2 KiB
Swift

@preconcurrency import Foundation
struct Logger {
nonisolated(unsafe) static var logLevel: LogLevel = .info
nonisolated(unsafe) private static var logFileHandle: FileHandle?
nonisolated(unsafe) private static var logFilePath: String?
static func setup() {
let logsDir = getLogsDirectory()
try? FileManager.default.createDirectory(at: URL(fileURLWithPath: logsDir), withIntermediateDirectories: true)
logFilePath = "\(logsDir)/uskey.log"
FileManager.default.createFile(atPath: logFilePath!, contents: nil)
logFileHandle = FileHandle(forWritingAtPath: logFilePath!)
info("Logger initialized, log file: \(logFilePath!)")
}
static func getLogFilePath() -> String? {
return logFilePath
}
static func getLogsDirectory() -> String {
let homeDir = FileManager.default.homeDirectoryForCurrentUser
return homeDir.appendingPathComponent(".config/uskey/logs").path
}
static func debug(_ message: String) {
log(.debug, message)
}
static func info(_ message: String) {
log(.info, message)
}
static func warning(_ message: String) {
log(.warning, message)
}
static func error(_ message: String) {
log(.error, message)
}
private static func log(_ level: LogLevel, _ message: String) {
guard logLevel.shouldLog(level) else { return }
let timestamp = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let timeString = formatter.string(from: timestamp)
let levelString: String
switch level {
case .debug:
levelString = "DEBUG"
case .info:
levelString = "INFO"
case .warning:
levelString = "WARN"
case .error:
levelString = "ERROR"
}
let logMessage = "[\(timeString)] [\(levelString)] \(message)\n"
print(logMessage, terminator: "")
if let data = logMessage.data(using: .utf8) {
logFileHandle?.write(data)
}
}
static func cleanup() {
logFileHandle?.closeFile()
}
}