From 642b627d277cdb30b91682ba29b5c3a226d607d9 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 13 Dec 2018 15:26:04 +0100 Subject: Rewrite Logger This reverts all of Roop's changes to the C code, and then rewrites the logger logic to be cleaner. Signed-off-by: Jason A. Donenfeld --- WireGuard/Shared/Logging/Logger.swift | 88 +++++++++++++++++------------------ WireGuard/Shared/Logging/ringlogger.c | 46 ++++++++++++------ WireGuard/Shared/Logging/ringlogger.h | 21 ++------- 3 files changed, 78 insertions(+), 77 deletions(-) (limited to 'WireGuard/Shared/Logging') diff --git a/WireGuard/Shared/Logging/Logger.swift b/WireGuard/Shared/Logging/Logger.swift index 7df162a..f8ef70a 100644 --- a/WireGuard/Shared/Logging/Logger.swift +++ b/WireGuard/Shared/Logging/Logger.swift @@ -4,65 +4,65 @@ import Foundation import os.log -class Logger { - static var logPtr: UnsafeMutablePointer? +public class Logger { + static var global: Logger? - static func configure(withFilePath filePath: String) -> Bool { - let logPtr = filePath.withCString { filePathCStr -> UnsafeMutablePointer? in - return open_log(filePathCStr) + var log: OpaquePointer? + var tag: String + + init(withFilePath filePath: String, withTag tag: String) { + self.tag = tag + self.log = filePath.withCString { fileC -> OpaquePointer? in + open_log(fileC) + } + if self.log == nil { + os_log("Cannot open log file for writing. Log will not be saved to file.", log: OSLog.default, type: .error) } - Logger.logPtr = logPtr - return (logPtr != nil) } - static func writeLog(mergedWith otherLogFile: String, tag: String, otherTag: String, to targetFile: String) -> Bool { - let otherlogPtr = otherLogFile.withCString { otherLogFileCStr -> UnsafeMutablePointer? in - return open_log(otherLogFileCStr) + func log(message: String) { + guard let log = log else { return } + String(format: "[%@] %@", tag, message.trimmingCharacters(in: .newlines)).withCString { messageC in + write_msg_to_log(log, messageC) } - if let thisLogPtr = Logger.logPtr, let otherlogPtr = otherlogPtr { - return targetFile.withCString { targetFileCStr -> Bool in - return tag.withCString { tagCStr -> Bool in - return otherTag.withCString { otherTagCStr -> Bool in - let returnValue = write_logs_to_file(targetFileCStr, tagCStr, thisLogPtr, otherTagCStr, otherlogPtr) - return (returnValue == 0) - } - } - } + } + + func writeLog(mergedWith otherLogFile: String, to targetFile: String) -> Bool { + guard let log = log else { return false } + guard let other = otherLogFile.withCString({ otherC -> OpaquePointer? in + return open_log(otherC) + }) else { return false } + defer { close_log(other) } + return targetFile.withCString { fileC -> Bool in + return write_logs_to_file(fileC, log, other) == 0 } - return false } -} -func wg_log_versions_to_file() { - var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown version" - if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String { - appVersion += " (\(appBuild))" + static func configureGlobal(withFilePath filePath: String?, withTag tag: String) { + if Logger.global != nil { + return + } + guard let filePath = filePath else { + os_log("Unable to determine log destination path. Log will not be saved to file.", log: OSLog.default, type: .error) + return + } + Logger.global = Logger(withFilePath: filePath, withTag: tag) + var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown version" + if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String { + appVersion += " (\(appBuild))" + } + let goBackendVersion = WIREGUARD_GO_VERSION + Logger.global?.log(message: "App version: \(appVersion); Go backend version: \(goBackendVersion)") + } - let goBackendVersion = WIREGUARD_GO_VERSION - file_log(message: "App version: \(appVersion); Go backend version: \(goBackendVersion)") } func wg_log(_ type: OSLogType, staticMessage msg: StaticString) { - // Write to os log os_log(msg, log: OSLog.default, type: type) - // Write to file log - let msgString: String = msg.withUTF8Buffer { (ptr: UnsafeBufferPointer) -> String in - return String(decoding: ptr, as: UTF8.self) - } - file_log(message: msgString) + Logger.global?.log(message: "\(msg)") } func wg_log(_ type: OSLogType, message msg: String) { - // Write to os log os_log("%{public}s", log: OSLog.default, type: type, msg) - // Write to file log - file_log(message: msg) -} - -private func file_log(message: String) { - message.withCString { messageCStr in - if let logPtr = Logger.logPtr { - write_msg_to_log(logPtr, messageCStr) - } - } + Logger.global?.log(message: msg) } diff --git a/WireGuard/Shared/Logging/ringlogger.c b/WireGuard/Shared/Logging/ringlogger.c index 5c6fae6..e3dfe1a 100644 --- a/WireGuard/Shared/Logging/ringlogger.c +++ b/WireGuard/Shared/Logging/ringlogger.c @@ -16,6 +16,23 @@ #include #include "ringlogger.h" +enum { + MAX_LOG_LINE_LENGTH = 512, + MAX_LINES = 1024, + MAGIC = 0xdeadbeefU +}; + +struct log_line { + struct timeval tv; + char line[MAX_LOG_LINE_LENGTH]; +}; + +struct log { + struct { uint32_t first, len; } header; + struct log_line lines[MAX_LINES]; + uint32_t magic; +}; + void write_msg_to_log(struct log *log, const char *msg) { struct log_line *line = &log->lines[(log->header.first + log->header.len) % MAX_LINES]; @@ -29,13 +46,6 @@ void write_msg_to_log(struct log *log, const char *msg) strncpy(line->line, msg, MAX_LOG_LINE_LENGTH - 1); line->line[MAX_LOG_LINE_LENGTH - 1] = '\0'; - // Trim trailing newlines - unsigned long length = strlen(msg); - while ((length > 0) && (msg[length - 1] == '\n' || msg[length - 1] == '\r')) { - line->line[length - 1] = '\0'; - length--; - } - msync(&log->header, sizeof(log->header), MS_ASYNC); msync(line, sizeof(*line), MS_ASYNC); } @@ -44,12 +54,12 @@ static bool first_before_second(const struct log_line *line1, const struct log_l { if (line1->tv.tv_sec <= line2->tv.tv_sec) return true; - else if (line1->tv.tv_sec == line2->tv.tv_sec) + if (line1->tv.tv_sec == line2->tv.tv_sec) return line1->tv.tv_usec <= line2->tv.tv_usec; return false; } -int write_logs_to_file(const char *file_name, const char *tag1, const struct log *log1, const char *tag2, const struct log *log2) +int write_logs_to_file(const char *file_name, const struct log *log1, const struct log *log2) { uint32_t i1, i2, len1 = log1->header.len, len2 = log2->header.len; char buf[MAX_LOG_LINE_LENGTH]; @@ -68,22 +78,19 @@ int write_logs_to_file(const char *file_name, const char *tag1, const struct log const struct log_line *line1 = &log1->lines[(log1->header.first + i1) % MAX_LINES]; const struct log_line *line2 = &log2->lines[(log2->header.first + i2) % MAX_LINES]; const struct log_line *line; - const char *tag; if (i1 < len1 && (i2 >= len2 || first_before_second(line1, line2))) { line = line1; - tag = (const char *) tag1; ++i1; } else if (i2 < len2 && (i1 >= len1 || first_before_second(line2, line1))) { line = line2; - tag = (const char *) tag2; ++i2; } else { break; } memcpy(buf, line->line, MAX_LOG_LINE_LENGTH); buf[MAX_LOG_LINE_LENGTH - 1] = '\0'; - if (fprintf(file, "%lu.%06d: [%s] %s\n", line->tv.tv_sec, line->tv.tv_usec, tag, buf) < 0) { + if (fprintf(file, "%lu.%06d: %s\n", line->tv.tv_sec, line->tv.tv_usec, buf) < 0) { int ret = -errno; fclose(file); return ret; @@ -102,10 +109,10 @@ struct log *open_log(const char *file_name) if (fd < 0) return NULL; if (ftruncate(fd, sizeof(*log))) - return NULL; + goto err; log = mmap(NULL, sizeof(*log), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (log == MAP_FAILED) - return NULL; + goto err; close(fd); if (log->magic != MAGIC) { @@ -115,4 +122,13 @@ struct log *open_log(const char *file_name) } return log; + +err: + close(fd); + return NULL; +} + +void close_log(struct log *log) +{ + munmap(log, sizeof(*log)); } diff --git a/WireGuard/Shared/Logging/ringlogger.h b/WireGuard/Shared/Logging/ringlogger.h index be1d33c..ad58fb8 100644 --- a/WireGuard/Shared/Logging/ringlogger.h +++ b/WireGuard/Shared/Logging/ringlogger.h @@ -6,25 +6,10 @@ #ifndef RINGLOGGER_H #define RINGLOGGER_H -enum { - MAX_LOG_LINE_LENGTH = 512, - MAX_LINES = 1024, - MAGIC = 0xdeadbeefU -}; - -struct log_line { - struct timeval tv; - char line[MAX_LOG_LINE_LENGTH]; -}; - -struct log { - struct { uint32_t first, len; } header; - struct log_line lines[MAX_LINES]; - uint32_t magic; -}; - +struct log; void write_msg_to_log(struct log *log, const char *msg); -int write_logs_to_file(const char *file_name, const char *tag1, const struct log *log1, const char *tag2, const struct log *log2); +int write_logs_to_file(const char *file_name, const struct log *log1, const struct log *log2); struct log *open_log(const char *file_name); +void close_log(struct log *log); #endif -- cgit v1.2.3-59-g8ed1b