aboutsummaryrefslogtreecommitdiffstats
path: root/api/logger.c
diff options
context:
space:
mode:
authorSimon Rozman <simon@rozman.si>2020-10-13 19:40:52 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2020-10-30 16:50:59 +0100
commitf316c13b3e9548d8694bad9a1d634c15f31fa52d (patch)
tree1e9967655a2094425ce3f4f2d078025dfdf0c736 /api/logger.c
parentapi: split api.h (diff)
downloadwintun-f316c13b3e9548d8694bad9a1d634c15f31fa52d.tar.xz
wintun-f316c13b3e9548d8694bad9a1d634c15f31fa52d.zip
api: introduce logging
And other unifications with installer before merging. Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'api/logger.c')
-rw-r--r--api/logger.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/api/logger.c b/api/logger.c
new file mode 100644
index 0000000..a4400b9
--- /dev/null
+++ b/api/logger.c
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
+ */
+
+#include "pch.h"
+
+static VOID CALLBACK
+NopLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *LogLine)
+{
+ UNREFERENCED_PARAMETER(Level);
+ UNREFERENCED_PARAMETER(LogLine);
+}
+
+WINTUN_LOGGER_FUNC Logger = NopLogger;
+
+VOID CALLBACK
+WintunSetLogger(_In_ WINTUN_LOGGER_FUNC NewLogger)
+{
+ Logger = NewLogger;
+}
+
+_Post_equals_last_error_ DWORD
+LoggerError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error)
+{
+ WCHAR *SystemMessage = NULL, *FormattedMessage = NULL;
+ FormatMessageW(
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK,
+ NULL,
+ HRESULT_FROM_SETUPAPI(Error),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (VOID *)&SystemMessage,
+ 0,
+ NULL);
+ FormatMessageW(
+ FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY |
+ FORMAT_MESSAGE_MAX_WIDTH_MASK,
+ SystemMessage ? L"%1: %3(Code 0x%2!08X!)" : L"%1: Code 0x%2!08X!",
+ 0,
+ 0,
+ (VOID *)&FormattedMessage,
+ 0,
+ (va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage });
+ if (FormattedMessage)
+ Logger(WINTUN_LOG_ERR, FormattedMessage);
+ LocalFree(FormattedMessage);
+ LocalFree(SystemMessage);
+ return Error;
+}