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 <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-12-13 15:26:04 +01:00
parent 38accad27d
commit 642b627d27
6 changed files with 87 additions and 115 deletions

View File

@ -4,65 +4,65 @@
import Foundation import Foundation
import os.log import os.log
class Logger { public class Logger {
static var logPtr: UnsafeMutablePointer<log>? static var global: Logger?
static func configure(withFilePath filePath: String) -> Bool { var log: OpaquePointer?
let logPtr = filePath.withCString { filePathCStr -> UnsafeMutablePointer<log>? in var tag: String
return open_log(filePathCStr)
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 { func log(message: String) {
let otherlogPtr = otherLogFile.withCString { otherLogFileCStr -> UnsafeMutablePointer<log>? in guard let log = log else { return }
return open_log(otherLogFileCStr) 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)
} }
} }
}
}
return false
}
}
func wg_log_versions_to_file() { 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
}
}
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" var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown version"
if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String { if let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
appVersion += " (\(appBuild))" appVersion += " (\(appBuild))"
} }
let goBackendVersion = WIREGUARD_GO_VERSION let goBackendVersion = WIREGUARD_GO_VERSION
file_log(message: "App version: \(appVersion); Go backend version: \(goBackendVersion)") Logger.global?.log(message: "App version: \(appVersion); Go backend version: \(goBackendVersion)")
}
} }
func wg_log(_ type: OSLogType, staticMessage msg: StaticString) { func wg_log(_ type: OSLogType, staticMessage msg: StaticString) {
// Write to os log
os_log(msg, log: OSLog.default, type: type) os_log(msg, log: OSLog.default, type: type)
// Write to file log Logger.global?.log(message: "\(msg)")
let msgString: String = msg.withUTF8Buffer { (ptr: UnsafeBufferPointer<UInt8>) -> String in
return String(decoding: ptr, as: UTF8.self)
}
file_log(message: msgString)
} }
func wg_log(_ type: OSLogType, message msg: String) { func wg_log(_ type: OSLogType, message msg: String) {
// Write to os log
os_log("%{public}s", log: OSLog.default, type: type, msg) os_log("%{public}s", log: OSLog.default, type: type, msg)
// Write to file log Logger.global?.log(message: msg)
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)
}
}
} }

View File

@ -16,6 +16,23 @@
#include <sys/mman.h> #include <sys/mman.h>
#include "ringlogger.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) 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]; 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); strncpy(line->line, msg, MAX_LOG_LINE_LENGTH - 1);
line->line[MAX_LOG_LINE_LENGTH - 1] = '\0'; 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(&log->header, sizeof(log->header), MS_ASYNC);
msync(line, sizeof(*line), 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) if (line1->tv.tv_sec <= line2->tv.tv_sec)
return true; 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 line1->tv.tv_usec <= line2->tv.tv_usec;
return false; 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; uint32_t i1, i2, len1 = log1->header.len, len2 = log2->header.len;
char buf[MAX_LOG_LINE_LENGTH]; 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 *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 *line2 = &log2->lines[(log2->header.first + i2) % MAX_LINES];
const struct log_line *line; const struct log_line *line;
const char *tag;
if (i1 < len1 && (i2 >= len2 || first_before_second(line1, line2))) { if (i1 < len1 && (i2 >= len2 || first_before_second(line1, line2))) {
line = line1; line = line1;
tag = (const char *) tag1;
++i1; ++i1;
} else if (i2 < len2 && (i1 >= len1 || first_before_second(line2, line1))) { } else if (i2 < len2 && (i1 >= len1 || first_before_second(line2, line1))) {
line = line2; line = line2;
tag = (const char *) tag2;
++i2; ++i2;
} else { } else {
break; break;
} }
memcpy(buf, line->line, MAX_LOG_LINE_LENGTH); memcpy(buf, line->line, MAX_LOG_LINE_LENGTH);
buf[MAX_LOG_LINE_LENGTH - 1] = '\0'; 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; int ret = -errno;
fclose(file); fclose(file);
return ret; return ret;
@ -102,10 +109,10 @@ struct log *open_log(const char *file_name)
if (fd < 0) if (fd < 0)
return NULL; return NULL;
if (ftruncate(fd, sizeof(*log))) if (ftruncate(fd, sizeof(*log)))
return NULL; goto err;
log = mmap(NULL, sizeof(*log), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); log = mmap(NULL, sizeof(*log), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (log == MAP_FAILED) if (log == MAP_FAILED)
return NULL; goto err;
close(fd); close(fd);
if (log->magic != MAGIC) { if (log->magic != MAGIC) {
@ -115,4 +122,13 @@ struct log *open_log(const char *file_name)
} }
return log; return log;
err:
close(fd);
return NULL;
}
void close_log(struct log *log)
{
munmap(log, sizeof(*log));
} }

View File

@ -6,25 +6,10 @@
#ifndef RINGLOGGER_H #ifndef RINGLOGGER_H
#define RINGLOGGER_H #define RINGLOGGER_H
enum { struct log;
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); 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); struct log *open_log(const char *file_name);
void close_log(struct log *log);
#endif #endif

View File

@ -12,17 +12,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, func application(_ application: UIApplication,
willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Logger.configureGlobal(withFilePath: FileManager.appLogFileURL?.path, withTag: "APP")
if let appLogFilePath = FileManager.appLogFileURL?.path {
if !Logger.configure(withFilePath: appLogFilePath) {
os_log("Can't open log file for writing. Log is not saved to file.", log: OSLog.default, type: .error)
}
} else {
os_log("Can't obtain log file URL. Log is not saved to file.", log: OSLog.default, type: .error)
}
wg_log(.info, message: "Launching app")
wg_log_versions_to_file()
let window = UIWindow(frame: UIScreen.main.bounds) let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = UIColor.white window.backgroundColor = UIColor.white

View File

@ -115,13 +115,13 @@ class SettingsTableViewController: UITableViewController {
} }
guard let networkExtensionLogFilePath = FileManager.networkExtensionLogFileURL?.path else { guard let networkExtensionLogFilePath = FileManager.networkExtensionLogFileURL?.path else {
ErrorPresenter.showErrorAlert(title: "Log export failed", message: "Internal error obtaining extension log path", from: self) ErrorPresenter.showErrorAlert(title: "Log export failed", message: "Unable to determine extension log path", from: self)
return return
} }
let isWritten = Logger.writeLog(mergedWith: networkExtensionLogFilePath, tag: "APP", otherTag: "EXT", to: destinationURL.path) let isWritten = Logger.global?.writeLog(mergedWith: networkExtensionLogFilePath, to: destinationURL.path) ?? false
guard isWritten else { guard isWritten else {
ErrorPresenter.showErrorAlert(title: "Log export failed", message: "Internal error merging logs", from: self) ErrorPresenter.showErrorAlert(title: "Log export failed", message: "Unable to write logs to file", from: self)
return return
} }

View File

@ -13,8 +13,6 @@ enum PacketTunnelProviderError: Error {
case coultNotSetNetworkSettings case coultNotSetNetworkSettings
} }
private var logFileHandle: FileHandle?
/// A packet tunnel provider object. /// A packet tunnel provider object.
class PacketTunnelProvider: NEPacketTunnelProvider { class PacketTunnelProvider: NEPacketTunnelProvider {
@ -45,13 +43,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
func startTunnel(with tunnelConfiguration: TunnelConfiguration, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) { func startTunnel(with tunnelConfiguration: TunnelConfiguration, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
// Configure logging
configureLogger() configureLogger()
wg_log(.info, message: "Starting tunnel '\(tunnelConfiguration.interface.name)'") wg_log(.info, message: "Starting tunnel '\(tunnelConfiguration.interface.name)'")
wg_log_versions_to_file()
// Resolve endpoint domains
let endpoints = tunnelConfiguration.peers.map { $0.endpoint } let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
var resolvedEndpoints = [Endpoint?]() var resolvedEndpoints = [Endpoint?]()
@ -141,25 +135,13 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
if let handle = wgHandle { if let handle = wgHandle {
wgTurnOff(handle) wgTurnOff(handle)
} }
if let fileHandle = logFileHandle {
fileHandle.closeFile()
}
completionHandler() completionHandler()
} }
private func configureLogger() { private func configureLogger() {
Logger.configureGlobal(withFilePath: FileManager.networkExtensionLogFileURL?.path, withTag: "EXT")
// Setup writing the log to a file wgSetLogger { level, msgC in
if let networkExtensionLogFilePath = FileManager.networkExtensionLogFileURL?.path { guard let msgC = msgC else { return }
if !Logger.configure(withFilePath: networkExtensionLogFilePath) {
os_log("Can't open log file for writing. Log is not saved to file.", log: OSLog.default, type: .error)
}
} else {
os_log("Can't obtain log file URL. Log is not saved to file.", log: OSLog.default, type: .error)
}
// Setup WireGuard logger
wgSetLogger { level, msgCStr in
let logType: OSLogType let logType: OSLogType
switch level { switch level {
case 0: case 0:
@ -171,8 +153,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
default: default:
logType = .default logType = .default
} }
let msg = (msgCStr != nil) ? String(cString: msgCStr!) : "" wg_log(logType, message: String(cString: msgC))
wg_log(logType, message: msg)
} }
} }