Coverage Report

Created: 2025-08-24 06:54

/src/FreeRDP/winpr/include/winpr/wlog.h
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * WinPR Logger
4
 *
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 Bernhard Miklautz <bernhard.miklautz@thincast.com>
8
 *
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 *     http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22
23
#ifndef WINPR_LOG_H
24
#define WINPR_LOG_H
25
26
#ifdef __cplusplus
27
extern "C"
28
{
29
#endif
30
31
#include <stdarg.h>
32
33
#include <winpr/wtypes.h>
34
#include <winpr/winpr.h>
35
#include <winpr/synch.h>
36
#include <winpr/thread.h>
37
38
/**
39
 * Log Levels
40
 */
41
0
#define WLOG_TRACE 0
42
0
#define WLOG_DEBUG 1
43
1
#define WLOG_INFO 2
44
90
#define WLOG_WARN 3
45
915
#define WLOG_ERROR 4
46
0
#define WLOG_FATAL 5
47
2.01k
#define WLOG_OFF 6
48
1.00k
#define WLOG_LEVEL_INHERIT 0xFFFF
49
50
/** @defgroup LogMessageTypes Log Message
51
 *  @{
52
 */
53
2.01k
#define WLOG_MESSAGE_TEXT 0
54
0
#define WLOG_MESSAGE_DATA 1
55
0
#define WLOG_MESSAGE_IMAGE 2
56
0
#define WLOG_MESSAGE_PACKET 3
57
/**
58
 * @}
59
 */
60
61
/**
62
 * Log Appenders
63
 */
64
3
#define WLOG_APPENDER_CONSOLE 0
65
0
#define WLOG_APPENDER_FILE 1
66
0
#define WLOG_APPENDER_BINARY 2
67
0
#define WLOG_APPENDER_CALLBACK 3
68
0
#define WLOG_APPENDER_SYSLOG 4
69
#define WLOG_APPENDER_JOURNALD 5
70
0
#define WLOG_APPENDER_UDP 6
71
72
  typedef struct
73
  {
74
    DWORD Type;
75
76
    DWORD Level;
77
78
    LPSTR PrefixString;
79
80
    LPCSTR FormatString;
81
    LPCSTR TextString;
82
83
    size_t LineNumber;   /* __LINE__ */
84
    LPCSTR FileName;     /* __FILE__ */
85
    LPCSTR FunctionName; /* __func__ */
86
87
    /* Data Message */
88
89
    void* Data;
90
    size_t Length;
91
92
    /* Image Message */
93
94
    void* ImageData;
95
    size_t ImageWidth;
96
    size_t ImageHeight;
97
    size_t ImageBpp;
98
99
    /* Packet Message */
100
101
    void* PacketData;
102
    size_t PacketLength;
103
    DWORD PacketFlags;
104
  } wLogMessage;
105
  typedef struct s_wLogLayout wLogLayout;
106
  typedef struct s_wLogAppender wLogAppender;
107
  typedef struct s_wLog wLog;
108
109
#define WLOG_PACKET_INBOUND 1
110
0
#define WLOG_PACKET_OUTBOUND 2
111
112
  /** @brief specialized function to print text log messages.
113
   *  Same as @ref WLog_PrintMessage with \b type = WLOG_MESSAGE_TEXT but with compile time checks
114
   * for issues in format string.
115
   *
116
   *  @param log A pointer to the logger to use
117
   *  @param line the file line the log message originates from
118
   *  @param file the file name the log message originates from
119
   *  @param function the function name the log message originates from
120
   *  @param fmt the printf style format string
121
   *
122
   *  @return \b TRUE for success, \b FALSE otherwise.
123
   *  @since version 3.17.0
124
   */
125
  WINPR_ATTR_FORMAT_ARG(6, 7)
126
  WINPR_API BOOL WLog_PrintTextMessage(wLog* log, DWORD level, size_t line, const char* file,
127
                                       const char* function, WINPR_FORMAT_ARG const char* fmt,
128
                                       ...);
129
130
  /** @brief specialized function to print text log messages.
131
   *  Same as @ref WLog_PrintMessageVA with \b type = WLOG_MESSAGE_TEXT but with compile time
132
   * checks for issues in format string.
133
   *
134
   *  @param log A pointer to the logger to use
135
   *  @param line the file line the log message originates from
136
   *  @param file the file name the log message originates from
137
   *  @param function the function name the log message originates from
138
   *  @param fmt the printf style format string
139
   *
140
   *  @return \b TRUE for success, \b FALSE otherwise.
141
   *  @since version 3.17.0
142
   */
143
  WINPR_ATTR_FORMAT_ARG(6, 0)
144
  WINPR_API BOOL WLog_PrintTextMessageVA(wLog* log, DWORD level, size_t line, const char* file,
145
                                         const char* function, WINPR_FORMAT_ARG const char* fmt,
146
                                         va_list args);
147
148
  /** @brief log something of a specified type.
149
   *  @bug For /b WLOG_MESSAGE_TEXT the format string is not validated at compile time. Use \ref
150
   * WLog_PrintTextMessage instead.
151
   *
152
   *  @param log A pointer to the logger to use
153
   *  @param type The type of message to log, can be any of \ref LogMessageTypes
154
   *  @param line the file line the log message originates from
155
   *  @param file the file name the log message originates from
156
   *  @param function the function name the log message originates from
157
   *
158
   *  @return \b TRUE for success, \b FALSE otherwise.
159
   */
160
  WINPR_API BOOL WLog_PrintMessage(wLog* log, DWORD type, DWORD level, size_t line,
161
                                   const char* file, const char* function, ...);
162
163
  /** @brief log something of a specified type.
164
   *  @bug For /b WLOG_MESSAGE_TEXT the format string is not validated at compile time. Use \ref
165
   * WLog_PrintTextMessageVA instead.
166
   *
167
   *  @param log A pointer to the logger to use
168
   *  @param type The type of message to log, can be any of \ref LogMessageTypes
169
   *  @param line the file line the log message originates from
170
   *  @param file the file name the log message originates from
171
   *  @param function the function name the log message originates from
172
   *
173
   *  @return \b TRUE for success, \b FALSE otherwise.
174
   */
175
  WINPR_API BOOL WLog_PrintMessageVA(wLog* log, DWORD type, DWORD level, size_t line,
176
                                     const char* file, const char* function, va_list args);
177
178
  WINPR_API wLog* WLog_GetRoot(void);
179
  WINPR_API wLog* WLog_Get(LPCSTR name);
180
  WINPR_API DWORD WLog_GetLogLevel(wLog* log);
181
  WINPR_API BOOL WLog_IsLevelActive(wLog* _log, DWORD _log_level);
182
183
  /** @brief Set a custom context for a dynamic logger.
184
   *  This can be used to print a customized prefix, e.g. some session id for a specific context
185
   *
186
   *  @param log The logger to ste the context for. Must not be \b NULL
187
   *  @param fkt A function pointer that is called to get the custimized string.
188
   *  @param context A context \b fkt is called with. Caller must ensure it is still allocated
189
   * when \b log is used
190
   *
191
   *  @return \b TRUE for success, \b FALSE otherwise.
192
   */
193
  WINPR_API BOOL WLog_SetContext(wLog* log, const char* (*fkt)(void*), void* context);
194
195
#define WLog_Print_unchecked(_log, _log_level, ...)                                         \
196
0
  do                                                                                      \
197
0
  {                                                                                       \
198
0
    WLog_PrintTextMessage(_log, _log_level, __LINE__, __FILE__, __func__, __VA_ARGS__); \
199
0
  } while (0)
200
201
#define WLog_Print(_log, _log_level, ...)                        \
202
0
  do                                                           \
203
0
  {                                                            \
204
0
    if (WLog_IsLevelActive(_log, _log_level))                \
205
0
    {                                                        \
206
0
      WLog_Print_unchecked(_log, _log_level, __VA_ARGS__); \
207
0
    }                                                        \
208
0
  } while (0)
209
210
#define WLog_Print_tag(_tag, _log_level, ...)                 \
211
  do                                                        \
212
  {                                                         \
213
    static wLog* _log_cached_ptr = NULL;                  \
214
    if (!_log_cached_ptr)                                 \
215
      _log_cached_ptr = WLog_Get(_tag);                 \
216
    WLog_Print(_log_cached_ptr, _log_level, __VA_ARGS__); \
217
  } while (0)
218
219
#define WLog_PrintVA_unchecked(_log, _log_level, _args)                                 \
220
  do                                                                                  \
221
  {                                                                                   \
222
    WLog_PrintTextMessageVA(_log, _log_level, __LINE__, __FILE__, __func__, _args); \
223
  } while (0)
224
225
#define WLog_PrintVA(_log, _log_level, _args)                \
226
  do                                                       \
227
  {                                                        \
228
    if (WLog_IsLevelActive(_log, _log_level))            \
229
    {                                                    \
230
      WLog_PrintVA_unchecked(_log, _log_level, _args); \
231
    }                                                    \
232
  } while (0)
233
234
#define WLog_Data(_log, _log_level, ...)                                                         \
235
  do                                                                                           \
236
  {                                                                                            \
237
    if (WLog_IsLevelActive(_log, _log_level))                                                \
238
    {                                                                                        \
239
      WLog_PrintMessage(_log, WLOG_MESSAGE_DATA, _log_level, __LINE__, __FILE__, __func__, \
240
                        __VA_ARGS__);                                                      \
241
    }                                                                                        \
242
  } while (0)
243
244
#define WLog_Image(_log, _log_level, ...)                                                        \
245
  do                                                                                           \
246
  {                                                                                            \
247
    if (WLog_IsLevelActive(_log, _log_level))                                                \
248
    {                                                                                        \
249
      WLog_PrintMessage(_log, WLOG_MESSAGE_DATA, _log_level, __LINE__, __FILE__, __func__, \
250
                        __VA_ARGS__);                                                      \
251
    }                                                                                        \
252
  } while (0)
253
254
#define WLog_Packet(_log, _log_level, ...)                                                         \
255
  do                                                                                             \
256
  {                                                                                              \
257
    if (WLog_IsLevelActive(_log, _log_level))                                                  \
258
    {                                                                                          \
259
      WLog_PrintMessage(_log, WLOG_MESSAGE_PACKET, _log_level, __LINE__, __FILE__, __func__, \
260
                        __VA_ARGS__);                                                        \
261
    }                                                                                          \
262
  } while (0)
263
264
  static inline void WLog_Print_dbg_tag(const char* WINPR_RESTRICT tag, DWORD log_level,
265
                                        size_t line, const char* file, const char* fkt, ...)
266
1.00k
  {
267
1.00k
    static wLog* log_cached_ptr = NULL;
268
1.00k
    if (!log_cached_ptr)
269
2
      log_cached_ptr = WLog_Get(tag);
270
271
1.00k
    if (WLog_IsLevelActive(log_cached_ptr, log_level))
272
1.00k
    {
273
1.00k
      va_list ap;
274
1.00k
      va_start(ap, fkt);
275
1.00k
      WLog_PrintMessageVA(log_cached_ptr, WLOG_MESSAGE_TEXT, log_level, line, file, fkt, ap);
276
1.00k
      va_end(ap);
277
1.00k
    }
278
1.00k
  }
Unexecuted instantiation: TestFuzzCryptoCertificateDataSetPEM.c:WLog_Print_dbg_tag
Unexecuted instantiation: certificate_data.c:WLog_Print_dbg_tag
certificate.c:WLog_Print_dbg_tag
Line
Count
Source
266
6
  {
267
6
    static wLog* log_cached_ptr = NULL;
268
6
    if (!log_cached_ptr)
269
1
      log_cached_ptr = WLog_Get(tag);
270
271
6
    if (WLog_IsLevelActive(log_cached_ptr, log_level))
272
6
    {
273
6
      va_list ap;
274
6
      va_start(ap, fkt);
275
6
      WLog_PrintMessageVA(log_cached_ptr, WLOG_MESSAGE_TEXT, log_level, line, file, fkt, ap);
276
6
      va_end(ap);
277
6
    }
278
6
  }
Unexecuted instantiation: crypto.c:WLog_Print_dbg_tag
Unexecuted instantiation: ber.c:WLog_Print_dbg_tag
x509_utils.c:WLog_Print_dbg_tag
Line
Count
Source
266
999
  {
267
999
    static wLog* log_cached_ptr = NULL;
268
999
    if (!log_cached_ptr)
269
1
      log_cached_ptr = WLog_Get(tag);
270
271
999
    if (WLog_IsLevelActive(log_cached_ptr, log_level))
272
999
    {
273
999
      va_list ap;
274
999
      va_start(ap, fkt);
275
999
      WLog_PrintMessageVA(log_cached_ptr, WLOG_MESSAGE_TEXT, log_level, line, file, fkt, ap);
276
999
      va_end(ap);
277
999
    }
278
999
  }
Unexecuted instantiation: cert_common.c:WLog_Print_dbg_tag
Unexecuted instantiation: privatekey.c:WLog_Print_dbg_tag
Unexecuted instantiation: file.c:WLog_Print_dbg_tag
Unexecuted instantiation: hash.c:WLog_Print_dbg_tag
Unexecuted instantiation: stream.c:WLog_Print_dbg_tag
Unexecuted instantiation: debug.c:WLog_Print_dbg_tag
Unexecuted instantiation: wlog.c:WLog_Print_dbg_tag
Unexecuted instantiation: Appender.c:WLog_Print_dbg_tag
Unexecuted instantiation: FileAppender.c:WLog_Print_dbg_tag
Unexecuted instantiation: BinaryAppender.c:WLog_Print_dbg_tag
Unexecuted instantiation: CallbackAppender.c:WLog_Print_dbg_tag
Unexecuted instantiation: ConsoleAppender.c:WLog_Print_dbg_tag
Unexecuted instantiation: UdpAppender.c:WLog_Print_dbg_tag
Unexecuted instantiation: SyslogAppender.c:WLog_Print_dbg_tag
Unexecuted instantiation: error.c:WLog_Print_dbg_tag
Unexecuted instantiation: process.c:WLog_Print_dbg_tag
Unexecuted instantiation: winsock.c:WLog_Print_dbg_tag
Unexecuted instantiation: unicode.c:WLog_Print_dbg_tag
Unexecuted instantiation: string.c:WLog_Print_dbg_tag
Unexecuted instantiation: assert.c:WLog_Print_dbg_tag
Unexecuted instantiation: unicode_builtin.c:WLog_Print_dbg_tag
Unexecuted instantiation: nt.c:WLog_Print_dbg_tag
Unexecuted instantiation: critical.c:WLog_Print_dbg_tag
Unexecuted instantiation: event.c:WLog_Print_dbg_tag
Unexecuted instantiation: init.c:WLog_Print_dbg_tag
Unexecuted instantiation: sleep.c:WLog_Print_dbg_tag
Unexecuted instantiation: wait.c:WLog_Print_dbg_tag
Unexecuted instantiation: generic.c:WLog_Print_dbg_tag
Unexecuted instantiation: namedPipeClient.c:WLog_Print_dbg_tag
Unexecuted instantiation: pattern.c:WLog_Print_dbg_tag
Unexecuted instantiation: comm.c:WLog_Print_dbg_tag
Unexecuted instantiation: comm_ioctl.c:WLog_Print_dbg_tag
Unexecuted instantiation: comm_serial_sys.c:WLog_Print_dbg_tag
Unexecuted instantiation: comm_sercx_sys.c:WLog_Print_dbg_tag
Unexecuted instantiation: comm_sercx2_sys.c:WLog_Print_dbg_tag
Unexecuted instantiation: pipe.c:WLog_Print_dbg_tag
Unexecuted instantiation: interlocked.c:WLog_Print_dbg_tag
Unexecuted instantiation: environment.c:WLog_Print_dbg_tag
Unexecuted instantiation: path.c:WLog_Print_dbg_tag
Unexecuted instantiation: shell.c:WLog_Print_dbg_tag
Unexecuted instantiation: ArrayList.c:WLog_Print_dbg_tag
Unexecuted instantiation: Layout.c:WLog_Print_dbg_tag
Unexecuted instantiation: Message.c:WLog_Print_dbg_tag
Unexecuted instantiation: DataMessage.c:WLog_Print_dbg_tag
Unexecuted instantiation: ImageMessage.c:WLog_Print_dbg_tag
Unexecuted instantiation: PacketMessage.c:WLog_Print_dbg_tag
Unexecuted instantiation: sysinfo.c:WLog_Print_dbg_tag
Unexecuted instantiation: handle.c:WLog_Print_dbg_tag
Unexecuted instantiation: apc.c:WLog_Print_dbg_tag
Unexecuted instantiation: argv.c:WLog_Print_dbg_tag
Unexecuted instantiation: thread.c:WLog_Print_dbg_tag
Unexecuted instantiation: pollset.c:WLog_Print_dbg_tag
Unexecuted instantiation: comm_io.c:WLog_Print_dbg_tag
Unexecuted instantiation: image.c:WLog_Print_dbg_tag
279
280
#define WLog_LVL(tag, lvl, ...) \
281
  WLog_Print_dbg_tag(tag, lvl, __LINE__, __FILE__, __func__, __VA_ARGS__)
282
#define WLog_VRB(tag, ...) \
283
0
  WLog_Print_dbg_tag(tag, WLOG_TRACE, __LINE__, __FILE__, __func__, __VA_ARGS__)
284
#define WLog_DBG(tag, ...) \
285
0
  WLog_Print_dbg_tag(tag, WLOG_DEBUG, __LINE__, __FILE__, __func__, __VA_ARGS__)
286
#define WLog_INFO(tag, ...) \
287
0
  WLog_Print_dbg_tag(tag, WLOG_INFO, __LINE__, __FILE__, __func__, __VA_ARGS__)
288
#define WLog_WARN(tag, ...) \
289
90
  WLog_Print_dbg_tag(tag, WLOG_WARN, __LINE__, __FILE__, __func__, __VA_ARGS__)
290
#define WLog_ERR(tag, ...) \
291
915
  WLog_Print_dbg_tag(tag, WLOG_ERROR, __LINE__, __FILE__, __func__, __VA_ARGS__)
292
#define WLog_FATAL(tag, ...) \
293
0
  WLog_Print_dbg_tag(tag, WLOG_FATAL, __LINE__, __FILE__, __func__, __VA_ARGS__)
294
295
  WINPR_API BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel);
296
  WINPR_API BOOL WLog_SetStringLogLevel(wLog* log, LPCSTR level);
297
  WINPR_API BOOL WLog_AddStringLogFilters(LPCSTR filter);
298
299
  WINPR_API BOOL WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType);
300
  WINPR_API wLogAppender* WLog_GetLogAppender(wLog* log);
301
  WINPR_API BOOL WLog_OpenAppender(wLog* log);
302
  WINPR_API BOOL WLog_CloseAppender(wLog* log);
303
  WINPR_API BOOL WLog_ConfigureAppender(wLogAppender* appender, const char* setting, void* value);
304
305
  WINPR_API wLogLayout* WLog_GetLogLayout(wLog* log);
306
  WINPR_API BOOL WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format);
307
308
#if defined(WITH_WINPR_DEPRECATED)
309
  /** Deprecated */
310
  WINPR_DEPRECATED(WINPR_API BOOL WLog_Init(void));
311
  /** Deprecated */
312
  WINPR_DEPRECATED(WINPR_API BOOL WLog_Uninit(void));
313
#endif
314
315
  typedef BOOL (*wLogCallbackMessage_t)(const wLogMessage* msg);
316
  typedef BOOL (*wLogCallbackData_t)(const wLogMessage* msg);
317
  typedef BOOL (*wLogCallbackImage_t)(const wLogMessage* msg);
318
  typedef BOOL (*wLogCallbackPackage_t)(const wLogMessage* msg);
319
320
  typedef struct
321
  {
322
    wLogCallbackData_t data;
323
    wLogCallbackImage_t image;
324
    wLogCallbackMessage_t message;
325
    wLogCallbackPackage_t package;
326
  } wLogCallbacks;
327
328
#ifdef __cplusplus
329
}
330
#endif
331
332
#endif /* WINPR_WLOG_H */