From 98ebd552086e3bc5ff2c6f74bdeec80d24dfede1 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 10 Apr 2019 15:58:00 +0530 Subject: Log view: Don't use a global array to store log entries Signed-off-by: Roopesh Chander --- WireGuard/Shared/Logging/ringlogger.c | 4 ++-- WireGuard/Shared/Logging/ringlogger.h | 2 +- WireGuard/WireGuard/UI/LogViewHelper.swift | 14 +++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-) (limited to 'WireGuard') diff --git a/WireGuard/Shared/Logging/ringlogger.c b/WireGuard/Shared/Logging/ringlogger.c index 563483a..1edfc8d 100644 --- a/WireGuard/Shared/Logging/ringlogger.c +++ b/WireGuard/Shared/Logging/ringlogger.c @@ -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); diff --git a/WireGuard/Shared/Logging/ringlogger.h b/WireGuard/Shared/Logging/ringlogger.h index 7f5b074..c63f3e4 100644 --- a/WireGuard/Shared/Logging/ringlogger.h +++ b/WireGuard/Shared/Logging/ringlogger.h @@ -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); diff --git a/WireGuard/WireGuard/UI/LogViewHelper.swift b/WireGuard/WireGuard/UI/LogViewHelper.swift index cadd667..1d3619b 100644 --- a/WireGuard/WireGuard/UI/LogViewHelper.swift +++ b/WireGuard/WireGuard/UI/LogViewHelper.swift @@ -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) } } } -- cgit v1.2.3-59-g8ed1b