/src/FreeRDP/winpr/include/winpr/wlog.h
Line | Count | Source |
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/platform.h> |
34 | | #include <winpr/wtypes.h> |
35 | | #include <winpr/winpr.h> |
36 | | #include <winpr/synch.h> |
37 | | #include <winpr/thread.h> |
38 | | |
39 | | /** |
40 | | * Log Levels |
41 | | */ |
42 | 0 | #define WLOG_TRACE 0 |
43 | 0 | #define WLOG_DEBUG 1 |
44 | 0 | #define WLOG_INFO 2 |
45 | 0 | #define WLOG_WARN 3 |
46 | 0 | #define WLOG_ERROR 4 |
47 | 0 | #define WLOG_FATAL 5 |
48 | 0 | #define WLOG_OFF 6 |
49 | 0 | #define WLOG_LEVEL_INHERIT 0xFFFF |
50 | | |
51 | | /** @defgroup LogMessageTypes Log Message |
52 | | * @{ |
53 | | */ |
54 | 0 | #define WLOG_MESSAGE_TEXT 0 |
55 | 0 | #define WLOG_MESSAGE_DATA 1 |
56 | 0 | #define WLOG_MESSAGE_IMAGE 2 |
57 | 0 | #define WLOG_MESSAGE_PACKET 3 |
58 | | /** |
59 | | * @} |
60 | | */ |
61 | | |
62 | | /** |
63 | | * Log Appenders |
64 | | */ |
65 | 0 | #define WLOG_APPENDER_CONSOLE 0 |
66 | 0 | #define WLOG_APPENDER_FILE 1 |
67 | 0 | #define WLOG_APPENDER_BINARY 2 |
68 | 0 | #define WLOG_APPENDER_CALLBACK 3 |
69 | 0 | #define WLOG_APPENDER_SYSLOG 4 |
70 | | #define WLOG_APPENDER_JOURNALD 5 |
71 | 0 | #define WLOG_APPENDER_UDP 6 |
72 | | |
73 | | typedef struct |
74 | | { |
75 | | DWORD Type; |
76 | | |
77 | | DWORD Level; |
78 | | |
79 | | LPSTR PrefixString; |
80 | | |
81 | | LPCSTR FormatString; |
82 | | LPCSTR TextString; |
83 | | |
84 | | size_t LineNumber; /* __LINE__ */ |
85 | | LPCSTR FileName; /* __FILE__ */ |
86 | | LPCSTR FunctionName; /* __func__ */ |
87 | | |
88 | | /* Data Message */ |
89 | | |
90 | | void* Data; |
91 | | size_t Length; |
92 | | |
93 | | /* Image Message */ |
94 | | |
95 | | void* ImageData; |
96 | | size_t ImageWidth; |
97 | | size_t ImageHeight; |
98 | | size_t ImageBpp; |
99 | | |
100 | | /* Packet Message */ |
101 | | |
102 | | void* PacketData; |
103 | | size_t PacketLength; |
104 | | DWORD PacketFlags; |
105 | | } wLogMessage; |
106 | | typedef struct s_wLogLayout wLogLayout; |
107 | | typedef struct s_wLogAppender wLogAppender; |
108 | | typedef struct s_wLog wLog; |
109 | | |
110 | | #define WLOG_PACKET_INBOUND 1 |
111 | 0 | #define WLOG_PACKET_OUTBOUND 2 |
112 | | |
113 | | /** @brief specialized function to print text log messages. |
114 | | * Same as @ref WLog_PrintMessage with \b type = WLOG_MESSAGE_TEXT but with compile time checks |
115 | | * for issues in format string. |
116 | | * |
117 | | * @param log A pointer to the logger to use |
118 | | * @param line the file line the log message originates from |
119 | | * @param file the file name the log message originates from |
120 | | * @param function the function name the log message originates from |
121 | | * @param fmt the printf style format string |
122 | | * |
123 | | * @return \b TRUE for success, \b FALSE otherwise. |
124 | | * @since version 3.17.0 |
125 | | */ |
126 | | WINPR_ATTR_FORMAT_ARG(6, 7) |
127 | | WINPR_API BOOL WLog_PrintTextMessage(wLog* log, DWORD level, size_t line, const char* file, |
128 | | const char* function, WINPR_FORMAT_ARG const char* fmt, |
129 | | ...); |
130 | | |
131 | | /** @brief specialized function to print text log messages. |
132 | | * Same as @ref WLog_PrintMessageVA with \b type = WLOG_MESSAGE_TEXT but with compile time |
133 | | * checks for issues in format string. |
134 | | * |
135 | | * @param log A pointer to the logger to use |
136 | | * @param line the file line the log message originates from |
137 | | * @param file the file name the log message originates from |
138 | | * @param function the function name the log message originates from |
139 | | * @param fmt the printf style format string |
140 | | * |
141 | | * @return \b TRUE for success, \b FALSE otherwise. |
142 | | * @since version 3.17.0 |
143 | | */ |
144 | | WINPR_ATTR_FORMAT_ARG(6, 0) |
145 | | WINPR_API BOOL WLog_PrintTextMessageVA(wLog* log, DWORD level, size_t line, const char* file, |
146 | | const char* function, WINPR_FORMAT_ARG const char* fmt, |
147 | | va_list args); |
148 | | |
149 | | /** @brief log something of a specified type. |
150 | | * @bug For /b WLOG_MESSAGE_TEXT the format string is not validated at compile time. Use \ref |
151 | | * WLog_PrintTextMessage instead. |
152 | | * |
153 | | * @param log A pointer to the logger to use |
154 | | * @param type The type of message to log, can be any of \ref LogMessageTypes |
155 | | * @param line the file line the log message originates from |
156 | | * @param file the file name the log message originates from |
157 | | * @param function the function name the log message originates from |
158 | | * |
159 | | * @return \b TRUE for success, \b FALSE otherwise. |
160 | | */ |
161 | | WINPR_API BOOL WLog_PrintMessage(wLog* log, DWORD type, DWORD level, size_t line, |
162 | | const char* file, const char* function, ...); |
163 | | |
164 | | /** @brief log something of a specified type. |
165 | | * @bug For /b WLOG_MESSAGE_TEXT the format string is not validated at compile time. Use \ref |
166 | | * WLog_PrintTextMessageVA instead. |
167 | | * |
168 | | * @param log A pointer to the logger to use |
169 | | * @param type The type of message to log, can be any of \ref LogMessageTypes |
170 | | * @param line the file line the log message originates from |
171 | | * @param file the file name the log message originates from |
172 | | * @param function the function name the log message originates from |
173 | | * |
174 | | * @return \b TRUE for success, \b FALSE otherwise. |
175 | | */ |
176 | | WINPR_API BOOL WLog_PrintMessageVA(wLog* log, DWORD type, DWORD level, size_t line, |
177 | | const char* file, const char* function, va_list args); |
178 | | |
179 | | WINPR_ATTR_NODISCARD |
180 | | WINPR_API wLog* WLog_GetRoot(void); |
181 | | |
182 | | WINPR_ATTR_NODISCARD |
183 | | WINPR_API wLog* WLog_Get(LPCSTR name); |
184 | | |
185 | | /** @brief discard a WLog instance created by \b WLog_Discard and not managed internally. |
186 | | * |
187 | | * @param log The logger instance to discard. |
188 | | * @since version 3.25.0 |
189 | | */ |
190 | | WINPR_API void WLog_Discard(wLog* log); |
191 | | |
192 | | /** @brief Create an independent logger instance. Management of this logger |
193 | | * is up to the caller. |
194 | | * |
195 | | * @param name The name of the logger, must not be \b nullptr |
196 | | * @param root The parent logger this instance should copy defaults from. |
197 | | * @return A new logger instance or \b nullptr in case of failures. |
198 | | * @since version 3.25.0 |
199 | | */ |
200 | | WINPR_ATTR_MALLOC(WLog_Discard, 1) |
201 | | WINPR_API wLog* WLog_Create(LPCSTR name, wLog* root); |
202 | | |
203 | | WINPR_ATTR_NODISCARD |
204 | | WINPR_API DWORD WLog_GetLogLevel(wLog* log); |
205 | | |
206 | | WINPR_ATTR_NODISCARD |
207 | | WINPR_API BOOL WLog_IsLevelActive(wLog* _log, DWORD _log_level); |
208 | | |
209 | | /** @brief Set a custom context for a dynamic logger. |
210 | | * This can be used to print a customized prefix, e.g. some session id for a specific context |
211 | | * |
212 | | * @warning In multi instance applications only use this with loggers created with \b |
213 | | * WLog_Create otherwise there may be out of bound reads as the internally managed loggers only |
214 | | * support a single context |
215 | | * |
216 | | * @param log The logger to ste the context for. Must not be \b nullptr |
217 | | * @param fkt A function pointer that is called to get the custimized string. |
218 | | * @param context A context \b fkt is called with. Caller must ensure it is still allocated |
219 | | * when \b log is used |
220 | | * |
221 | | * @return \b TRUE for success, \b FALSE otherwise. |
222 | | */ |
223 | | WINPR_API BOOL WLog_SetContext(wLog* log, const char* (*fkt)(void*), void* context); |
224 | | |
225 | | /** @brief Set a application wide global logger prefix. |
226 | | * This can be used to distinguish WLog entries from different applicatiions like |
227 | | * freerpd-shadow-cli and xfreerdp. It also allows setting a dynamic prefix depending on command |
228 | | * line arguments to separate different xfreerdp instances. |
229 | | * |
230 | | * @warning This function should only be called directly after \b main before any threads start |
231 | | * up. Thread safety is undefined! |
232 | | * |
233 | | * @param globalprefix Set a global prefix string prepended to all logger entries. Use \b |
234 | | * nullptr to disable prefix. |
235 | | * @return \b TRUE for success, \b FALSE otherwise. |
236 | | * @version since 3.25.0 |
237 | | */ |
238 | | WINPR_API BOOL WLog_SetGlobalContext(const char* globalprefix); |
239 | | |
240 | | #define WLog_Print_unchecked(_log, _log_level, ...) \ |
241 | 0 | do \ |
242 | 0 | { \ |
243 | 0 | WLog_PrintTextMessage(_log, _log_level, __LINE__, __FILE__, __func__, __VA_ARGS__); \ |
244 | 0 | } while (0) |
245 | | |
246 | | #define WLog_Print(_log, _log_level, ...) \ |
247 | 0 | do \ |
248 | 0 | { \ |
249 | 0 | if (WLog_IsLevelActive(_log, _log_level)) \ |
250 | 0 | { \ |
251 | 0 | WLog_Print_unchecked(_log, _log_level, __VA_ARGS__); \ |
252 | 0 | } \ |
253 | 0 | } while (0) |
254 | | |
255 | | #define WLog_Print_tag(_tag, _log_level, ...) \ |
256 | | do \ |
257 | | { \ |
258 | | static wLog* _log_cached_ptr = nullptr; \ |
259 | | if (!_log_cached_ptr) \ |
260 | | _log_cached_ptr = WLog_Get(_tag); \ |
261 | | WLog_Print(_log_cached_ptr, _log_level, __VA_ARGS__); \ |
262 | | } while (0) |
263 | | |
264 | | #define WLog_PrintVA_unchecked(_log, _log_level, _fmt, _args) \ |
265 | | do \ |
266 | | { \ |
267 | | WLog_PrintTextMessageVA(_log, _log_level, __LINE__, __FILE__, __func__, _fmt, _args); \ |
268 | | } while (0) |
269 | | |
270 | | #define WLog_PrintVA(_log, _log_level, _fmt, _args) \ |
271 | | do \ |
272 | | { \ |
273 | | if (WLog_IsLevelActive(_log, _log_level)) \ |
274 | | { \ |
275 | | WLog_PrintVA_unchecked(_log, _log_level, _fmt, _args); \ |
276 | | } \ |
277 | | } while (0) |
278 | | |
279 | | #define WLog_Data(_log, _log_level, ...) \ |
280 | | do \ |
281 | | { \ |
282 | | if (WLog_IsLevelActive(_log, _log_level)) \ |
283 | | { \ |
284 | | WLog_PrintMessage(_log, WLOG_MESSAGE_DATA, _log_level, __LINE__, __FILE__, __func__, \ |
285 | | __VA_ARGS__); \ |
286 | | } \ |
287 | | } while (0) |
288 | | |
289 | | #define WLog_Image(_log, _log_level, ...) \ |
290 | | do \ |
291 | | { \ |
292 | | if (WLog_IsLevelActive(_log, _log_level)) \ |
293 | | { \ |
294 | | WLog_PrintMessage(_log, WLOG_MESSAGE_DATA, _log_level, __LINE__, __FILE__, __func__, \ |
295 | | __VA_ARGS__); \ |
296 | | } \ |
297 | | } while (0) |
298 | | |
299 | | #define WLog_Packet(_log, _log_level, ...) \ |
300 | | do \ |
301 | | { \ |
302 | | if (WLog_IsLevelActive(_log, _log_level)) \ |
303 | | { \ |
304 | | WLog_PrintMessage(_log, WLOG_MESSAGE_PACKET, _log_level, __LINE__, __FILE__, __func__, \ |
305 | | __VA_ARGS__); \ |
306 | | } \ |
307 | | } while (0) |
308 | | |
309 | | WINPR_ATTR_FORMAT_ARG(6, 7) |
310 | | static inline void WLog_Print_dbg_tag(const char* WINPR_RESTRICT tag, DWORD log_level, |
311 | | size_t line, const char* file, const char* fkt, |
312 | | WINPR_FORMAT_ARG const char* fmt, ...) |
313 | 0 | { |
314 | 0 | static wLog* log_cached_ptr = nullptr; |
315 | 0 | if (!log_cached_ptr) |
316 | 0 | log_cached_ptr = WLog_Get(tag); |
317 | |
|
318 | 0 | if (WLog_IsLevelActive(log_cached_ptr, log_level)) |
319 | 0 | { |
320 | 0 | va_list ap = WINPR_C_ARRAY_INIT; |
321 | 0 | va_start(ap, fmt); |
322 | 0 | WLog_PrintTextMessageVA(log_cached_ptr, log_level, line, file, fkt, fmt, ap); |
323 | 0 | va_end(ap); |
324 | 0 | } |
325 | 0 | } Unexecuted instantiation: TestFuzzChannelCliprdr.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: 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: 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: file.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: atexit.c:WLog_Print_dbg_tag Unexecuted instantiation: winpr.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: error.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: nt.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 Unexecuted instantiation: cliprdr_common.c:WLog_Print_dbg_tag |
326 | | |
327 | | #define WLog_LVL(tag, lvl, ...) \ |
328 | | WLog_Print_dbg_tag(tag, lvl, __LINE__, __FILE__, __func__, __VA_ARGS__) |
329 | | #define WLog_VRB(tag, ...) \ |
330 | | WLog_Print_dbg_tag(tag, WLOG_TRACE, __LINE__, __FILE__, __func__, __VA_ARGS__) |
331 | | #define WLog_DBG(tag, ...) \ |
332 | 0 | WLog_Print_dbg_tag(tag, WLOG_DEBUG, __LINE__, __FILE__, __func__, __VA_ARGS__) |
333 | | #define WLog_INFO(tag, ...) \ |
334 | | WLog_Print_dbg_tag(tag, WLOG_INFO, __LINE__, __FILE__, __func__, __VA_ARGS__) |
335 | | #define WLog_WARN(tag, ...) \ |
336 | 0 | WLog_Print_dbg_tag(tag, WLOG_WARN, __LINE__, __FILE__, __func__, __VA_ARGS__) |
337 | | #define WLog_ERR(tag, ...) \ |
338 | 0 | WLog_Print_dbg_tag(tag, WLOG_ERROR, __LINE__, __FILE__, __func__, __VA_ARGS__) |
339 | | #define WLog_FATAL(tag, ...) \ |
340 | 0 | WLog_Print_dbg_tag(tag, WLOG_FATAL, __LINE__, __FILE__, __func__, __VA_ARGS__) |
341 | | |
342 | | WINPR_ATTR_NODISCARD |
343 | | WINPR_API BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel); |
344 | | |
345 | | WINPR_ATTR_NODISCARD |
346 | | WINPR_API BOOL WLog_SetStringLogLevel(wLog* log, LPCSTR level); |
347 | | |
348 | | WINPR_ATTR_NODISCARD |
349 | | WINPR_API BOOL WLog_AddStringLogFilters(LPCSTR filter); |
350 | | |
351 | | WINPR_ATTR_NODISCARD |
352 | | WINPR_API BOOL WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType); |
353 | | |
354 | | WINPR_ATTR_NODISCARD |
355 | | WINPR_API wLogAppender* WLog_GetLogAppender(wLog* log); |
356 | | |
357 | | WINPR_ATTR_NODISCARD |
358 | | WINPR_API BOOL WLog_OpenAppender(wLog* log); |
359 | | |
360 | | WINPR_ATTR_NODISCARD |
361 | | WINPR_API BOOL WLog_CloseAppender(wLog* log); |
362 | | |
363 | | WINPR_ATTR_NODISCARD |
364 | | WINPR_API BOOL WLog_ConfigureAppender(wLogAppender* appender, const char* setting, void* value); |
365 | | |
366 | | WINPR_ATTR_NODISCARD |
367 | | WINPR_API wLogLayout* WLog_GetLogLayout(wLog* log); |
368 | | |
369 | | WINPR_ATTR_NODISCARD |
370 | | WINPR_API BOOL WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format); |
371 | | |
372 | | #if defined(WITH_WINPR_DEPRECATED) |
373 | | /** Deprecated */ |
374 | | WINPR_DEPRECATED(WINPR_ATTR_NODISCARD WINPR_API BOOL WLog_Init(void)); |
375 | | |
376 | | /** Deprecated */ |
377 | | WINPR_DEPRECATED(WINPR_ATTR_NODISCARD WINPR_API BOOL WLog_Uninit(void)); |
378 | | #endif |
379 | | |
380 | | typedef BOOL (*wLogCallbackMessage_t)(const wLogMessage* msg); |
381 | | typedef BOOL (*wLogCallbackData_t)(const wLogMessage* msg); |
382 | | typedef BOOL (*wLogCallbackImage_t)(const wLogMessage* msg); |
383 | | typedef BOOL (*wLogCallbackPackage_t)(const wLogMessage* msg); |
384 | | |
385 | | typedef struct |
386 | | { |
387 | | WINPR_ATTR_NODISCARD wLogCallbackData_t data; |
388 | | WINPR_ATTR_NODISCARD wLogCallbackImage_t image; |
389 | | WINPR_ATTR_NODISCARD wLogCallbackMessage_t message; |
390 | | WINPR_ATTR_NODISCARD wLogCallbackPackage_t package; |
391 | | } wLogCallbacks; |
392 | | |
393 | | #ifdef __cplusplus |
394 | | } |
395 | | #endif |
396 | | |
397 | | #endif /* WINPR_WLOG_H */ |