]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Add LogViewHelper
authorRoopesh Chander <roop@roopc.net>
Tue, 26 Mar 2019 07:51:00 +0000 (13:21 +0530)
committerRoopesh Chander <roop@roopc.net>
Wed, 27 Mar 2019 12:25:52 +0000 (17:55 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard.xcodeproj/project.pbxproj
WireGuard/WireGuard/UI/LogViewHelper.swift [new file with mode: 0644]

index 9a1c3993fffb71c91ffada5f1325852fdcd97c57..ed00af9fd1d63a4cfc710a2cbf73bca8e016f9fc 100644 (file)
@@ -85,6 +85,8 @@
                6F8F0D7222258153000E8335 /* ActivateOnDemandViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F8F0D7022258153000E8335 /* ActivateOnDemandViewModel.swift */; };
                6F8F0D7422267AD2000E8335 /* ChevronCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F8F0D7322267AD2000E8335 /* ChevronCell.swift */; };
                6F8F0D7722267C57000E8335 /* SSIDOptionEditTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F8F0D7622267C57000E8335 /* SSIDOptionEditTableViewController.swift */; };
+               6F907C9C224663A2003CED21 /* LogViewHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F907C9B224663A2003CED21 /* LogViewHelper.swift */; };
+               6F907C9D224663A2003CED21 /* LogViewHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F907C9B224663A2003CED21 /* LogViewHelper.swift */; };
                6F919EC3218A2AE90023B400 /* ErrorPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F919EC2218A2AE90023B400 /* ErrorPresenter.swift */; };
                6F919ED9218C65C50023B400 /* wireguard_doc_logo_22x29.png in Resources */ = {isa = PBXBuildFile; fileRef = 6F919ED5218C65C50023B400 /* wireguard_doc_logo_22x29.png */; };
                6F919EDA218C65C50023B400 /* wireguard_doc_logo_44x58.png in Resources */ = {isa = PBXBuildFile; fileRef = 6F919ED6218C65C50023B400 /* wireguard_doc_logo_44x58.png */; };
                6F8F0D7022258153000E8335 /* ActivateOnDemandViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivateOnDemandViewModel.swift; sourceTree = "<group>"; };
                6F8F0D7322267AD2000E8335 /* ChevronCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChevronCell.swift; sourceTree = "<group>"; };
                6F8F0D7622267C57000E8335 /* SSIDOptionEditTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SSIDOptionEditTableViewController.swift; sourceTree = "<group>"; };
+               6F907C9B224663A2003CED21 /* LogViewHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogViewHelper.swift; sourceTree = "<group>"; };
                6F919EC2218A2AE90023B400 /* ErrorPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorPresenter.swift; sourceTree = "<group>"; };
                6F919ED5218C65C50023B400 /* wireguard_doc_logo_22x29.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wireguard_doc_logo_22x29.png; sourceTree = "<group>"; };
                6F919ED6218C65C50023B400 /* wireguard_doc_logo_44x58.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wireguard_doc_logo_44x58.png; sourceTree = "<group>"; };
                                6FBA103D21D6B6D70051C35F /* TunnelImporter.swift */,
                                6FBA103A21D6B4280051C35F /* ErrorPresenterProtocol.swift */,
                                6B62E45E220A6FA900EF34A6 /* PrivateDataConfirmation.swift */,
+                               6F907C9B224663A2003CED21 /* LogViewHelper.swift */,
                        );
                        path = UI;
                        sourceTree = "<group>";
                                6FB1BDC521D50F0300A991BF /* Endpoint.swift in Sources */,
                                6FB1BDC621D50F0300A991BF /* DNSServer.swift in Sources */,
                                6FB1BDC721D50F0300A991BF /* InterfaceConfiguration.swift in Sources */,
+                               6F907C9D224663A2003CED21 /* LogViewHelper.swift in Sources */,
                                6FB1BDC821D50F0300A991BF /* PeerConfiguration.swift in Sources */,
                                6FB1BDC921D50F0300A991BF /* FileManager+Extension.swift in Sources */,
                                6FB1BD6021D2607A00A991BF /* AppDelegate.swift in Sources */,
                                6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */,
                                6FFA5DA42197085D0001E2F7 /* ActivateOnDemandOption.swift in Sources */,
                                5F4541B221CBFAEE00994C13 /* String+ArrayConversion.swift in Sources */,
+                               6F907C9C224663A2003CED21 /* LogViewHelper.swift in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
diff --git a/WireGuard/WireGuard/UI/LogViewHelper.swift b/WireGuard/WireGuard/UI/LogViewHelper.swift
new file mode 100644 (file)
index 0000000..e3ebacd
--- /dev/null
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
+
+import Foundation
+
+public class LogViewHelper {
+    var log: OpaquePointer
+    var cursor: UInt32 = UINT32_MAX
+    static let formatOptions: ISO8601DateFormatter.Options = [
+        .withYear, .withMonth, .withDay, .withTime,
+        .withDashSeparatorInDate, .withColonSeparatorInTime, .withSpaceBetweenDateAndTime,
+        .withFractionalSeconds
+    ]
+
+    struct LogEntry {
+        let timestamp: String
+        let message: String
+
+        func text() -> String {
+            return timestamp + " " + message
+        }
+    }
+
+    static var logEntries = [LogEntry]()
+
+    init?(logFilePath: String?) {
+        guard let logFilePath = logFilePath else { return nil }
+        guard let log = open_log(logFilePath) else { return nil }
+        self.log = log
+    }
+
+    deinit {
+        close_log(self.log)
+    }
+
+    func fetchLogEntriesSinceLastFetch(completion: @escaping ([LogViewHelper.LogEntry]) -> Void) {
+        LogViewHelper.logEntries = []
+        DispatchQueue.global(qos: .userInitiated).async { [weak self] in
+            guard let self = self else { return }
+            let newCursor = view_lines_from_cursor(self.log, self.cursor) { cStr, timestamp in
+                let message = cStr != nil ? String(cString: cStr!) : ""
+                let date = Date(timeIntervalSince1970: Double(timestamp) / 1000000000)
+                let dateString = ISO8601DateFormatter.string(from: date, timeZone: TimeZone.current, formatOptions: LogViewHelper.formatOptions)
+                LogViewHelper.logEntries.append(LogEntry(timestamp: dateString, message: message))
+            }
+            DispatchQueue.main.async { [weak self] in
+                self?.cursor = newCursor
+                completion(LogViewHelper.logEntries)
+            }
+        }
+    }
+}