aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Logging/Logger.swift
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-12-13 15:26:04 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-13 18:06:37 +0100
commit642b627d277cdb30b91682ba29b5c3a226d607d9 (patch)
tree2df2a73a01f7123b313ed3c23950b1437f934fff /WireGuard/Shared/Logging/Logger.swift
parentMore reliable logo sizing (diff)
downloadwireguard-apple-642b627d277cdb30b91682ba29b5c3a226d607d9.tar.xz
wireguard-apple-642b627d277cdb30b91682ba29b5c3a226d607d9.zip
Rewrite Logger
This reverts all of Roop's changes to the C code, and then rewrites the logger logic to be cleaner. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'WireGuard/Shared/Logging/Logger.swift')
-rw-r--r--WireGuard/Shared/Logging/Logger.swift88
1 files changed, 44 insertions, 44 deletions
diff --git a/WireGuard/Shared/Logging/Logger.swift b/WireGuard/Shared/Logging/Logger.swift
index 7df162a..f8ef70a 100644
--- a/WireGuard/Shared/Logging/Logger.swift
+++ b/WireGuard/Shared/Logging/Logger.swift
@@ -4,65 +4,65 @@
import Foundation
import os.log
-class Logger {
- static var logPtr: UnsafeMutablePointer<log>?
+public class Logger {
+ static var global: Logger?
- static func configure(withFilePath filePath: String) -> Bool {
- let logPtr = filePath.withCString { filePathCStr -> UnsafeMutablePointer<log>? in
- return open_log(filePathCStr)
+ var log: OpaquePointer?
+ var tag: String
+
+ init(withFilePath filePath: String, withTag tag: String) {
+ self.tag = tag
+ self.log = filePath.withCString { fileC -> OpaquePointer? in
+ open_log(fileC)
+ }
+ if self.log == nil {
+ os_log("Cannot open log file for writing. Log will not be saved to file.", log: OSLog.default, type: .error)
}
- Logger.logPtr = logPtr
- return (logPtr != nil)
}
- static func writeLog(mergedWith otherLogFile: String, tag: String, otherTag: String, to targetFile: String) -> Bool {
- let otherlogPtr = otherLogFile.withCString { otherLogFileCStr -> UnsafeMutablePointer<log>? in
- return open_log(otherLogFileCStr)
+ func log(message: String) {
+ guard let log = log else { return }
+ String(format: "[%@] %@", tag, message.trimmingCharacters(in: .newlines)).withCString { messageC in
+ write_msg_to_log(log, messageC)
}
- if let thisLogPtr = Logger.logPtr, let otherlogPtr = otherlogPtr {
- return targetFile.withCString { targetFileCStr -> Bool in
- return tag.withCString { tagCStr -> Bool in
- return otherTag.withCString { otherTagCStr -> Bool in
- let returnValue = write_logs_to_file(targetFileCStr, tagCStr, thisLogPtr, otherTagCStr, otherlogPtr)
- return (returnValue == 0)
- }
- }
- }
+ }
+
+ func writeLog(mergedWith otherLogFile: String, to targetFile: String) -> Bool {
+ guard let log = log else { return false }
+ guard let other = otherLogFile.withCString({ otherC -> OpaquePointer? in
+ return open_log(otherC)
+ }) else { return false }
+ defer { close_log(other) }
+ return targetFile.withCString { fileC -> Bool in
+ return write_logs_to_file(fileC, log, other) == 0
}
- return false
}
-}
-func wg_log_versions_to_file() {
- var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown version"
- if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
- appVersion += " (\(appBuild))"
+ static func configureGlobal(withFilePath filePath: String?, withTag tag: String) {
+ 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
+ }
+ Logger.global = Logger(withFilePath: filePath, withTag: tag)
+ 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)")
+
}
- let goBackendVersion = WIREGUARD_GO_VERSION
- file_log(message: "App version: \(appVersion); Go backend version: \(goBackendVersion)")
}
func wg_log(_ type: OSLogType, staticMessage msg: StaticString) {
- // Write to os log
os_log(msg, log: OSLog.default, type: type)
- // Write to file log
- let msgString: String = msg.withUTF8Buffer { (ptr: UnsafeBufferPointer<UInt8>) -> String in
- return String(decoding: ptr, as: UTF8.self)
- }
- file_log(message: msgString)
+ Logger.global?.log(message: "\(msg)")
}
func wg_log(_ type: OSLogType, message msg: String) {
- // Write to os log
os_log("%{public}s", log: OSLog.default, type: type, msg)
- // Write to file log
- file_log(message: msg)
-}
-
-private func file_log(message: String) {
- message.withCString { messageCStr in
- if let logPtr = Logger.logPtr {
- write_msg_to_log(logPtr, messageCStr)
- }
- }
+ Logger.global?.log(message: msg)
}