2018-12-13 10:08:10 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
|
|
|
|
|
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
|
2018-12-13 14:26:04 +00:00
|
|
|
|
2018-12-14 21:53:42 +00:00
|
|
|
init(withFilePath filePath: String) throws {
|
|
|
|
guard let log = open_log(filePath) else { throw LoggerError.openFailure }
|
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-12-14 21:53:42 +00:00
|
|
|
write_msg_to_log(log, message.trimmingCharacters(in: .newlines))
|
2018-12-13 14:26:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 21:53:42 +00:00
|
|
|
func writeLog(called ourTag: String, mergedWith otherLogFile: String, called otherTag: String, to targetFile: String) -> Bool {
|
2018-12-13 18:02:48 +00:00
|
|
|
guard let other = open_log(otherLogFile) else { return false }
|
2018-12-14 21:53:42 +00:00
|
|
|
let ret = write_logs_to_file(targetFile, log, ourTag, other, otherTag)
|
2018-12-13 18:02:48 +00:00
|
|
|
close_log(other)
|
|
|
|
return ret == 0
|
2018-12-13 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 21:53:42 +00:00
|
|
|
static func configureGlobal(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
|
|
|
|
}
|
2018-12-14 21:53:42 +00:00
|
|
|
do {
|
|
|
|
try Logger.global = Logger(withFilePath: filePath)
|
|
|
|
} catch {
|
|
|
|
os_log("Unable to open log file for writing. Log will not be saved to file.", log: OSLog.default, type: .error)
|
|
|
|
return
|
|
|
|
}
|
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))"
|
|
|
|
}
|
|
|
|
let goBackendVersion = WIREGUARD_GO_VERSION
|
|
|
|
Logger.global?.log(message: "App version: \(appVersion); Go backend version: \(goBackendVersion)")
|
|
|
|
|
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
|
|
|
}
|