aboutsummaryrefslogtreecommitdiffstats
path: root/api/logger.h
blob: 3ffda6e9ca94c90ad1879c595fbc107ce7482872 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
 */

#pragma once

#include "wintun.h"
#include "entry.h"
#include "registry.h"
#include <Windows.h>
#include <stdarg.h>
#include <wchar.h>

extern WINTUN_LOGGER_CALLBACK Logger;

/**
 * @copydoc WINTUN_SET_LOGGER_FUNC
 */
void WINAPI
WintunSetLogger(_In_ WINTUN_LOGGER_CALLBACK NewLogger);

_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
LoggerLogV(
    _In_ WINTUN_LOGGER_LEVEL Level,
    _In_z_ const WCHAR *Function,
    _In_z_ _Printf_format_string_ const WCHAR *Format,
    _In_ va_list Args);

static inline _Post_equals_last_error_ DWORD
LoggerLogFmt(
    _In_ WINTUN_LOGGER_LEVEL Level,
    _In_z_ const WCHAR *Function,
    _In_z_ _Printf_format_string_ const WCHAR *Format,
    ...)
{
    va_list Args;
    va_start(Args, Format);
    DWORD LastError = LoggerLogV(Level, Function, Format, Args);
    va_end(Args);
    return LastError;
}

_Post_equals_last_error_ DWORD
LoggerError(_In_ DWORD Error, _In_z_ const WCHAR *Function, _In_z_ const WCHAR *Prefix);

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

static inline _Post_equals_last_error_ DWORD
LoggerErrorFmt(_In_ DWORD Error, _In_z_ const WCHAR *Function, _In_z_ _Printf_format_string_ const WCHAR *Format, ...)
{
    va_list Args;
    va_start(Args, Format);
    DWORD LastError = LoggerErrorV(Error, Function, Format, Args);
    va_end(Args);
    return LastError;
}

static inline _Post_equals_last_error_ DWORD
LoggerLastErrorV(_In_z_ const WCHAR *Function, _In_z_ _Printf_format_string_ const WCHAR *Format, _In_ va_list Args)
{
    DWORD LastError = GetLastError();
    LoggerErrorV(LastError, Function, Format, Args);
    SetLastError(LastError);
    return LastError;
}

static inline _Post_equals_last_error_ DWORD
LoggerLastErrorFmt(_In_z_ const WCHAR *Function, _In_z_ _Printf_format_string_ const WCHAR *Format, ...)
{
    va_list Args;
    va_start(Args, Format);
    DWORD LastError = LoggerLastErrorV(Function, Format, Args);
    va_end(Args);
    return LastError;
}

VOID
LoggerGetRegistryKeyPath(_In_ HKEY Key, _Out_cap_c_(MAX_REG_PATH) WCHAR *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 RET_ERROR(Ret, Error) ((Error) == ERROR_SUCCESS ? (Ret) : (SetLastError(Error), 0))

static inline _Return_type_success_(return != NULL) _Ret_maybenull_
    _Post_writable_byte_size_(Size) void *LoggerAlloc(_In_z_ const WCHAR *Function, _In_ DWORD Flags, _In_ SIZE_T Size)
{
    void *Data = HeapAlloc(ModuleHeap, Flags, Size);
    if (!Data)
    {
        LoggerLogFmt(WINTUN_LOG_ERR, Function, L"Out of memory (flags: 0x%x, requested size: 0x%zx)", Flags, Size);
        SetLastError(ERROR_OUTOFMEMORY);
    }
    return Data;
}
#define Alloc(Size) LoggerAlloc(_L(__FUNCTION__), 0, Size)
#define Zalloc(Size) LoggerAlloc(_L(__FUNCTION__), HEAP_ZERO_MEMORY, Size)

static inline void
Free(void *Ptr)
{
    if (!Ptr)
        return;
    DWORD LastError = GetLastError();
    HeapFree(ModuleHeap, 0, Ptr);
    SetLastError(LastError);
}