api: concatenate function name at runtime

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-11-04 12:24:46 +01:00 committed by Simon Rozman
parent 0a51e26730
commit 9c349273f5
3 changed files with 29 additions and 18 deletions

View File

@ -25,7 +25,24 @@ WintunSetLogger(_In_ WINTUN_LOGGER_CALLBACK_FUNC NewLogger)
}
_Post_equals_last_error_ DWORD
LoggerError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error)
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)
return LastError;
Logger(Level, Combined);
}
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)
{
WCHAR *SystemMessage = NULL, *FormattedMessage = NULL;
FormatMessageW(
@ -39,12 +56,12 @@ LoggerError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error)
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!",
SystemMessage ? L"%4: %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 });
(va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage, (DWORD_PTR)Function });
if (FormattedMessage)
Logger(WINTUN_LOG_ERR, FormattedMessage);
LocalFree(FormattedMessage);

View File

@ -16,29 +16,23 @@ extern WINTUN_LOGGER_CALLBACK_FUNC Logger;
void WINAPI
WintunSetLogger(_In_ WINTUN_LOGGER_CALLBACK_FUNC NewLogger);
static inline _Post_equals_last_error_ DWORD
LoggerLog(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *LogLine)
{
DWORD LastError = GetLastError();
Logger(Level, LogLine);
SetLastError(LastError);
return LastError;
}
_Post_equals_last_error_ DWORD
LoggerLog(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Function, _In_z_ const WCHAR *LogLine);
_Post_equals_last_error_ DWORD
LoggerError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error);
LoggerError(_In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix, _In_ DWORD Error);
static inline _Post_equals_last_error_ DWORD
LoggerLastError(_In_z_ const WCHAR *Prefix)
LoggerLastError(_In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix)
{
DWORD LastError = GetLastError();
LoggerError(Prefix, LastError);
LoggerError(Prefix, Function, LastError);
SetLastError(LastError);
return LastError;
}
#define __L(x) L##x
#define _L(x) __L(x)
#define LOG(lvl, msg) (LoggerLog((lvl), _L(__FUNCTION__) L": " msg))
#define LOG_ERROR(msg, err) (LoggerError(_L(__FUNCTION__) L": " msg, (err)))
#define LOG_LAST_ERROR(msg) (LoggerLastError(_L(__FUNCTION__) L": " msg))
#define LOG(lvl, msg) (LoggerLog((lvl), _L(__FUNCTION__), msg))
#define LOG_ERROR(msg, err) (LoggerError(_L(__FUNCTION__), msg, (err)))
#define LOG_LAST_ERROR(msg) (LoggerLastError(_L(__FUNCTION__), msg))

View File

@ -72,7 +72,7 @@ ProcessStderr(_In_ HANDLE Stderr)
else if (State == OnMsg && c == L'\n')
{
Msg[Count] = 0;
LoggerLog(Level, Msg);
LoggerLog(Level, NULL, Msg);
State = OnNone;
Count = 0;
}