aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/LogViewHelper.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-03-26 13:21:00 +0530
committerRoopesh Chander <roop@roopc.net>2019-03-27 17:55:52 +0530
commitb7c3bd0d8c988058a78b6bea6703e2b682531939 (patch)
tree61d13ce93dee0ca2d751d943ad892fae8f4909ea /WireGuard/WireGuard/UI/LogViewHelper.swift
parentmacOS: Syntax highlighter: Free spans array (diff)
downloadwireguard-apple-b7c3bd0d8c988058a78b6bea6703e2b682531939.tar.xz
wireguard-apple-b7c3bd0d8c988058a78b6bea6703e2b682531939.zip
Add LogViewHelper
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r--WireGuard/WireGuard/UI/LogViewHelper.swift52
1 files changed, 52 insertions, 0 deletions
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)
+ }
+ }
+ }
+}