aboutsummaryrefslogtreecommitdiffstats
path: root/api/logger.c
diff options
context:
space:
mode:
authorSimon Rozman <simon@rozman.si>2021-02-02 13:12:45 +0100
committerSimon Rozman <simon@rozman.si>2021-02-16 04:19:21 +0100
commitbc5cde89160e706789ec9cdadd06be8127d086f9 (patch)
tree0d19fb46411441437ebd9fdab35489b6088dc368 /api/logger.c
parentapi: tighten function parameter code analysis annotations (diff)
downloadwintun-bc5cde89160e706789ec9cdadd06be8127d086f9.tar.xz
wintun-bc5cde89160e706789ec9cdadd06be8127d086f9.zip
api: upgrade logging0.10.2
Log runtime information to quickly check whether the values are sane when analyzing error logs sent in by users. Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'api/logger.c')
-rw-r--r--api/logger.c75
1 files changed, 70 insertions, 5 deletions
diff --git a/api/logger.c b/api/logger.c
index 0ebdd6e..7907437 100644
--- a/api/logger.c
+++ b/api/logger.c
@@ -4,7 +4,9 @@
*/
#include "logger.h"
+#include "ntdll.h"
#include <Windows.h>
+#include <winternl.h>
#include <wchar.h>
static BOOL CALLBACK
@@ -25,6 +27,13 @@ WintunSetLogger(_In_ WINTUN_LOGGER_CALLBACK NewLogger)
Logger = NewLogger;
}
+static VOID
+StrTruncate(_Inout_count_(StrChars) WCHAR *Str, _In_ SIZE_T StrChars)
+{
+ Str[StrChars - 2] = L'\u2026'; /* Horizontal Ellipsis */
+ Str[StrChars - 1] = 0;
+}
+
_Post_equals_last_error_ DWORD
LoggerLog(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Function, _In_z_ const WCHAR *LogLine)
{
@@ -33,10 +42,7 @@ LoggerLog(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Function, _In_z_ c
{
WCHAR Combined[0x400];
if (_snwprintf_s(Combined, _countof(Combined), _TRUNCATE, L"%s: %s", Function, LogLine) == -1)
- {
- Combined[_countof(Combined) - 2] = L'\u2026'; /* Horizontal Ellipsis */
- Combined[_countof(Combined) - 1] = 0;
- }
+ StrTruncate(Combined, _countof(Combined));
Logger(Level, Combined);
}
else
@@ -46,7 +52,26 @@ LoggerLog(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Function, _In_z_ c
}
_Post_equals_last_error_ DWORD
-LoggerError(_In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix, _In_ DWORD Error)
+LoggerLogV(
+ _In_ WINTUN_LOGGER_LEVEL Level,
+ _In_z_ const WCHAR *Function,
+ _In_z_ _Printf_format_string_ const WCHAR *Format,
+ _In_ va_list Args)
+{
+ DWORD LastError = GetLastError();
+ WCHAR LogLine[0x400];
+ if (_vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, Args) == -1)
+ StrTruncate(LogLine, _countof(LogLine));
+ if (Function)
+ LoggerLog(Level, Function, LogLine);
+ else
+ Logger(Level, LogLine);
+ SetLastError(LastError);
+ return LastError;
+}
+
+_Post_equals_last_error_ DWORD
+LoggerError(_In_ DWORD Error, _In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix)
{
WCHAR *SystemMessage = NULL, *FormattedMessage = NULL;
FormatMessageW(
@@ -72,3 +97,43 @@ LoggerError(_In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix, _In_ DWORD
LocalFree(SystemMessage);
return Error;
}
+
+_Post_equals_last_error_ DWORD
+LoggerErrorV(
+ _In_ DWORD Error,
+ _In_z_ const WCHAR *Function,
+ _In_z_ _Printf_format_string_ const WCHAR *Format,
+ _In_ va_list Args)
+{
+ WCHAR Prefix[0x400];
+ if (_vsnwprintf_s(Prefix, _countof(Prefix), _TRUNCATE, Format, Args) == -1)
+ StrTruncate(Prefix, _countof(Prefix));
+ return LoggerError(Error, Function, Prefix);
+}
+
+VOID
+LoggerGetRegistryKeyPath(_In_ HKEY Key, _Out_cap_c_(MAX_REG_PATH) WCHAR *Path)
+{
+ DWORD LastError = GetLastError();
+ if (Key == NULL)
+ {
+ wcscpy_s(Path, MAX_REG_PATH, L"<null>");
+ goto out;
+ }
+ if (_snwprintf_s(Path, MAX_REG_PATH, _TRUNCATE, L"0x%p", Key) == -1)
+ StrTruncate(Path, MAX_REG_PATH);
+ union
+ {
+ KEY_NAME_INFORMATION KeyNameInfo;
+ WCHAR Data[offsetof(KEY_NAME_INFORMATION, Name) + MAX_REG_PATH];
+ } Buffer;
+ DWORD Size;
+ if (!NT_SUCCESS(NtQueryKey(Key, 3, &Buffer, sizeof(Buffer), &Size)) ||
+ Size < offsetof(KEY_NAME_INFORMATION, Name) || Buffer.KeyNameInfo.NameLength >= MAX_REG_PATH * sizeof(WCHAR))
+ goto out;
+ Buffer.KeyNameInfo.NameLength /= sizeof(WCHAR);
+ wmemcpy_s(Path, MAX_REG_PATH, Buffer.KeyNameInfo.Name, Buffer.KeyNameInfo.NameLength);
+ Path[Buffer.KeyNameInfo.NameLength] = L'\0';
+out:
+ SetLastError(LastError);
+}