]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Simplify logging tags
authorJason A. Donenfeld <Jason@zx2c4.com>
Fri, 14 Dec 2018 21:53:42 +0000 (22:53 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 14 Dec 2018 21:53:42 +0000 (22:53 +0100)
This was roop's initial idea, and it turns out to be the better one, now
that we can pass cstrings more easily.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
WireGuard/Shared/Logging/Logger.swift
WireGuard/Shared/Logging/ringlogger.c
WireGuard/Shared/Logging/ringlogger.h
WireGuard/WireGuard/UI/iOS/AppDelegate.swift
WireGuard/WireGuard/UI/iOS/Settings/SettingsTableViewController.swift
WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift

index b8dbdfd45dfdbe405df244503373cde353f50aa2..10b90d8034a8370e14418876566480a090e88a88 100644 (file)
@@ -5,33 +5,34 @@ import Foundation
 import os.log
 
 public class Logger {
+    enum LoggerError: Error {
+        case openFailure
+    }
     static var global: Logger?
 
-    var log: OpaquePointer?
-    var tag: String
+    var log: OpaquePointer
 
-    init(withFilePath filePath: String, withTag tag: String) {
-        self.tag = tag
-        self.log = open_log(filePath)
-        if self.log == nil {
-            os_log("Cannot open log file for writing. Log will not be saved to file.", log: OSLog.default, type: .error)
-        }
+    init(withFilePath filePath: String) throws {
+        guard let log = open_log(filePath) else { throw LoggerError.openFailure }
+        self.log = log
+    }
+
+    deinit {
+        close_log(self.log)
     }
 
     func log(message: String) {
-        guard let log = log else { return }
-        write_msg_to_log(log, String(format: "[%@] %@", tag, message.trimmingCharacters(in: .newlines)))
+        write_msg_to_log(log, message.trimmingCharacters(in: .newlines))
     }
 
-    func writeLog(mergedWith otherLogFile: String, to targetFile: String) -> Bool {
-        guard let log = log else { return false }
+    func writeLog(called ourTag: String, mergedWith otherLogFile: String, called otherTag: String, to targetFile: String) -> Bool {
         guard let other = open_log(otherLogFile) else { return false }
-        let ret = write_logs_to_file(targetFile, log, other)
+        let ret = write_logs_to_file(targetFile, log, ourTag, other, otherTag)
         close_log(other)
         return ret == 0
     }
 
-    static func configureGlobal(withFilePath filePath: String?, withTag tag: String) {
+    static func configureGlobal(withFilePath filePath: String?) {
         if Logger.global != nil {
             return
         }
@@ -39,7 +40,12 @@ public class Logger {
             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)
+        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
+        }
         var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown version"
         if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
             appVersion += " (\(appBuild))"
index e3dfe1a4759a29ed17cb570c3714564b6761ff58..a30b3818dbe1cc86398f43b0d426d99cd08bc53f 100644 (file)
@@ -19,7 +19,7 @@
 enum {
        MAX_LOG_LINE_LENGTH = 512,
        MAX_LINES = 1024,
-       MAGIC = 0xdeadbeefU
+       MAGIC = 0xbeefbabeU
 };
 
 struct log_line {
@@ -59,7 +59,7 @@ static bool first_before_second(const struct log_line *line1, const struct log_l
        return false;
 }
 
-int write_logs_to_file(const char *file_name, const struct log *log1, const struct log *log2)
+int write_logs_to_file(const char *file_name, const struct log *log1, const char *tag1, const struct log *log2, const char *tag2)
 {
        uint32_t i1, i2, len1 = log1->header.len, len2 = log2->header.len;
        char buf[MAX_LOG_LINE_LENGTH];
@@ -78,19 +78,22 @@ int write_logs_to_file(const char *file_name, const struct log *log1, const stru
                const struct log_line *line1 = &log1->lines[(log1->header.first + i1) % MAX_LINES];
                const struct log_line *line2 = &log2->lines[(log2->header.first + i2) % MAX_LINES];
                const struct log_line *line;
+               const char *tag;
 
                if (i1 < len1 && (i2 >= len2 || first_before_second(line1, line2))) {
                        line = line1;
+                       tag = tag1;
                        ++i1;
                } else if (i2 < len2 && (i1 >= len1 || first_before_second(line2, line1))) {
                        line = line2;
+                       tag = tag2;
                        ++i2;
                } else {
                        break;
                }
                memcpy(buf, line->line, MAX_LOG_LINE_LENGTH);
                buf[MAX_LOG_LINE_LENGTH - 1] = '\0';
-               if (fprintf(file, "%lu.%06d: %s\n", line->tv.tv_sec, line->tv.tv_usec, buf) < 0) {
+               if (fprintf(file, "%lu.%06d: [%s] %s\n", line->tv.tv_sec, line->tv.tv_usec, tag, buf) < 0) {
                        int ret = -errno;
                        fclose(file);
                        return ret;
index ad58fb882e97792ecc9f3066a04bf4967d9efe97..ca097b26bffe6d43bcffe32a52d1bef5e7538e49 100644 (file)
@@ -8,7 +8,7 @@
 
 struct log;
 void write_msg_to_log(struct log *log, const char *msg);
-int write_logs_to_file(const char *file_name, const struct log *log1, const struct log *log2);
+int write_logs_to_file(const char *file_name, const struct log *log1, const char *tag1, const struct log *log2, const char *tag2);
 struct log *open_log(const char *file_name);
 void close_log(struct log *log);
 
index 32c1286a37586af32605d76e1d27010e8fad4e1f..31463465fcee8b8c89f5b61546936a1300e08f91 100644 (file)
@@ -12,7 +12,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 
     func application(_ application: UIApplication,
                      willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
-        Logger.configureGlobal(withFilePath: FileManager.appLogFileURL?.path, withTag: "APP")
+        Logger.configureGlobal(withFilePath: FileManager.appLogFileURL?.path)
 
         let window = UIWindow(frame: UIScreen.main.bounds)
         window.backgroundColor = UIColor.white
index c87d452daa1b8188b8996131e7894450468c240b..5e8aee6006d02aaf4e09bb1731e8bf7537e2de84 100644 (file)
@@ -119,7 +119,7 @@ class SettingsTableViewController: UITableViewController {
                 return
             }
 
-            let isWritten = Logger.global?.writeLog(mergedWith: networkExtensionLogFilePath, to: destinationURL.path) ?? false
+            let isWritten = Logger.global?.writeLog(called: "APP", mergedWith: networkExtensionLogFilePath, called: "NET", to: destinationURL.path) ?? false
             guard isWritten else {
                 ErrorPresenter.showErrorAlert(title: "Log export failed", message: "Unable to write logs to file", from: self)
                 return
index 559c7c26de57c2ee2c461d75b0dcd2b1a111460f..e2588f82691b4dddadb80192298e3eb5c1bb1eb6 100644 (file)
@@ -140,7 +140,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
     }
 
     private func configureLogger() {
-        Logger.configureGlobal(withFilePath: FileManager.networkExtensionLogFileURL?.path, withTag: "EXT")
+        Logger.configureGlobal(withFilePath: FileManager.networkExtensionLogFileURL?.path)
         wgSetLogger { level, msgC in
             guard let msgC = msgC else { return }
             let logType: OSLogType