2020-10-13 19:40:52 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "logger.h"
|
|
|
|
#include <Windows.h>
|
2020-11-04 12:55:25 +01:00
|
|
|
#include <wchar.h>
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2020-10-16 14:17:22 +02:00
|
|
|
static BOOL CALLBACK
|
2020-10-13 19:40:52 +02:00
|
|
|
NopLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *LogLine)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(Level);
|
|
|
|
UNREFERENCED_PARAMETER(LogLine);
|
2020-10-16 14:17:22 +02:00
|
|
|
return TRUE;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 23:08:00 +01:00
|
|
|
WINTUN_LOGGER_CALLBACK_FUNC Logger = NopLogger;
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2020-10-17 15:12:20 +02:00
|
|
|
void CALLBACK
|
2020-10-31 23:08:00 +01:00
|
|
|
WintunSetLogger(_In_ WINTUN_LOGGER_CALLBACK_FUNC NewLogger)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-11-02 17:42:52 +01:00
|
|
|
if (!NewLogger)
|
|
|
|
NewLogger = NopLogger;
|
2020-10-13 19:40:52 +02:00
|
|
|
Logger = NewLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Post_equals_last_error_ DWORD
|
2020-11-04 12:24:46 +01:00
|
|
|
LoggerLog(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Function, _In_z_ const WCHAR *LogLine)
|
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
if (Function)
|
|
|
|
{
|
|
|
|
WCHAR Combined[0x400];
|
|
|
|
if (_snwprintf_s(Combined, _countof(Combined), _TRUNCATE, L"%s: %s", Function, LogLine) == -1)
|
2020-11-04 12:55:25 +01:00
|
|
|
Logger(Level, LogLine);
|
|
|
|
else
|
|
|
|
Logger(Level, Combined);
|
2020-11-04 12:24:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Logger(Level, LogLine);
|
|
|
|
SetLastError(LastError);
|
|
|
|
return LastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Post_equals_last_error_ DWORD
|
|
|
|
LoggerError(_In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix, _In_ DWORD Error)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
|
|
|
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),
|
2020-10-17 15:12:20 +02:00
|
|
|
(void *)&SystemMessage,
|
2020-10-13 19:40:52 +02:00
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
FormatMessageW(
|
|
|
|
FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY |
|
|
|
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
2020-11-04 12:24:46 +01:00
|
|
|
SystemMessage ? L"%4: %1: %3(Code 0x%2!08X!)" : L"%1: Code 0x%2!08X!",
|
2020-10-13 19:40:52 +02:00
|
|
|
0,
|
|
|
|
0,
|
2020-10-17 15:12:20 +02:00
|
|
|
(void *)&FormattedMessage,
|
2020-10-13 19:40:52 +02:00
|
|
|
0,
|
2020-11-04 12:24:46 +01:00
|
|
|
(va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage, (DWORD_PTR)Function });
|
2020-10-13 19:40:52 +02:00
|
|
|
if (FormattedMessage)
|
|
|
|
Logger(WINTUN_LOG_ERR, FormattedMessage);
|
|
|
|
LocalFree(FormattedMessage);
|
|
|
|
LocalFree(SystemMessage);
|
|
|
|
return Error;
|
|
|
|
}
|