aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Logging
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-12-13 15:38:10 +0530
committerRoopesh Chander <roop@roopc.net>2018-12-13 17:37:14 +0530
commitae7fb7323faf6321d07dd855ea4f7641d2424ec0 (patch)
treee06c8af688ca2de4c0464b7d206a140d9cf92f3d /WireGuard/Shared/Logging
parentAvoid using 'VPN' in code where possible (diff)
downloadwireguard-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/Logging')
-rw-r--r--WireGuard/Shared/Logging/Logger.swift54
-rw-r--r--WireGuard/Shared/Logging/ringlogger.c108
-rw-r--r--WireGuard/Shared/Logging/ringlogger.h30
3 files changed, 192 insertions, 0 deletions
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/Logging/ringlogger.c b/WireGuard/Shared/Logging/ringlogger.c
new file mode 100644
index 0000000..ea862de
--- /dev/null
+++ b/WireGuard/Shared/Logging/ringlogger.c
@@ -0,0 +1,108 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright © 2018 WireGuard LLC. All Rights Reserved.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+#include "ringlogger.h"
+
+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];
+
+ if (log->header.len == MAX_LINES)
+ log->header.first = (log->header.first + 1) % MAX_LINES;
+ else
+ ++log->header.len;
+
+ gettimeofday(&line->tv, NULL);
+ strncpy(line->line, msg, MAX_LOG_LINE_LENGTH - 1);
+ line->line[MAX_LOG_LINE_LENGTH - 1] = '\0';
+
+ msync(&log->header, sizeof(log->header), MS_ASYNC);
+ msync(line, sizeof(*line), MS_ASYNC);
+}
+
+static bool first_before_second(const struct log_line *line1, const struct log_line *line2)
+{
+ if (line1->tv.tv_sec <= line2->tv.tv_sec)
+ return true;
+ else 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 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];
+ FILE *file;
+
+ if (len1 > MAX_LINES)
+ len1 = MAX_LINES;
+ if (len2 > MAX_LINES)
+ len2 = MAX_LINES;
+
+ file = fopen(file_name, "w");
+ if (!file)
+ return -errno;
+
+ for (i1 = 0, i2 = 0;;) {
+ 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;
+
+ if (i1 < len1 && (i2 >= len2 || first_before_second(line1, line2))) {
+ line = line1;
+ ++i1;
+ } else if (i2 < len2 && (i1 >= len1 || first_before_second(line2, line1))) {
+ line = line2;
+ ++i2;
+ } else {
+ break;
+ }
+ memcpy(buf, line->line, MAX_LOG_LINE_LENGTH);
+ buf[MAX_LOG_LINE_LENGTH - 1] = '\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;
+ }
+ }
+ fclose(file);
+ return 0;
+}
+
+struct log *open_log(const char *file_name)
+{
+ int fd;
+ struct log *log;
+
+ fd = open(file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
+ if (fd < 0)
+ return NULL;
+ if (ftruncate(fd, sizeof(*log)))
+ return NULL;
+ log = mmap(NULL, sizeof(*log), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (log == MAP_FAILED)
+ return NULL;
+ close(fd);
+
+ if (log->magic != MAGIC) {
+ memset(log, 0, sizeof(*log));
+ log->magic = MAGIC;
+ msync(log, sizeof(*log), MS_ASYNC);
+ }
+
+ return log;
+}
diff --git a/WireGuard/Shared/Logging/ringlogger.h b/WireGuard/Shared/Logging/ringlogger.h
new file mode 100644
index 0000000..a8d07c0
--- /dev/null
+++ b/WireGuard/Shared/Logging/ringlogger.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright © 2018 WireGuard LLC. All Rights Reserved.
+ */
+
+#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;
+};
+
+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);
+
+#endif