5520de154b59875032b5574aa9ee723292915951
[strongswan.git] / src / libstrongswan / utils / printf_hook / 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 utils
20 */
21
22 #ifndef PRINTF_HOOK_H_
23 #define PRINTF_HOOK_H_
24
25 #include <stdlib.h>
26
27 typedef struct printf_hook_t printf_hook_t;
28 typedef struct printf_hook_spec_t printf_hook_spec_t;
29 typedef struct printf_hook_data_t printf_hook_data_t;
30 typedef enum printf_hook_argtype_t printf_hook_argtype_t;
31
32 #if defined(USE_VSTR)
33 # include "printf_hook_vstr.h"
34 #endif
35
36 /**
37 * Argument types to pass to printf hook.
38 */
39 enum printf_hook_argtype_t {
40 PRINTF_HOOK_ARGTYPE_END,
41 PRINTF_HOOK_ARGTYPE_INT,
42 PRINTF_HOOK_ARGTYPE_POINTER,
43 };
44
45 /**
46 * Callback function type for printf hooks.
47 *
48 * @param data hook data, to pass to print_in_hook()
49 * @param spec format specifier
50 * @param args arguments array
51 * @return number of characters written
52 */
53 typedef int (*printf_hook_function_t)(printf_hook_data_t *data,
54 printf_hook_spec_t *spec,
55 const void *const *args);
56
57 /**
58 * Properties of the format specifier
59 */
60 struct printf_hook_spec_t {
61
62 /**
63 * TRUE if a '#' was used in the format specifier
64 */
65 int hash;
66
67 /**
68 * TRUE if a '-' was used in the format specifier
69 */
70 int minus;
71
72 /**
73 * TRUE if a '+' was used in the format specifier
74 */
75 int plus;
76
77 /**
78 * The width as given in the format specifier.
79 */
80 int width;
81 };
82
83 /**
84 * Printf handler management.
85 */
86 struct printf_hook_t {
87
88 /**
89 * Register a printf handler.
90 *
91 * @param spec printf hook format character
92 * @param hook hook function
93 * @param ... list of PRINTF_HOOK_ARGTYPE_*, MUST end with PRINTF_HOOK_ARGTYPE_END
94 */
95 void (*add_handler)(printf_hook_t *this, char spec,
96 printf_hook_function_t hook, ...);
97
98 /**
99 * Destroy a printf_hook instance.
100 */
101 void (*destroy)(printf_hook_t *this);
102 };
103
104 /**
105 * Create a printf_hook instance.
106 */
107 printf_hook_t *printf_hook_create();
108
109 /**
110 * Print with format string within a printf hook.
111 *
112 * @param data hook data, as passed to printf hook
113 * @param fmt printf format string
114 * @param ... arguments to format string
115 * @return number of characters written
116 */
117 size_t print_in_hook(printf_hook_data_t *data, char *fmt, ...);
118
119 #endif /** PRINTF_HOOK_H_ @}*/