updated Doxyfile
[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 * $Id$
17 */
18
19 /**
20 * @defgroup printf_hook printf_hook
21 * @{ @ingroup libstrongswan
22 */
23
24 #ifndef PRINTF_HOOK_H_
25 #define PRINTF_HOOK_H_
26
27 typedef struct printf_hook_t printf_hook_t;
28 typedef struct printf_hook_spec_t printf_hook_spec_t;
29 typedef enum printf_hook_argtype_t printf_hook_argtype_t;
30
31 #ifdef HAVE_PRINTF_HOOKS
32
33 #include <printf.h>
34
35 enum printf_hook_argtype_t {
36 PRINTF_HOOK_ARGTYPE_END = PA_LAST,
37 PRINTF_HOOK_ARGTYPE_INT = PA_INT,
38 PRINTF_HOOK_ARGTYPE_POINTER = PA_POINTER,
39 };
40
41 #else
42
43 #include <vstr.h>
44
45 enum printf_hook_argtype_t {
46 PRINTF_HOOK_ARGTYPE_END = VSTR_TYPE_FMT_END,
47 PRINTF_HOOK_ARGTYPE_INT = VSTR_TYPE_FMT_INT,
48 PRINTF_HOOK_ARGTYPE_POINTER = VSTR_TYPE_FMT_PTR_VOID,
49 };
50
51 /**
52 * Redefining printf and alike
53 */
54 #include <stdio.h>
55 #include <stdarg.h>
56
57 int vstr_wrapper_printf(const char *format, ...);
58 int vstr_wrapper_fprintf(FILE *stream, const char *format, ...);
59 int vstr_wrapper_sprintf(char *str, const char *format, ...);
60 int vstr_wrapper_snprintf(char *str, size_t size, const char *format, ...);
61
62 int vstr_wrapper_vprintf(const char *format, va_list ap);
63 int vstr_wrapper_vfprintf(FILE *stream, const char *format, va_list ap);
64 int vstr_wrapper_vsprintf(char *str, const char *format, va_list ap);
65 int vstr_wrapper_vsnprintf(char *str, size_t size, const char *format, va_list ap);
66
67 #define printf vstr_wrapper_printf
68 #define fprintf vstr_wrapper_fprintf
69 #define sprintf vstr_wrapper_sprintf
70 #define snprintf vstr_wrapper_snprintf
71
72 #define vprintf vstr_wrapper_vprintf
73 #define vfprintf vstr_wrapper_vfprintf
74 #define vsprintf vstr_wrapper_vsprintf
75 #define vsnprintf vstr_wrapper_vsnprintf
76
77 #endif
78
79 /**
80 * Callback function type for printf hooks.
81 *
82 * @param dst destination buffer
83 * @param len length of the buffer
84 * @param spec format specifier
85 * @param args arguments array
86 * @return number of characters written
87 */
88 typedef int (*printf_hook_function_t)(char *dst, size_t len,
89 printf_hook_spec_t *spec,
90 const void *const *args);
91
92 /**
93 * Helper macro to be used in printf hook callbacks.
94 * buf and buflen get modified.
95 */
96 #define print_in_hook(buf, buflen, fmt, ...) ({\
97 int _written = snprintf(buf, buflen, fmt, ##__VA_ARGS__);\
98 if (_written < 0 || _written >= buflen)\
99 {\
100 _written = buflen - 1;\
101 }\
102 buf += _written;\
103 buflen -= _written;\
104 _written;\
105 })
106
107 /**
108 * Properties of the format specifier
109 */
110 struct printf_hook_spec_t {
111 /**
112 * TRUE if a '#' was used in the format specifier
113 */
114 int hash;
115
116 /**
117 * TRUE if a '-' was used in the format specifier
118 */
119 int minus;
120
121 /**
122 * The width as given in the format specifier.
123 */
124 int width;
125 };
126
127 /**
128 * Printf handler management.
129 */
130 struct printf_hook_t {
131
132 /**
133 * Register a printf handler.
134 *
135 * @param spec printf hook format character
136 * @param hook hook function
137 * @param ... list of PRINTF_HOOK_ARGTYPE_*, MUST end with PRINTF_HOOK_ARGTYPE_END
138 */
139 void (*add_handler)(printf_hook_t *this, char spec,
140 printf_hook_function_t hook, ...);
141
142 /**
143 * Destroy a printf_hook instance.
144 */
145 void (*destroy)(printf_hook_t *this);
146 };
147
148 /**
149 * Create a printf_hook instance.
150 */
151 printf_hook_t *printf_hook_create();
152
153 #endif /** PRINTF_HOOK_H_ @}*/