2018-12-13 10:08:10 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-04 11:15:29 +00:00
|
|
|
// Copyright © 2018-2020 WireGuard LLC. All Rights Reserved.
|
2018-12-13 10:08:10 +00:00
|
|
|
|
2018-12-13 12:39:38 +00:00
|
|
|
import Foundation
|
2018-12-13 10:08:10 +00:00
|
|
|
import os.log
|
|
|
|
|
2018-12-13 14:26:04 +00:00
|
|
|
public class Logger {
|
2018-12-14 21:53:42 +00:00
|
|
|
enum LoggerError: Error {
|
|
|
|
case openFailure
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 14:26:04 +00:00
|
|
|
static var global: Logger?
|
2018-12-13 10:08:10 +00:00
|
|
|
|
2018-12-14 21:53:42 +00:00
|
|
|
var log: OpaquePointer
|
2019-03-17 06:41:10 +00:00
|
|
|
var tag: String
|
2018-12-13 14:26:04 +00:00
|
|
|
|
2019-03-17 06:41:10 +00:00
|
|
|
init(tagged tag: String, withFilePath filePath: String) throws {
|
2018-12-14 21:53:42 +00:00
|
|
|
guard let log = open_log(filePath) else { throw LoggerError.openFailure }
|
|
|
|
self.log = log
|
2019-03-17 06:41:10 +00:00
|
|
|
self.tag = tag
|
2018-12-14 21:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
close_log(self.log)
|
2018-12-13 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 14:26:04 +00:00
|
|
|
func log(message: String) {
|
2019-03-17 06:41:10 +00:00
|
|
|
write_msg_to_log(log, tag, message.trimmingCharacters(in: .newlines))
|
2018-12-13 14:26:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 06:41:10 +00:00
|
|
|
func writeLog(to targetFile: String) -> Bool {
|
|
|
|
return write_log_to_file(targetFile, self.log) == 0
|
2018-12-13 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 06:41:10 +00:00
|
|
|
static func configureGlobal(tagged tag: String, withFilePath filePath: String?) {
|
2018-12-13 14:26:04 +00:00
|
|
|
if Logger.global != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let filePath = filePath else {
|
|
|
|
os_log("Unable to determine log destination path. Log will not be saved to file.", log: OSLog.default, type: .error)
|
|
|
|
return
|
|
|
|
}
|
2019-03-17 06:41:10 +00:00
|
|
|
guard let logger = try? Logger(tagged: tag, withFilePath: filePath) else {
|
2018-12-14 21:53:42 +00:00
|
|
|
os_log("Unable to open log file for writing. Log will not be saved to file.", log: OSLog.default, type: .error)
|
|
|
|
return
|
|
|
|
}
|
2019-01-28 12:16:12 +00:00
|
|
|
Logger.global = logger
|
2018-12-13 14:26:04 +00:00
|
|
|
var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown version"
|
|
|
|
if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
|
|
|
|
appVersion += " (\(appBuild))"
|
|
|
|
}
|
2020-12-04 10:01:36 +00:00
|
|
|
|
|
|
|
Logger.global?.log(message: "App version: \(appVersion)")
|
2018-12-13 12:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:08:10 +00:00
|
|
|
func wg_log(_ type: OSLogType, staticMessage msg: StaticString) {
|
|
|
|
os_log(msg, log: OSLog.default, type: type)
|
2018-12-13 14:26:04 +00:00
|
|
|
Logger.global?.log(message: "\(msg)")
|
2018-12-13 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func wg_log(_ type: OSLogType, message msg: String) {
|
|
|
|
os_log("%{public}s", log: OSLog.default, type: type, msg)
|
2018-12-13 14:26:04 +00:00
|
|
|
Logger.global?.log(message: msg)
|
2018-12-13 10:08:10 +00:00
|
|
|
}
|