]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Log view: Don't use a global array to store log entries
authorRoopesh Chander <roop@roopc.net>
Wed, 10 Apr 2019 10:28:00 +0000 (15:58 +0530)
committerRoopesh Chander <roop@roopc.net>
Wed, 10 Apr 2019 12:27:36 +0000 (17:57 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/Shared/Logging/ringlogger.c
WireGuard/Shared/Logging/ringlogger.h
WireGuard/WireGuard/UI/LogViewHelper.swift

index 563483a4a5c880965f35a647ae07e801bf9c99a6..1edfc8dce73b19d94a23f2cf007b777c44ac0fed 100644 (file)
@@ -107,7 +107,7 @@ err:
        return ret;
 }
 
-uint32_t view_lines_from_cursor(const struct log *input_log, uint32_t cursor, void(*cb)(const char *, uint64_t))
+uint32_t view_lines_from_cursor(const struct log *input_log, uint32_t cursor, void *ctx, void(*cb)(const char *, uint64_t, void *))
 {
        struct log *log;
        uint32_t l, i = cursor;
@@ -132,7 +132,7 @@ uint32_t view_lines_from_cursor(const struct log *input_log, uint32_t cursor, vo
                        else
                                break;
                }
-               cb(line->line, line->time_ns);
+               cb(line->line, line->time_ns, ctx);
                cursor = (i + 1) % MAX_LINES;
        }
        free(log);
index 7f5b07496b6eb8c5f4d49a084f014a723e0ae666..c63f3e412b3b12b6686cfe3b07604c99fc911ac6 100644 (file)
@@ -11,7 +11,7 @@
 struct log;
 void write_msg_to_log(struct log *log, const char *tag, const char *msg);
 int write_log_to_file(const char *file_name, const struct log *input_log);
-uint32_t view_lines_from_cursor(const struct log *input_log, uint32_t cursor, void(*)(const char *, uint64_t));
+uint32_t view_lines_from_cursor(const struct log *input_log, uint32_t cursor, void *ctx, void(*)(const char *, uint64_t, void *));
 struct log *open_log(const char *file_name);
 void close_log(struct log *log);
 
index cadd66707220a4d76e2f593f126e08f4881c9d81..1d3619be6fa533fdf148f365dd87daf4372c1406 100644 (file)
@@ -21,7 +21,9 @@ public class LogViewHelper {
         }
     }
 
-    static var logEntries = [LogEntry]()
+    class LogEntries {
+        var entries: [LogEntry] = []
+    }
 
     init?(logFilePath: String?) {
         guard let logFilePath = logFilePath else { return nil }
@@ -34,19 +36,21 @@ public class LogViewHelper {
     }
 
     func fetchLogEntriesSinceLastFetch(completion: @escaping ([LogViewHelper.LogEntry]) -> Void) {
-        LogViewHelper.logEntries = []
+        var logEntries = 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 newCursor = view_lines_from_cursor(self.log, self.cursor, &logEntries) { cStr, timestamp, ctx 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))
+                if let logEntries = ctx?.bindMemory(to: LogEntries.self, capacity: 1) {
+                    logEntries.pointee.entries.append(LogEntry(timestamp: dateString, message: message))
+                }
             }
             DispatchQueue.main.async { [weak self] in
                 guard let self = self else { return }
                 self.cursor = newCursor
-                completion(LogViewHelper.logEntries)
+                completion(logEntries.entries)
             }
         }
     }