1 | /* $Id: logstrict.h 2436 2005-11-13 11:35:02Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * InnoTek LIBC - Debug Logging and Strict Checking Features.
|
---|
5 | *
|
---|
6 | * InnoTek Systemberatung GmbH
|
---|
7 | *
|
---|
8 | * Copyright (c) 2004 InnoTek Systemberatung GmbH
|
---|
9 | * Copyright (c) 2004-2005 knut st. osmundsen <bird-srcspam@anduin.net>
|
---|
10 | * Author: knut st. osmundsen <bird-srcspam@anduin.net>
|
---|
11 | *
|
---|
12 | * All Rights Reserved
|
---|
13 | *
|
---|
14 | */
|
---|
15 |
|
---|
16 | #ifndef __InnoTekLIBC_LOG_H__
|
---|
17 | #define __InnoTekLIBC_LOG_H__
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <sys/cdefs.h>
|
---|
23 | #include <sys/types.h> /* size_t */
|
---|
24 | #include <sys/param.h> /* NULL */
|
---|
25 |
|
---|
26 | /** @defgroup __libc_log Debug Logging and Strict Checking Features
|
---|
27 | *
|
---|
28 | * The logging feature is not accessible unless DEBUG_LOGGING is #defined.
|
---|
29 | *
|
---|
30 | * The strict checking feature is not accessible unless __LIBC_STRICT is #defined.
|
---|
31 | *
|
---|
32 | * The user of this feature must #define __LIBC_LOG_GROUP to a valid group
|
---|
33 | * number before including this file.
|
---|
34 | *
|
---|
35 | * The user may also #define __LIBC_LOG_INSTANCE if it doesn't want to use
|
---|
36 | * the default logging device.
|
---|
37 | *
|
---|
38 | * Note that all everything but the main logger & strict macros are using the
|
---|
39 | * default prefix to avoid too much namespace pollution. The main logger and
|
---|
40 | * strict macros are not prefixed with the double underscore because that
|
---|
41 | * would make the code less readable (and mean more typing which is painful).
|
---|
42 | *
|
---|
43 | * @{
|
---|
44 | */
|
---|
45 |
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Defined Constants And Macros *
|
---|
49 | *******************************************************************************/
|
---|
50 | /**
|
---|
51 | * The user may also #define __LIBC_LOG_INSTANCE if it doesn't want to use
|
---|
52 | * the default logging device.
|
---|
53 | */
|
---|
54 | #ifndef __LIBC_LOG_INSTANCE
|
---|
55 | #define __LIBC_LOG_INSTANCE NULL
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The user of this feature must #define __LIBC_LOG_GROUP to a valid group
|
---|
61 | * number before including this file.
|
---|
62 | */
|
---|
63 | #ifndef __LIBC_LOG_GROUP
|
---|
64 | #define __LIBC_LOG_GROUP 0
|
---|
65 | #error "__LIBC_LOG_GROUP must be defined before including InnoTekLIBC/log.h"
|
---|
66 | #endif
|
---|
67 |
|
---|
68 |
|
---|
69 | /** Macro to, in release mode too, log a generic message within a function. */
|
---|
70 | #define LIBCLOG_REL(...) \
|
---|
71 | __libc_LogMsg(~0, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __VA_ARGS__)
|
---|
72 |
|
---|
73 |
|
---|
74 | /** Macro to log a function entry. */
|
---|
75 | #ifdef DEBUG_LOGGING
|
---|
76 | #define LIBCLOG_ENTER(...) \
|
---|
77 | unsigned __libclog_uEnterTS__ = __libc_LogEnter(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __VA_ARGS__)
|
---|
78 | #else
|
---|
79 | #define LIBCLOG_ENTER(...) //...
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | /** Macro to log a generic message within a function entered by LIBCLOG_ENTER(). */
|
---|
83 | #ifdef DEBUG_LOGGING
|
---|
84 | #define LIBCLOG_MSG(...) \
|
---|
85 | __libc_LogMsg(__libclog_uEnterTS__, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __VA_ARGS__)
|
---|
86 | #else
|
---|
87 | #define LIBCLOG_MSG(...) ((void)0)
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | /** Macro to log a generic message within a function. */
|
---|
91 | #ifdef DEBUG_LOGGING
|
---|
92 | #define LIBCLOG_MSG2(...) \
|
---|
93 | __libc_LogMsg(~0, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __VA_ARGS__)
|
---|
94 | #else
|
---|
95 | #define LIBCLOG_MSG2(...) ((void)0)
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | /** Macro to log an user error within a function entered by LIBCLOG_ENTER(). */
|
---|
99 | #ifdef DEBUG_LOGGING
|
---|
100 | #define LIBCLOG_ERROR(...) \
|
---|
101 | __libc_LogError(__libclog_uEnterTS__, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
|
---|
102 | #else
|
---|
103 | #define LIBCLOG_ERROR(...) ((void)0)
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /** Macro to log an user error within a function. */
|
---|
107 | #ifdef DEBUG_LOGGING
|
---|
108 | #define LIBCLOG_ERROR2(...) \
|
---|
109 | __libc_LogError(~0, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
|
---|
110 | #else
|
---|
111 | #define LIBCLOG_ERROR2(...) ((void)0)
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | /** Macro to check for and log an user error within a function entered by LIBCLOG_ENTER(). */
|
---|
115 | #ifdef DEBUG_LOGGING
|
---|
116 | #define LIBCLOG_ERROR_CHECK(expr, ...) \
|
---|
117 | (!(expr) ? __libc_LogError(__libclog_uEnterTS__, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) : ((void)0) )
|
---|
118 | #else
|
---|
119 | #define LIBCLOG_ERROR_CHECK(expr, ...) ((void)0)
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | /** Macro to check for and log an user error within a function. */
|
---|
123 | #ifdef DEBUG_LOGGING
|
---|
124 | #define LIBCLOG_ERROR2_CHECK(expr, ...) \
|
---|
125 | (!(expr) ? __libc_LogError(~0, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) : ((void)0) )
|
---|
126 | #else
|
---|
127 | #define LIBCLOG_ERROR2_CHECK(expr, ...) ((void)0)
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | /** Macro to log a raw message. */
|
---|
131 | #ifdef DEBUG_LOGGING
|
---|
132 | #define LIBCLOG_RAW(string, maxlen) \
|
---|
133 | __libc_LogRaw(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, string, maxlen)
|
---|
134 | #else
|
---|
135 | #define LIBCLOG_RAW(...) ((void)0)
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | /** Macro to leave a function entered by LIBCLOG_ENTER(). */
|
---|
139 | #ifdef DEBUG_LOGGING
|
---|
140 | #define LIBCLOG_LEAVE(...) \
|
---|
141 | __libc_LogLeave(__libclog_uEnterTS__, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __VA_ARGS__)
|
---|
142 | #else
|
---|
143 | #define LIBCLOG_LEAVE(...) ((void)0)
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | /** Macro to log a custom message and return. */
|
---|
147 | #define LIBCLOG_RETURN_MSG(rc,...) do { LIBCLOG_LEAVE(__VA_ARGS__); return (rc); } while (0)
|
---|
148 | /** Macro to log a custom message and return. */
|
---|
149 | #define LIBCLOG_RETURN_MSG_VOID(...) do { LIBCLOG_LEAVE(__VA_ARGS__); return; } while (0)
|
---|
150 |
|
---|
151 | /** Macro to log a void return and do the return. */
|
---|
152 | #define LIBCLOG_RETURN_VOID() LIBCLOG_RETURN_MSG_VOID( "ret void\n")
|
---|
153 | /** Macro to log an int return and do the return. */
|
---|
154 | #define LIBCLOG_RETURN_INT(rc) LIBCLOG_RETURN_MSG((rc), "ret %d (%#x)\n", (rc), (rc))
|
---|
155 | /** Macro to log an unsigned int return and do the return. */
|
---|
156 | #define LIBCLOG_RETURN_UINT(rc) LIBCLOG_RETURN_MSG((rc), "ret %u (%#x)\n", (rc), (rc))
|
---|
157 | /** Macro to log an long int return and do the return. */
|
---|
158 | #define LIBCLOG_RETURN_LONG(rc) LIBCLOG_RETURN_MSG((rc), "ret %ld (%#lx)\n", (rc), (rc))
|
---|
159 | /** Macro to log an unsigned long int return and do the return. */
|
---|
160 | #define LIBCLOG_RETURN_ULONG(rc) LIBCLOG_RETURN_MSG((rc), "ret %lu (%#lx)\n", (rc), (rc))
|
---|
161 | /** Macro to log a pointer return and do the return. */
|
---|
162 | #define LIBCLOG_RETURN_P(rc) LIBCLOG_RETURN_MSG((rc), "ret %p\n", (void*)(rc))
|
---|
163 |
|
---|
164 |
|
---|
165 | /** Macro to leave a function entered by LIBCLOG_ENTER() on user error. */
|
---|
166 | #ifdef DEBUG_LOGGING
|
---|
167 | #define LIBCLOG_ERROR_LEAVE(...) \
|
---|
168 | __libc_LogErrorLeave(__libclog_uEnterTS__, __LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
|
---|
169 | #else
|
---|
170 | #define LIBCLOG_ERROR_LEAVE(...) ((void)0)
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | /** Macro to log a user error and return. */
|
---|
174 | #define LIBCLOG_ERROR_RETURN(rc,...) do { LIBCLOG_ERROR_LEAVE(__VA_ARGS__); return (rc); } while (0)
|
---|
175 | #define LIBCLOG_ERROR_RETURN_MSG LIBCLOG_ERROR_RETURN
|
---|
176 | /** Macro to log a user error and return. */
|
---|
177 | #define LIBCLOG_ERROR_RETURN_MSG_VOID(...) do { LIBCLOG_ERROR_LEAVE(__VA_ARGS__); return; } while (0)
|
---|
178 |
|
---|
179 | /** Macro to log a void return user error and do the return. */
|
---|
180 | #define LIBCLOG_ERROR_RETURN_VOID() LIBCLOG_ERROR_RETURN_MSG_VOID( "ret void\n")
|
---|
181 | /** Macro to log an int return user error and do the return. */
|
---|
182 | #define LIBCLOG_ERROR_RETURN_INT(rc) LIBCLOG_ERROR_RETURN_MSG((rc), "ret %d (%#x)\n", (rc), (rc))
|
---|
183 | /** Macro to log an unsigned int return user error and do the return. */
|
---|
184 | #define LIBCLOG_ERROR_RETURN_UINT(rc) LIBCLOG_ERROR_RETURN_MSG((rc), "ret %u (%#x)\n", (rc), (rc))
|
---|
185 | /** Macro to log an long int return user error and do the return. */
|
---|
186 | #define LIBCLOG_ERROR_RETURN_LONG(rc) LIBCLOG_ERROR_RETURN_MSG((rc), "ret %ld (%#lx)\n", (rc), (rc))
|
---|
187 | /** Macro to log an unsigned long int return user error and do the return. */
|
---|
188 | #define LIBCLOG_ERROR_RETURN_ULONG(rc) LIBCLOG_ERROR_RETURN_MSG((rc), "ret %lu (%#lx)\n", (rc), (rc))
|
---|
189 | /** Macro to log a pointer return user error and do the return. */
|
---|
190 | #define LIBCLOG_ERROR_RETURN_P(rc) LIBCLOG_ERROR_RETURN_MSG((rc), "ret %p\n", (void*)(rc))
|
---|
191 |
|
---|
192 | /** Macro to log an typical int return which is an error if negative and otherwise a success. */
|
---|
193 | #define LIBCLOG_MIX_RETURN_INT(rc) do { if (rc < 0) LIBCLOG_ERROR_RETURN_INT(rc); LIBCLOG_RETURN_INT(rc); } while (0)
|
---|
194 |
|
---|
195 | /** Macro to log an typical int return which is an error if not zero and otherwise a success. */
|
---|
196 | #define LIBCLOG_MIX0_RETURN_INT(rc) do { if (rc) LIBCLOG_ERROR_RETURN_INT(rc); LIBCLOG_RETURN_INT(rc); } while (0)
|
---|
197 |
|
---|
198 |
|
---|
199 | /** @defgroup __libc_log_flags Message Flags (to be combined with group)
|
---|
200 | *
|
---|
201 | * In source files which uses the logger you can or these flags together
|
---|
202 | * with a group specification in the __LIBC_LOG_GROUP #define.
|
---|
203 | * @{
|
---|
204 | */
|
---|
205 | /** Forces a flush of the output file after the message have been written. */
|
---|
206 | #define __LIBC_LOG_MSGF_FLUSH 0x00010000
|
---|
207 | /** @} */
|
---|
208 |
|
---|
209 |
|
---|
210 | /** @defgroup __libc_log_groups Default Logging Groups
|
---|
211 | *
|
---|
212 | * In source files which uses the default logger you must #define
|
---|
213 | * __LIBC_LOG_GROUP to one of these defines.
|
---|
214 | *
|
---|
215 | * @{
|
---|
216 | */
|
---|
217 | /** whatever. */
|
---|
218 | #define __LIBC_LOG_GRP_NOGROUP 0
|
---|
219 |
|
---|
220 | /*-- LIBC --*/
|
---|
221 | /** Process APIs. */
|
---|
222 | #define __LIBC_LOG_GRP_PROCESS 1
|
---|
223 | /** Heap APIs. */
|
---|
224 | #define __LIBC_LOG_GRP_HEAP 2
|
---|
225 | /** File stream APIs. */
|
---|
226 | #define __LIBC_LOG_GRP_STREAM 3
|
---|
227 | /** Other I/O APIs. */
|
---|
228 | #define __LIBC_LOG_GRP_IO 4
|
---|
229 | /** String APIs. */
|
---|
230 | #define __LIBC_LOG_GRP_STRING 5
|
---|
231 | /** Locale APIs. */
|
---|
232 | #define __LIBC_LOG_GRP_LOCALE 6
|
---|
233 | /** Regular expression APIs. */
|
---|
234 | #define __LIBC_LOG_GRP_REGEX 7
|
---|
235 | /** Math APIs. */
|
---|
236 | #define __LIBC_LOG_GRP_MATH 8
|
---|
237 | /** Time APIs. */
|
---|
238 | #define __LIBC_LOG_GRP_TIME 9
|
---|
239 | /** BSD DB APIs. */
|
---|
240 | #define __LIBC_LOG_GRP_BSD_DB 10
|
---|
241 | /** GLIBC POSIX APIs. */
|
---|
242 | #define __LIBC_LOG_GRP_GLIBC_POSIX 11
|
---|
243 | /** Thread APIs. */
|
---|
244 | #define __LIBC_LOG_GRP_THREAD 12
|
---|
245 | /** Mutex Semaphores. */
|
---|
246 | #define __LIBC_LOG_GRP_MUTEX 13
|
---|
247 | /** Signal APIs and Events. */
|
---|
248 | #define __LIBC_LOG_GRP_SIGNAL 14
|
---|
249 | /** Environment APIs. */
|
---|
250 | #define __LIBC_LOG_GRP_ENV 15
|
---|
251 | /** Memory Manager APIs. */
|
---|
252 | #define __LIBC_LOG_GRP_MMAN 16
|
---|
253 | /** Load APIs. */
|
---|
254 | #define __LIBC_LOG_GRP_LDR 1 /** @todo fix me */
|
---|
255 |
|
---|
256 | /** Backend SysV IPC APIs. */
|
---|
257 | #define __LIBC_LOG_GRP_BACK_IPC 17
|
---|
258 | /** Backend Thread APIs. */
|
---|
259 | #define __LIBC_LOG_GRP_BACK_THREAD 18
|
---|
260 | /** Backend Process APIs. */
|
---|
261 | #define __LIBC_LOG_GRP_BACK_PROCESS 19
|
---|
262 | /** Backend Signal APIs and Events. */
|
---|
263 | #define __LIBC_LOG_GRP_BACK_SIGNAL 20
|
---|
264 | /** Backend Memory Mananger APIs. */
|
---|
265 | #define __LIBC_LOG_GRP_BACK_MMAN 21
|
---|
266 | /** Backend Loader APIs. */
|
---|
267 | #define __LIBC_LOG_GRP_BACK_LDR 22
|
---|
268 | /** Backend Filesystem APIs. */
|
---|
269 | #define __LIBC_LOG_GRP_BACK_FS 23
|
---|
270 | /** Shared Process Database and LIBC Shared Memory APIs. */
|
---|
271 | #define __LIBC_LOG_GRP_BACK_SPM 24
|
---|
272 | /** Fork APIs. */
|
---|
273 | #define __LIBC_LOG_GRP_FORK 25
|
---|
274 | /** Backend IO APIs. */
|
---|
275 | #define __LIBC_LOG_GRP_BACK_IO 26
|
---|
276 | /** Init/Term APIs and Events. */
|
---|
277 | #define __LIBC_LOG_GRP_INITTERM 27
|
---|
278 | /** Backend APIs. */
|
---|
279 | #define __LIBC_LOG_GRP_BACKEND 28
|
---|
280 | /** Misc APIs. */
|
---|
281 | #define __LIBC_LOG_GRP_MISC 29
|
---|
282 | /** BSD Gen APIs. */
|
---|
283 | #define __LIBC_LOG_GRP_BSD_GEN 30
|
---|
284 | /** GLIBC Misc APIs. */
|
---|
285 | #define __LIBC_LOG_GRP_GLIBC_MISC 31
|
---|
286 |
|
---|
287 | /*-- other libraries/APIs --*/
|
---|
288 | /** Socket APIs. */
|
---|
289 | #define __LIBC_LOG_GRP_SOCKET 32
|
---|
290 | /** Other TCP/IP APIs. */
|
---|
291 | #define __LIBC_LOG_GRP_TCPIP 33
|
---|
292 | /** iconv APIs. */
|
---|
293 | #define __LIBC_LOG_GRP_ICONV 34
|
---|
294 | /** Dynamic Library (libdl) APIs. */
|
---|
295 | #define __LIBC_LOG_GRP_DLFCN 35
|
---|
296 | /** Posix thread APIs. */
|
---|
297 | #define __LIBC_LOG_GRP_PTHREAD 36
|
---|
298 | /** Posix thread APIs. */
|
---|
299 | #define __LIBC_LOG_GRP_DOSEX 37
|
---|
300 |
|
---|
301 | /** @todo complete this */
|
---|
302 | #define __LIBC_LOG_GRP_MAX 37
|
---|
303 | /** @} */
|
---|
304 |
|
---|
305 |
|
---|
306 | /** @defgroup __libc_log_strict Strict Assertions
|
---|
307 | * @{ */
|
---|
308 |
|
---|
309 | /** Generic assertion.
|
---|
310 | * @param expr Boolean expression,
|
---|
311 | */
|
---|
312 | #ifdef __LIBC_STRICT
|
---|
313 | #define LIBC_ASSERT(expr) ((expr) ? (void)0 \
|
---|
314 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #expr, NULL))
|
---|
315 | #else
|
---|
316 | #define LIBC_ASSERT(expr) ((void)0)
|
---|
317 | #endif
|
---|
318 |
|
---|
319 | /** Generic assertion failed.
|
---|
320 | * (Yeah, this always fails.)
|
---|
321 | */
|
---|
322 | #ifdef __LIBC_STRICT
|
---|
323 | #define LIBC_ASSERT_FAILED() __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, "0", NULL)
|
---|
324 | #else
|
---|
325 | #define LIBC_ASSERT_FAILED() ((void)0)
|
---|
326 | #endif
|
---|
327 |
|
---|
328 | /** Assert that a memory buffer is readable.
|
---|
329 | * @param pv Pointer to buffer.
|
---|
330 | * @param cb Size of buffer.
|
---|
331 | */
|
---|
332 | #ifdef __LIBC_STRICT
|
---|
333 | #define LIBC_ASSERT_MEM_R(pv, cb) (__libc_StrictMemoryR((pv), (cb)) ? (void)0 \
|
---|
334 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #pv "; " #cb, \
|
---|
335 | "Memory buffer at %p of %d bytes isn't readable!\n", (pv), (cb)))
|
---|
336 | #else
|
---|
337 | #define LIBC_ASSERT_MEM_R(pv, cb) ((void)0)
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | /** Assert that a memory buffer is readable and writable.
|
---|
341 | * @param pv Pointer to buffer.
|
---|
342 | * @param cb Size of buffer.
|
---|
343 | */
|
---|
344 | #ifdef __LIBC_STRICT
|
---|
345 | #define LIBC_ASSERT_MEM_RW(pv, cb) (__libc_StrictMemoryRW((pv), (cb)) ? (void)0 \
|
---|
346 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #pv "; " #cb, \
|
---|
347 | "Memory buffer at %p of %d bytes isn't readable and writable!\n", (pv), (cb)))
|
---|
348 | #else
|
---|
349 | #define LIBC_ASSERT_MEM_RW(pv, cb) ((void)0)
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | /** Assert that a zero terminated string is readable.
|
---|
353 | * @param psz Pointer to buffer.
|
---|
354 | */
|
---|
355 | #ifdef __LIBC_STRICT
|
---|
356 | #define LIBC_ASSERT_STR(psz) (__libc_StrictStringR((psz), ~0) ? (void)0 \
|
---|
357 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #psz, \
|
---|
358 | "String at %p isn't readable!\n", (psz)))
|
---|
359 | #else
|
---|
360 | #define LIBC_ASSERT_STR(psz) ((void)0)
|
---|
361 | #endif
|
---|
362 |
|
---|
363 | /** Assert that a zero terminated string with a maximum lenght is readable.
|
---|
364 | * @param psz Pointer to buffer.
|
---|
365 | * @param cchMax Max string length.
|
---|
366 | */
|
---|
367 | #ifdef __LIBC_STRICT
|
---|
368 | #define LIBC_ASSERT_NSTR(psz, cchMax) (__libc_StrictStringR((psz), cchMax) ? (void)0 \
|
---|
369 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #psz " " #cchMax, \
|
---|
370 | "String at %p of maximum %d bytes isn't readable!\n", (psz), (cchMax)))
|
---|
371 | #else
|
---|
372 | #define LIBC_ASSERT_NSTR(psz, cchMax) ((void)0)
|
---|
373 | #endif
|
---|
374 |
|
---|
375 |
|
---|
376 | /** Generic assertion, custom message.
|
---|
377 | * @param expr Boolean expression,
|
---|
378 | * @param ... Custom error message.
|
---|
379 | */
|
---|
380 | #ifdef __LIBC_STRICT
|
---|
381 | #define LIBC_ASSERTM(expr, ...) ((expr) ? (void)0 \
|
---|
382 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #expr, \
|
---|
383 | __VA_ARGS__))
|
---|
384 | #else
|
---|
385 | #define LIBC_ASSERTM(expr, ...) ((void)0)
|
---|
386 | #endif
|
---|
387 |
|
---|
388 | /** Generic assertion failed, custom message.
|
---|
389 | * (Yeah, this always fails.)
|
---|
390 | * @param ... Custom error message.
|
---|
391 | */
|
---|
392 | #ifdef __LIBC_STRICT
|
---|
393 | #define LIBC_ASSERTM_FAILED(...) __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, "0", __VA_ARGS__)
|
---|
394 | #else
|
---|
395 | #define LIBC_ASSERTM_FAILED(...) ((void)0)
|
---|
396 | #endif
|
---|
397 |
|
---|
398 | /** Assert that a memory buffer is readable, custom message
|
---|
399 | * @param pv Pointer to buffer.
|
---|
400 | * @param cb Size of buffer.
|
---|
401 | */
|
---|
402 | #ifdef __LIBC_STRICT
|
---|
403 | #define LIBC_ASSERTM_MEM_R(pv, cb, ...) (__libc_StrictMemoryR((pv), (cb)) ? (void)0 \
|
---|
404 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #pv "; " #cb, \
|
---|
405 | __VA_ARGS__))
|
---|
406 | #else
|
---|
407 | #define LIBC_ASSERTM_MEM_R(pv, cb, ...) ((void)0)
|
---|
408 | #endif
|
---|
409 |
|
---|
410 | /** Assert that a memory buffer is readable and writable, custom message
|
---|
411 | * @param pv Pointer to buffer.
|
---|
412 | * @param cb Size of buffer.
|
---|
413 | */
|
---|
414 | #ifdef __LIBC_STRICT
|
---|
415 | #define LIBC_ASSERTM_MEM_RW(pv, cb, ...) (__libc_StrictMemoryRW((pv), (cb)) ? (void)0 \
|
---|
416 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #pv "; " #cb, \
|
---|
417 | __VA_ARGS__))
|
---|
418 | #else
|
---|
419 | #define LIBC_ASSERTM_MEM_RW(pv, cb, ...) ((void)0)
|
---|
420 | #endif
|
---|
421 |
|
---|
422 | /** Assert that a zero terminated string is readable, custom message
|
---|
423 | * @param psz Pointer to buffer.
|
---|
424 | */
|
---|
425 | #ifdef __LIBC_STRICT
|
---|
426 | #define LIBC_ASSERTM_STR(psz, ...) (__libc_StrictStringR((psz), ~0) ? (void)0 \
|
---|
427 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #psz, \
|
---|
428 | __VA_ARGS__))
|
---|
429 | #else
|
---|
430 | #define LIBC_ASSERTM_STR(psz, ...) ((void)0)
|
---|
431 | #endif
|
---|
432 |
|
---|
433 | /** Assert that a zero terminated string with a maximum lenght is readable, custom message
|
---|
434 | * @param psz Pointer to buffer.
|
---|
435 | * @param cchMax Max string length.
|
---|
436 | */
|
---|
437 | #ifdef __LIBC_STRICT
|
---|
438 | #define LIBC_ASSERTM_NSTR(psz, cchMax, ...) (__libc_StrictStringR((psz), cchMax) ? (void)0 \
|
---|
439 | : __libc_LogAssert(__LIBC_LOG_INSTANCE, __LIBC_LOG_GROUP, __PRETTY_FUNCTION__, __FILE__, __LINE__, #psz " " #cchMax, \
|
---|
440 | __VA_ARGS__))
|
---|
441 | #else
|
---|
442 | #define LIBC_ASSERTM_NSTR(psz, cchMax, ...) ((void)0)
|
---|
443 | #endif
|
---|
444 |
|
---|
445 | /** Extracts the group from the fGroupAndFlags argument. */
|
---|
446 | #define __LIBC_LOG_GETGROUP(fGroupAndFlags) ((fGroupAndFlags) & 0xffff)
|
---|
447 |
|
---|
448 | /** @} */
|
---|
449 |
|
---|
450 |
|
---|
451 | /*******************************************************************************
|
---|
452 | * Structures and Typedefs *
|
---|
453 | *******************************************************************************/
|
---|
454 | /** Logging group. */
|
---|
455 | typedef struct __libc_log_group
|
---|
456 | {
|
---|
457 | /** Set if logging for the group is enabled, clear if it's disabled. */
|
---|
458 | int fEnabled;
|
---|
459 | /** Group name */
|
---|
460 | const char * pszGroupName;
|
---|
461 | } __LIBC_LOGGROUP, *__LIBC_PLOGGROUP;
|
---|
462 |
|
---|
463 | /** Ordered collection of logging groups. */
|
---|
464 | typedef struct __libc_log_groups
|
---|
465 | {
|
---|
466 | /** Group index base. This value is subtracted from the group part of the
|
---|
467 | * fFlagsAndGroups arguments to make an index into paGroups. */
|
---|
468 | unsigned uBase;
|
---|
469 | /** Number of groups in the array. */
|
---|
470 | unsigned cGroups;
|
---|
471 | /** Array of log groups. */
|
---|
472 | __LIBC_PLOGGROUP paGroups;
|
---|
473 | } __LIBC_LOGGROUPS, *__LIBC_PLOGGROUPS;
|
---|
474 |
|
---|
475 |
|
---|
476 | /*******************************************************************************
|
---|
477 | * External Functions *
|
---|
478 | *******************************************************************************/
|
---|
479 | __BEGIN_DECLS
|
---|
480 | /**
|
---|
481 | * Create a logger.
|
---|
482 | *
|
---|
483 | * @returns Pointer to a logger instance on success.
|
---|
484 | * @returns NULL on failure. errno is set.
|
---|
485 | * @param fFlags Flags reserved for future use. Set to zero.
|
---|
486 | * @param pGroups Pointer to a table of logging groups used for this
|
---|
487 | * logger instance.
|
---|
488 | * @param pszFilenameFormat Format string for making up the log filename.
|
---|
489 | * @param ... Arguments to the format string.
|
---|
490 | */
|
---|
491 | extern void *__libc_LogInit(unsigned fFlags, __LIBC_PLOGGROUPS pGroups, const char *pszFilenameFormat, ...) __printflike(3, 4);
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Parses the given environment variable and sets the group
|
---|
495 | * flags accordingly.
|
---|
496 | *
|
---|
497 | * The environment variable is a sequence of group idendifiers with
|
---|
498 | * a prefix which determins whether or not that group is enabled.
|
---|
499 | * A special group 'all' can be used to address all groups.
|
---|
500 | *
|
---|
501 | * If the environment variable is not present no changes will be
|
---|
502 | * performed.
|
---|
503 | *
|
---|
504 | * @param pGroups Pointer to groups to init.
|
---|
505 | * @param pszEnvVar Name of the environment variable.
|
---|
506 | * This is taken from the initial environment of the process
|
---|
507 | * and not from the current!!
|
---|
508 | */
|
---|
509 | extern void __libc_LogGroupInit(__LIBC_PLOGGROUPS pGroups, const char *pszEnvVar);
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Terminate (or close if you like) a logger instance.
|
---|
513 | * This means flushing any buffered messages and writing a termination
|
---|
514 | * message before closing the log file.
|
---|
515 | *
|
---|
516 | * @returns 0 on succes.
|
---|
517 | * @returns -1 on failure, error is set.
|
---|
518 | * @param pvInstance Logger instance.
|
---|
519 | */
|
---|
520 | extern int __libc_LogTerm(void *pvInstance);
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * Output an enter function log message.
|
---|
524 | * An enter message is considered to be one line and is appended a newline if
|
---|
525 | * none was given.
|
---|
526 | *
|
---|
527 | * @returns Current timestamp.
|
---|
528 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
529 | * default log instance.
|
---|
530 | * @param fGroupAndFlags Logging group and logging flags.
|
---|
531 | * @param pszFunction Name of the function which was entered.
|
---|
532 | * @param pszFormat Format string to display arguments.
|
---|
533 | * @param ... Arguments to the format string.
|
---|
534 | */
|
---|
535 | extern unsigned __libc_LogEnter(void *pvInstance, unsigned fGroupAndFlags, const char *pszFunction, const char *pszFormat, ...) __printflike(4, 5);
|
---|
536 |
|
---|
537 | /**
|
---|
538 | * Output a leave function log message.
|
---|
539 | *
|
---|
540 | * A leave message is considered to be one line and is appended a newline if
|
---|
541 | * none was given.
|
---|
542 | *
|
---|
543 | * @param uEnterTS The timestamp returned by LogEnter.
|
---|
544 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
545 | * default log instance.
|
---|
546 | * @param fGroupAndFlags Logging group and logging flags.
|
---|
547 | * @param pszFunction Name of the function which was entered.
|
---|
548 | * @param pszFormat Format string to display the result.
|
---|
549 | * @param ... Arguments to the format string.
|
---|
550 | */
|
---|
551 | extern void __libc_LogLeave(unsigned uEnterTS, void *pvInstance, unsigned fGroupAndFlags, const char *pszFunction, const char *pszFormat, ...) __printflike(5, 6);
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Output a leave function log message upon a user error.
|
---|
555 | *
|
---|
556 | * The function may breakpoint if configured to do so. A leave message is
|
---|
557 | * considered to be one line and is appended a newline if none was given.
|
---|
558 | *
|
---|
559 | * @param uEnterTS The timestamp returned by LogEnter.
|
---|
560 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
561 | * default log instance.
|
---|
562 | * @param fGroupAndFlags Logging group and logging flags.
|
---|
563 | * @param pszFunction Name of the function which was entered.
|
---|
564 | * @param pszFile Source filename.
|
---|
565 | * @param uLine Line number.
|
---|
566 | * @param pszFormat Format string to display the result.
|
---|
567 | * @param ... Arguments to the format string.
|
---|
568 | */
|
---|
569 | extern void __libc_LogErrorLeave(unsigned uEnterTS, void *pvInstance, unsigned fGroupAndFlags, const char *pszFunction, const char *pszFile, unsigned uLine, const char *pszFormat, ...) __printflike(7, 8);
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * Output a log message.
|
---|
573 | *
|
---|
574 | * A log message is considered to be one line and is appended a newline if
|
---|
575 | * none was given.
|
---|
576 | *
|
---|
577 | * @param uEnterTS The timestamp returned by LogEnter.
|
---|
578 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
579 | * default log instance.
|
---|
580 | * @param fGroupAndFlags Logging group and logging flags.
|
---|
581 | * @param pszFunction Name of the function which was entered.
|
---|
582 | * @param pszFormat Format string for the message to log.
|
---|
583 | * @param ... Arguments to the format string.
|
---|
584 | */
|
---|
585 | extern void __libc_LogMsg(unsigned uEnterTS, void *pvInstance, unsigned fGroupAndFlags, const char *pszFunction, const char *pszFormat, ...) __printflike(5, 6);
|
---|
586 |
|
---|
587 | /**
|
---|
588 | * Output a log user error message.
|
---|
589 | *
|
---|
590 | * This may raise a breakpoint exception if configured so. A log message is
|
---|
591 | * considered to be one line and is appended a newline if none was given.
|
---|
592 | *
|
---|
593 | * @param uEnterTS The timestamp returned by LogEnter.
|
---|
594 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
595 | * default log instance.
|
---|
596 | * @param fGroupAndFlags Logging group and logging flags.
|
---|
597 | * @param pszFunction Name of the function which was entered.
|
---|
598 | * @param pszFile Source filename.
|
---|
599 | * @param uLine Line number.
|
---|
600 | * @param pszFormat Format string for the message to log.
|
---|
601 | * @param ... Arguments to the format string.
|
---|
602 | */
|
---|
603 | extern void __libc_LogError(unsigned uEnterTS, void *pvInstance, unsigned fGroupAndFlags, const char *pszFunction, const char *pszFile, unsigned uLine, const char *pszFormat, ...) __printflike(7, 8);
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Output a raw log message.
|
---|
607 | * Nothing is prepended. No newline is appended.
|
---|
608 | *
|
---|
609 | * @param uEnterTS The timestamp returned by LogEnter.
|
---|
610 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
611 | * default log instance.
|
---|
612 | * @param fGroupAndFlags Logging group and logging flags.
|
---|
613 | * @param pszFunction Name of the function which was entered.
|
---|
614 | * @param pszString Pointer to raw log message.
|
---|
615 | * @param cchMax Maximum number of bytes to write.
|
---|
616 | */
|
---|
617 | extern void __libc_LogRaw(void *pvInstance, unsigned fGroupAndFlags, const char *pszString, unsigned cchMax);
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Dumps a byte block.
|
---|
621 | *
|
---|
622 | * @param uEnterTS The timestamp returned by LogEnter.
|
---|
623 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
624 | * default log instance.
|
---|
625 | * @param fGroupAndFlags Logging group and logging flags.
|
---|
626 | * @param pszFunction Name of the function which was entered.
|
---|
627 | * @param pvData Pointer to the bytes to dump.
|
---|
628 | * @param cbData Number of bytes to dump.
|
---|
629 | * @param pszFormat Format string for the message to log.
|
---|
630 | * @param ... Arguments to the format string.
|
---|
631 | */
|
---|
632 | extern void __libc_LogDumpHex(unsigned uEnterTS, void *pvInstance, unsigned fGroupAndFlags, const char *pszFunction, void *pvData, unsigned cbData, const char *pszFormat, ...);
|
---|
633 |
|
---|
634 | /**
|
---|
635 | * Assertion helper.
|
---|
636 | * Logs and displays (stderr) an assertion failed message.
|
---|
637 | *
|
---|
638 | * @param pvInstance Logger instance. If NULL the message goes to the
|
---|
639 | * default log instance.
|
---|
640 | * @param pszFunction Name of the function which was entered.
|
---|
641 | * @param pszFile Source filename.
|
---|
642 | * @param uLine Line number.
|
---|
643 | * @param pszExpression Expression.
|
---|
644 | * @param pszFormat Format string for the message to log.
|
---|
645 | * @param ... Arguments to the format string.
|
---|
646 | */
|
---|
647 | extern void __libc_LogAssert(void *pvInstance, unsigned fGroupAndFlags,
|
---|
648 | const char *pszFunction, const char *pszFile, unsigned uLine, const char *pszExpression,
|
---|
649 | const char *pszFormat, ...) __printflike(7, 8);
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Validate a memory area for read access.
|
---|
653 | * @returns 1 if readable.
|
---|
654 | * @returns 0 if not entirely readable.
|
---|
655 | * @param pv Pointer to memory area.
|
---|
656 | * @param cb Size of memory area.
|
---|
657 | */
|
---|
658 | extern int __libc_StrictMemoryR(const void *pv, size_t cb);
|
---|
659 |
|
---|
660 | /**
|
---|
661 | * Validate a memory area for read & write access.
|
---|
662 | * @returns 1 if readable and writable.
|
---|
663 | * @returns 0 if not entirely readable and writable.
|
---|
664 | * @param pv Pointer to memory area.
|
---|
665 | * @param cb Size of memory area.
|
---|
666 | */
|
---|
667 | extern int __libc_StrictMemoryRW(void *pv, size_t cb);
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Validate a zero terminated string for read access.
|
---|
671 | * @returns 1 if readable.
|
---|
672 | * @returns 0 if not entirely readable.
|
---|
673 | * @param psz Pointer to string.
|
---|
674 | * @param cchMax Max string length. Use ~0 if to very all the
|
---|
675 | * way to the terminator.
|
---|
676 | */
|
---|
677 | extern int __libc_StrictStringR(const char *psz, size_t cchMax);
|
---|
678 |
|
---|
679 | __END_DECLS
|
---|
680 |
|
---|
681 | /** @} */
|
---|
682 |
|
---|
683 | #endif
|
---|