2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2006-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
18 * @defgroup printf_hook printf_hook
19 * @{ @ingroup libstrongswan
22 #ifndef PRINTF_HOOK_H_
23 #define PRINTF_HOOK_H_
25 typedef struct printf_hook_t printf_hook_t
;
26 typedef struct printf_hook_spec_t printf_hook_spec_t
;
27 typedef struct printf_hook_data_t printf_hook_data_t
;
28 typedef enum printf_hook_argtype_t printf_hook_argtype_t
;
30 #if !defined(USE_VSTR) && \
31 !defined(HAVE_PRINTF_FUNCTION) && \
32 !defined(HAVE_PRINTF_SPECIFIER)
33 /* assume newer glibc register_printf_specifier if none given */
34 #define HAVE_PRINTF_SPECIFIER
37 #if !defined(USE_VSTR) && \
38 (defined(HAVE_PRINTF_FUNCTION) || defined(HAVE_PRINTF_SPECIFIER))
43 enum printf_hook_argtype_t
{
44 PRINTF_HOOK_ARGTYPE_END
= -1,
45 PRINTF_HOOK_ARGTYPE_INT
= PA_INT
,
46 PRINTF_HOOK_ARGTYPE_POINTER
= PA_POINTER
,
50 * Data to pass to a printf hook.
52 struct printf_hook_data_t
{
61 * Helper macro to be used in printf hook callbacks.
63 #define print_in_hook(data, fmt, ...) ({\
64 int _written = fprintf(data->stream, fmt, ##__VA_ARGS__);\
76 enum printf_hook_argtype_t
{
77 PRINTF_HOOK_ARGTYPE_END
= VSTR_TYPE_FMT_END
,
78 PRINTF_HOOK_ARGTYPE_INT
= VSTR_TYPE_FMT_INT
,
79 PRINTF_HOOK_ARGTYPE_POINTER
= VSTR_TYPE_FMT_PTR_VOID
,
83 * Redefining printf and alike
88 int vstr_wrapper_printf(const char *format
, ...);
89 int vstr_wrapper_fprintf(FILE *stream
, const char *format
, ...);
90 int vstr_wrapper_sprintf(char *str
, const char *format
, ...);
91 int vstr_wrapper_snprintf(char *str
, size_t size
, const char *format
, ...);
92 int vstr_wrapper_asprintf(char **str
, const char *format
, ...);
94 int vstr_wrapper_vprintf(const char *format
, va_list ap
);
95 int vstr_wrapper_vfprintf(FILE *stream
, const char *format
, va_list ap
);
96 int vstr_wrapper_vsprintf(char *str
, const char *format
, va_list ap
);
97 int vstr_wrapper_vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
98 int vstr_wrapper_vasprintf(char **str
, const char *format
, va_list ap
);
100 #define printf vstr_wrapper_printf
101 #define fprintf vstr_wrapper_fprintf
102 #define sprintf vstr_wrapper_sprintf
103 #define snprintf vstr_wrapper_snprintf
104 #define asprintf vstr_wrapper_asprintf
106 #define vprintf vstr_wrapper_vprintf
107 #define vfprintf vstr_wrapper_vfprintf
108 #define vsprintf vstr_wrapper_vsprintf
109 #define vsnprintf vstr_wrapper_vsnprintf
110 #define vasprintf vstr_wrapper_vasprintf
113 * Data to pass to a printf hook.
115 struct printf_hook_data_t
{
118 * Base to append printf to
123 * Position in base to write to
129 * Helper macro to be used in printf hook callbacks.
131 #define print_in_hook(data, fmt, ...) ({\
132 int _written = vstr_add_fmt(data->base, data->pos, fmt, ##__VA_ARGS__);\
133 data->pos += _written;\
140 * Callback function type for printf hooks.
142 * @param data hook data, to pass to print_in_hook()
143 * @param spec format specifier
144 * @param args arguments array
145 * @return number of characters written
147 typedef int (*printf_hook_function_t
)(printf_hook_data_t
*data
,
148 printf_hook_spec_t
*spec
,
149 const void *const *args
);
152 * Properties of the format specifier
154 struct printf_hook_spec_t
{
156 * TRUE if a '#' was used in the format specifier
161 * TRUE if a '-' was used in the format specifier
166 * The width as given in the format specifier.
172 * Printf handler management.
174 struct printf_hook_t
{
177 * Register a printf handler.
179 * @param spec printf hook format character
180 * @param hook hook function
181 * @param ... list of PRINTF_HOOK_ARGTYPE_*, MUST end with PRINTF_HOOK_ARGTYPE_END
183 void (*add_handler
)(printf_hook_t
*this, char spec
,
184 printf_hook_function_t hook
, ...);
187 * Destroy a printf_hook instance.
189 void (*destroy
)(printf_hook_t
*this);
193 * Create a printf_hook instance.
195 printf_hook_t
*printf_hook_create();
197 #endif /** PRINTF_HOOK_H_ @}*/