From b9fc0f666504d285f487abddbac1c4e4aa163e1e Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 4 Oct 2021 00:08:12 +0000 Subject: [PATCH] api: logger: remove function prefixes The strings are already unique, so prefixing function names adds nothing. Signed-off-by: Jason A. Donenfeld --- api/logger.c | 35 ++++++++++++----------------------- api/logger.h | 47 ++++++++++++++++++----------------------------- 2 files changed, 30 insertions(+), 52 deletions(-) diff --git a/api/logger.c b/api/logger.c index 639711a..7ad6c5c 100644 --- a/api/logger.c +++ b/api/logger.c @@ -46,41 +46,30 @@ StrTruncate(_Inout_count_(StrChars) LPWSTR Str, _In_ SIZE_T StrChars) _Use_decl_annotations_ DWORD -LoggerLog(WIREGUARD_LOGGER_LEVEL Level, LPCWSTR Function, LPCWSTR LogLine) +LoggerLog(WIREGUARD_LOGGER_LEVEL Level, LPCWSTR LogLine) { DWORD LastError = GetLastError(); - if (Function) - { - WCHAR Combined[0x400]; - if (_snwprintf_s(Combined, _countof(Combined), _TRUNCATE, L"%s: %s", Function, LogLine) == -1) - StrTruncate(Combined, _countof(Combined)); - Logger(Level, Now(), Combined); - } - else - Logger(Level, Now(), LogLine); + Logger(Level, Now(), LogLine); SetLastError(LastError); return LastError; } _Use_decl_annotations_ DWORD -LoggerLogV(WIREGUARD_LOGGER_LEVEL Level, LPCWSTR Function, LPCWSTR Format, va_list Args) +LoggerLogV(WIREGUARD_LOGGER_LEVEL Level, LPCWSTR Format, 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, Now(), LogLine); + Logger(Level, Now(), LogLine); SetLastError(LastError); return LastError; } _Use_decl_annotations_ DWORD -LoggerError(DWORD Error, LPCWSTR Function, LPCWSTR Prefix) +LoggerError(DWORD Error, LPCWSTR Prefix) { LPWSTR SystemMessage = NULL, FormattedMessage = NULL; FormatMessageW( @@ -94,12 +83,12 @@ LoggerError(DWORD Error, LPCWSTR Function, LPCWSTR Prefix) FormatMessageW( FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_MAX_WIDTH_MASK, - SystemMessage ? L"%4: %1: %3(Code 0x%2!08X!)" : L"%4: %1: Code 0x%2!08X!", + 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, (DWORD_PTR)Function }); + (va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage }); if (FormattedMessage) Logger(WIREGUARD_LOG_ERR, Now(), FormattedMessage); LocalFree(FormattedMessage); @@ -109,12 +98,12 @@ LoggerError(DWORD Error, LPCWSTR Function, LPCWSTR Prefix) _Use_decl_annotations_ DWORD -LoggerErrorV(DWORD Error, LPCWSTR Function, LPCWSTR Format, va_list Args) +LoggerErrorV(DWORD Error, LPCWSTR Format, 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); + WCHAR LogLine[0x400]; + if (_vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, Args) == -1) + StrTruncate(LogLine, _countof(LogLine)); + return LoggerError(Error, LogLine); } _Use_decl_annotations_ diff --git a/api/logger.h b/api/logger.h index a878b0f..b24a536 100644 --- a/api/logger.h +++ b/api/logger.h @@ -27,71 +27,59 @@ WIREGUARD_SET_ADAPTER_LOGGING_FUNC WireGuardSetAdapterLogging; _Post_equals_last_error_ DWORD -LoggerLog(_In_ WIREGUARD_LOGGER_LEVEL Level, _In_z_ LPCWSTR Function, _In_z_ LPCWSTR LogLine); +LoggerLog(_In_ WIREGUARD_LOGGER_LEVEL Level, _In_z_ LPCWSTR LogLine); _Post_equals_last_error_ DWORD -LoggerLogV( - _In_ WIREGUARD_LOGGER_LEVEL Level, - _In_z_ LPCWSTR Function, - _In_z_ _Printf_format_string_ LPCWSTR Format, - _In_ va_list Args); +LoggerLogV(_In_ WIREGUARD_LOGGER_LEVEL Level, _In_z_ _Printf_format_string_ LPCWSTR Format, _In_ va_list Args); _Post_equals_last_error_ static inline DWORD -LoggerLogFmt( - _In_ WIREGUARD_LOGGER_LEVEL Level, - _In_z_ LPCWSTR Function, - _In_z_ _Printf_format_string_ LPCWSTR Format, - ...) +LoggerLogFmt(_In_ WIREGUARD_LOGGER_LEVEL Level, _In_z_ _Printf_format_string_ LPCWSTR Format, ...) { va_list Args; va_start(Args, Format); - DWORD LastError = LoggerLogV(Level, Function, Format, Args); + DWORD LastError = LoggerLogV(Level, Format, Args); va_end(Args); return LastError; } _Post_equals_last_error_ DWORD -LoggerError(_In_ DWORD Error, _In_z_ LPCWSTR Function, _In_z_ LPCWSTR Prefix); +LoggerError(_In_ DWORD Error, _In_z_ LPCWSTR Prefix); _Post_equals_last_error_ DWORD -LoggerErrorV( - _In_ DWORD Error, - _In_z_ LPCWSTR Function, - _In_z_ _Printf_format_string_ LPCWSTR Format, - _In_ va_list Args); +LoggerErrorV(_In_ DWORD Error, _In_z_ _Printf_format_string_ LPCWSTR Format, _In_ va_list Args); _Post_equals_last_error_ static inline DWORD -LoggerErrorFmt(_In_ DWORD Error, _In_z_ LPCWSTR Function, _In_z_ _Printf_format_string_ LPCWSTR Format, ...) +LoggerErrorFmt(_In_ DWORD Error, _In_z_ _Printf_format_string_ LPCWSTR Format, ...) { va_list Args; va_start(Args, Format); - DWORD LastError = LoggerErrorV(Error, Function, Format, Args); + DWORD LastError = LoggerErrorV(Error, Format, Args); va_end(Args); return LastError; } _Post_equals_last_error_ static inline DWORD -LoggerLastErrorV(_In_z_ LPCWSTR Function, _In_z_ _Printf_format_string_ LPCWSTR Format, _In_ va_list Args) +LoggerLastErrorV(_In_z_ _Printf_format_string_ LPCWSTR Format, _In_ va_list Args) { DWORD LastError = GetLastError(); - LoggerErrorV(LastError, Function, Format, Args); + LoggerErrorV(LastError, Format, Args); SetLastError(LastError); return LastError; } _Post_equals_last_error_ static inline DWORD -LoggerLastErrorFmt(_In_z_ LPCWSTR Function, _In_z_ _Printf_format_string_ LPCWSTR Format, ...) +LoggerLastErrorFmt(_In_z_ _Printf_format_string_ LPCWSTR Format, ...) { va_list Args; va_start(Args, Format); - DWORD LastError = LoggerLastErrorV(Function, Format, Args); + DWORD LastError = LoggerLastErrorV(Format, Args); va_end(Args); return LastError; } @@ -99,11 +87,9 @@ LoggerLastErrorFmt(_In_z_ LPCWSTR Function, _In_z_ _Printf_format_string_ LPCWST VOID LoggerGetRegistryKeyPath(_In_ HKEY Key, _Out_writes_z_(MAX_REG_PATH) LPWSTR Path); -#define __L(x) L##x -#define _L(x) __L(x) -#define LOG(lvl, msg, ...) (LoggerLogFmt((lvl), _L(__FUNCTION__), msg, __VA_ARGS__)) -#define LOG_ERROR(err, msg, ...) (LoggerErrorFmt((err), _L(__FUNCTION__), msg, __VA_ARGS__)) -#define LOG_LAST_ERROR(msg, ...) (LoggerLastErrorFmt(_L(__FUNCTION__), msg, __VA_ARGS__)) +#define LOG(lvl, msg, ...) (LoggerLogFmt((lvl), msg, __VA_ARGS__)) +#define LOG_ERROR(err, msg, ...) (LoggerErrorFmt((err), msg, __VA_ARGS__)) +#define LOG_LAST_ERROR(msg, ...) (LoggerLastErrorFmt(msg, __VA_ARGS__)) #define RET_ERROR(Ret, Error) ((Error) == ERROR_SUCCESS ? (Ret) : (SetLastError(Error), 0)) @@ -139,6 +125,9 @@ LoggerReAlloc(_In_z_ LPCWSTR Function, _In_ DWORD Flags, _Frees_ptr_opt_ LPVOID } return Data; } + +#define __L(x) L##x +#define _L(x) __L(x) #define Alloc(Size) LoggerAlloc(_L(__FUNCTION__), 0, Size) #define ReAlloc(Mem, Size) LoggerReAlloc(_L(__FUNCTION__), 0, Mem, Size) #define Zalloc(Size) LoggerAlloc(_L(__FUNCTION__), HEAP_ZERO_MEMORY, Size)