From b7c3bd0d8c988058a78b6bea6703e2b682531939 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Tue, 26 Mar 2019 13:21:00 +0530 Subject: Add LogViewHelper Signed-off-by: Roopesh Chander --- WireGuard/WireGuard.xcodeproj/project.pbxproj | 6 ++++ WireGuard/WireGuard/UI/LogViewHelper.swift | 52 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 WireGuard/WireGuard/UI/LogViewHelper.swift (limited to 'WireGuard') diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index 9a1c399..ed00af9 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -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 */; }; @@ -330,6 +332,7 @@ 6F8F0D7022258153000E8335 /* ActivateOnDemandViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivateOnDemandViewModel.swift; sourceTree = ""; }; 6F8F0D7322267AD2000E8335 /* ChevronCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChevronCell.swift; sourceTree = ""; }; 6F8F0D7622267C57000E8335 /* SSIDOptionEditTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SSIDOptionEditTableViewController.swift; sourceTree = ""; }; + 6F907C9B224663A2003CED21 /* LogViewHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogViewHelper.swift; sourceTree = ""; }; 6F919EC2218A2AE90023B400 /* ErrorPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorPresenter.swift; sourceTree = ""; }; 6F919ED5218C65C50023B400 /* wireguard_doc_logo_22x29.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wireguard_doc_logo_22x29.png; sourceTree = ""; }; 6F919ED6218C65C50023B400 /* wireguard_doc_logo_44x58.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wireguard_doc_logo_44x58.png; sourceTree = ""; }; @@ -530,6 +533,7 @@ 6FBA103D21D6B6D70051C35F /* TunnelImporter.swift */, 6FBA103A21D6B4280051C35F /* ErrorPresenterProtocol.swift */, 6B62E45E220A6FA900EF34A6 /* PrivateDataConfirmation.swift */, + 6F907C9B224663A2003CED21 /* LogViewHelper.swift */, ); path = UI; sourceTree = ""; @@ -1280,6 +1284,7 @@ 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 */, @@ -1375,6 +1380,7 @@ 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 index 0000000..e3ebacd --- /dev/null +++ b/WireGuard/WireGuard/UI/LogViewHelper.swift @@ -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) + } + } + } +} -- cgit v1.2.3-59-g8ed1b