diff options
author | Roopesh Chander <roop@roopc.net> | 2018-12-13 15:38:10 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-12-13 17:37:14 +0530 |
commit | ae7fb7323faf6321d07dd855ea4f7641d2424ec0 (patch) | |
tree | e06c8af688ca2de4c0464b7d206a140d9cf92f3d /WireGuard/Shared | |
parent | Avoid using 'VPN' in code where possible (diff) | |
download | wireguard-apple-ae7fb7323faf6321d07dd855ea4f7641d2424ec0.tar.xz wireguard-apple-ae7fb7323faf6321d07dd855ea4f7641d2424ec0.zip |
Logging: Use ringlogger for logging from the extension
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/Shared')
-rw-r--r-- | WireGuard/Shared/FileManager+Extension.swift | 10 | ||||
-rw-r--r-- | WireGuard/Shared/Logging/Logger.swift | 54 | ||||
-rw-r--r-- | WireGuard/Shared/Logging/ringlogger.c (renamed from WireGuard/Shared/RingLogger/ringlogger.c) | 17 | ||||
-rw-r--r-- | WireGuard/Shared/Logging/ringlogger.h (renamed from WireGuard/Shared/RingLogger/ringlogger.h) | 18 |
4 files changed, 80 insertions, 19 deletions
diff --git a/WireGuard/Shared/FileManager+Extension.swift b/WireGuard/Shared/FileManager+Extension.swift index 535b999..06f9e44 100644 --- a/WireGuard/Shared/FileManager+Extension.swift +++ b/WireGuard/Shared/FileManager+Extension.swift @@ -14,7 +14,15 @@ extension FileManager { os_log("Can't obtain shared folder URL", log: OSLog.default, type: .error) return nil } - return sharedFolderURL.appendingPathComponent("last-activated-tunnel-log.txt") + return sharedFolderURL.appendingPathComponent("tunnel-log.txt") + } + + static var appLogFileURL: URL? { + guard let documentDirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { + os_log("Can't obtain app documents folder URL", log: OSLog.default, type: .error) + return nil + } + return documentDirURL.appendingPathComponent("app-log.txt") } static func deleteFile(at url: URL) -> Bool { diff --git a/WireGuard/Shared/Logging/Logger.swift b/WireGuard/Shared/Logging/Logger.swift new file mode 100644 index 0000000..bc0ffd4 --- /dev/null +++ b/WireGuard/Shared/Logging/Logger.swift @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import os.log + +class Logger { + static var logPtr: UnsafeMutablePointer<log>? + + static func configure(withFilePath filePath: String) -> Bool { + let logPtr = filePath.withCString { filePathCStr -> UnsafeMutablePointer<log>? in + return open_log(filePathCStr) + } + Logger.logPtr = logPtr + return (logPtr != nil) + } + + static func writeLog(mergedWith otherLogFile: String, to targetFile: String) -> Bool { + let otherlogPtr = otherLogFile.withCString { otherLogFileCStr -> UnsafeMutablePointer<log>? in + return open_log(otherLogFileCStr) + } + if let thisLogPtr = Logger.logPtr, let otherlogPtr = otherlogPtr { + return targetFile.withCString { targetFileCStr -> Bool in + let returnValue = write_logs_to_file(targetFileCStr, thisLogPtr, otherlogPtr) + return (returnValue == 0) + } + } + return false + } +} + +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<UInt8>) -> String in + return String(decoding: ptr, as: UTF8.self) + } + file_log(type: type, message: msgString) +} + +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(type: type, message: msg) +} + +private func file_log(type: OSLogType, message: String) { + message.withCString { messageCStr in + if let logPtr = Logger.logPtr { + write_msg_to_log(logPtr, messageCStr) + } + } +} diff --git a/WireGuard/Shared/RingLogger/ringlogger.c b/WireGuard/Shared/Logging/ringlogger.c index d39b9d7..ea862de 100644 --- a/WireGuard/Shared/RingLogger/ringlogger.c +++ b/WireGuard/Shared/Logging/ringlogger.c @@ -16,23 +16,6 @@ #include <sys/mman.h> #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]; diff --git a/WireGuard/Shared/RingLogger/ringlogger.h b/WireGuard/Shared/Logging/ringlogger.h index d90e0c5..a8d07c0 100644 --- a/WireGuard/Shared/RingLogger/ringlogger.h +++ b/WireGuard/Shared/Logging/ringlogger.h @@ -6,7 +6,23 @@ #ifndef RINGLOGGER_H #define RINGLOGGER_H -struct log; +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); 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); |