Skip to content

Commit

Permalink
api: logger: remove function prefixes
Browse files Browse the repository at this point in the history
The strings are already unique, so prefixing function names adds
nothing.

Signed-off-by: Jason A. Donenfeld <[email protected]>
  • Loading branch information
zx2c4 committed Oct 6, 2021
1 parent 12993b1 commit b9fc0f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 52 deletions.
35 changes: 12 additions & 23 deletions api/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -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_
Expand Down
47 changes: 18 additions & 29 deletions api/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,83 +27,69 @@ 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;
}

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))

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b9fc0f6

Please sign in to comment.