eee6d44dcab0e04929b5d61cb4806a66ed17ea5b
[m6w6/ext-psi] / src / debug.h
1 /*******************************************************************************
2 Copyright (c) 2018, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifndef PSI_DEBUG_H
27 #define PSI_DEBUG_H
28
29 #include <stdarg.h>
30 #include <stdio.h>
31
32 #include "data.h"
33
34 #if PSI_THREADED_PARSER
35 void psi_debug_lock(struct psi_data *data);
36 void psi_debug_unlock(struct psi_data *data);
37 #else
38 # define psi_debug_lock(ctx)
39 # define psi_debug_unlock(ctx)
40 #endif
41
42 #define PSI_DEBUG_PRINT(ctx, ...) do { \
43 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
44 psi_debug_lock(PSI_DATA(ctx)); \
45 dprintf(PSI_DATA(ctx)->debug_fd, __VA_ARGS__); \
46 psi_debug_unlock(PSI_DATA(ctx)); \
47 } \
48 } while(0)
49
50 #define PSI_DEBUG_PRINTV(ctx, msg, argv) do { \
51 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
52 psi_debug_lock(PSI_DATA(ctx)); \
53 vdprintf(PSI_DATA(ctx)->debug_fd, msg, argv); \
54 psi_debug_unlock(PSI_DATA(ctx)); \
55 } \
56 } while(0)
57
58 #define PSI_DEBUG_DUMP(ctx, dump_func, ...) do { \
59 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
60 struct psi_dump dump_ = {{ .fd = PSI_DATA(ctx)->debug_fd}, \
61 .fun = (psi_dump_cb) dprintf}; \
62 psi_debug_lock(PSI_DATA(ctx)); \
63 dump_func(&dump_, __VA_ARGS__); \
64 psi_debug_unlock(PSI_DATA(ctx)); \
65 } \
66 } while (0)
67
68 #endif /* PSI_DEBUG_H */