2d70a58c424f06e752134ba6bca82ee016c63d06
[strongswan.git] / src / libstrongswan / printf_hook.h
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2006-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /**
18 * @defgroup printf_hook printf_hook
19 * @{ @ingroup libstrongswan
20 */
21
22 #ifndef PRINTF_HOOK_H_
23 #define PRINTF_HOOK_H_
24
25 typedef struct printf_hook_t printf_hook_t;
26 typedef struct printf_hook_spec_t printf_hook_spec_t;
27 typedef enum printf_hook_argtype_t printf_hook_argtype_t;
28
29 #ifdef HAVE_PRINTF_HOOKS
30
31 #include <stdio.h>
32 #include <printf.h>
33
34 enum printf_hook_argtype_t {
35 PRINTF_HOOK_ARGTYPE_END = -1,
36 PRINTF_HOOK_ARGTYPE_INT = PA_INT,
37 PRINTF_HOOK_ARGTYPE_POINTER = PA_POINTER,
38 };
39
40 #else
41
42 #include <vstr.h>
43
44 enum printf_hook_argtype_t {
45 PRINTF_HOOK_ARGTYPE_END = VSTR_TYPE_FMT_END,
46 PRINTF_HOOK_ARGTYPE_INT = VSTR_TYPE_FMT_INT,
47 PRINTF_HOOK_ARGTYPE_POINTER = VSTR_TYPE_FMT_PTR_VOID,
48 };
49
50 /**
51 * Redefining printf and alike
52 */
53 #include <stdio.h>
54 #include <stdarg.h>
55
56 int vstr_wrapper_printf(const char *format, ...);
57 int vstr_wrapper_fprintf(FILE *stream, const char *format, ...);
58 int vstr_wrapper_sprintf(char *str, const char *format, ...);
59 int vstr_wrapper_snprintf(char *str, size_t size, const char *format, ...);
60
61 int vstr_wrapper_vprintf(const char *format, va_list ap);
62 int vstr_wrapper_vfprintf(FILE *stream, const char *format, va_list ap);
63 int vstr_wrapper_vsprintf(char *str, const char *format, va_list ap);
64 int vstr_wrapper_vsnprintf(char *str, size_t size, const char *format, va_list ap);
65
66 #define printf vstr_wrapper_printf
67 #define fprintf vstr_wrapper_fprintf
68 #define sprintf vstr_wrapper_sprintf
69 #define snprintf vstr_wrapper_snprintf
70
71 #define vprintf vstr_wrapper_vprintf
72 #define vfprintf vstr_wrapper_vfprintf
73 #define vsprintf vstr_wrapper_vsprintf
74 #define vsnprintf vstr_wrapper_vsnprintf
75
76 #endif
77
78 /**
79 * Callback function type for printf hooks.
80 *
81 * @param dst destination buffer
82 * @param len length of the buffer
83 * @param spec format specifier
84 * @param args arguments array
85 * @return number of characters written
86 */
87 typedef int (*printf_hook_function_t)(char *dst, size_t len,
88 printf_hook_spec_t *spec,
89 const void *const *args);
90
91 /**
92 * Helper macro to be used in printf hook callbacks.
93 * buf and buflen get modified.
94 */
95 #define print_in_hook(buf, buflen, fmt, ...) ({\
96 int _written = snprintf(buf, buflen, fmt, ##__VA_ARGS__);\
97 if (_written < 0 || _written >= buflen)\
98 {\
99 _written = buflen - 1;\
100 }\
101 buf += _written;\
102 buflen -= _written;\
103 _written;\
104 })
105
106 /**
107 * Properties of the format specifier
108 */
109 struct printf_hook_spec_t {
110 /**
111 * TRUE if a '#' was used in the format specifier
112 */
113 int hash;
114
115 /**
116 * TRUE if a '-' was used in the format specifier
117 */
118 int minus;
119
120 /**
121 * The width as given in the format specifier.
122 */
123 int width;
124 };
125
126 /**
127 * Printf handler management.
128 */
129 struct printf_hook_t {
130
131 /**
132 * Register a printf handler.
133 *
134 * @param spec printf hook format character
135 * @param hook hook function
136 * @param ... list of PRINTF_HOOK_ARGTYPE_*, MUST end with PRINTF_HOOK_ARGTYPE_END
137 */
138 void (*add_handler)(printf_hook_t *this, char spec,
139 printf_hook_function_t hook, ...);
140
141 /**
142 * Destroy a printf_hook instance.
143 */
144 void (*destroy)(printf_hook_t *this);
145 };
146
147 /**
148 * Create a printf_hook instance.
149 */
150 printf_hook_t *printf_hook_create();
151
152 #endif /** PRINTF_HOOK_H_ @}*/