Log view: Don't use a global array to store log entries

This commit is contained in:
Roopesh Chander 2019-04-10 15:58:00 +05:30
parent af9bd363bd
commit 7f4b5b4a7b
3 changed files with 12 additions and 8 deletions

View File

@ -107,7 +107,7 @@ err:
return ret; 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; struct log *log;
uint32_t l, i = cursor; 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 else
break; break;
} }
cb(line->line, line->time_ns); cb(line->line, line->time_ns, ctx);
cursor = (i + 1) % MAX_LINES; cursor = (i + 1) % MAX_LINES;
} }
free(log); free(log);

View File

@ -11,7 +11,7 @@
struct log; struct log;
void write_msg_to_log(struct log *log, const char *tag, const char *msg); 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); 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); struct log *open_log(const char *file_name);
void close_log(struct log *log); void close_log(struct log *log);

View File

@ -21,7 +21,9 @@ public class LogViewHelper {
} }
} }
static var logEntries = [LogEntry]() class LogEntries {
var entries: [LogEntry] = []
}
init?(logFilePath: String?) { init?(logFilePath: String?) {
guard let logFilePath = logFilePath else { return nil } guard let logFilePath = logFilePath else { return nil }
@ -34,19 +36,21 @@ public class LogViewHelper {
} }
func fetchLogEntriesSinceLastFetch(completion: @escaping ([LogViewHelper.LogEntry]) -> Void) { func fetchLogEntriesSinceLastFetch(completion: @escaping ([LogViewHelper.LogEntry]) -> Void) {
LogViewHelper.logEntries = [] var logEntries = LogEntries()
DispatchQueue.global(qos: .userInitiated).async { [weak self] in DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self = self else { return } 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 message = cStr != nil ? String(cString: cStr!) : ""
let date = Date(timeIntervalSince1970: Double(timestamp) / 1000000000) let date = Date(timeIntervalSince1970: Double(timestamp) / 1000000000)
let dateString = ISO8601DateFormatter.string(from: date, timeZone: TimeZone.current, formatOptions: LogViewHelper.formatOptions) 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 DispatchQueue.main.async { [weak self] in
guard let self = self else { return } guard let self = self else { return }
self.cursor = newCursor self.cursor = newCursor
completion(LogViewHelper.logEntries) completion(logEntries.entries)
} }
} }
} }