introduced new logging subsystem using bus:
[strongswan.git] / src / libstrongswan / definitions.c
1 /**
2 * @file definitions.c
3 *
4 * @brief General purpose definitions and macros.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <printf.h>
25 #include <stdio.h>
26
27 #include "definitions.h"
28
29 /*
30 * Described in header
31 */
32 static char *enum_name(enum_name_t *e, long val)
33 {
34 do
35 {
36 if (val >= e->first && val <= e->last)
37 {
38 return e->names[val - e->first];
39 }
40 }
41 while ((e = e->next));
42 return NULL;
43 }
44
45
46 /**
47 * output handler in printf() for enum names
48 */
49 static int print_enum(FILE *stream, const struct printf_info *info,
50 const void *const *args)
51 {
52 enum_name_t *ed = *((void**)(args[0]));
53 long val = *((size_t*)(args[1]));
54 char *name;
55
56 name = enum_name(ed, val);
57 if (name == NULL)
58 {
59 return fprintf(stream, "(unknown enum value: %ld)", val);
60 }
61 return fprintf(stream, "%s", name);
62 }
63
64 /**
65 * arginfo handler in printf() for enum names
66 */
67 static int print_enum_arginfo(const struct printf_info *info, size_t n, int *argtypes)
68 {
69 if (n > 1)
70 {
71 /* enum_names ptr */
72 argtypes[0] = PA_POINTER;
73 /* value */
74 argtypes[1] = PA_INT;
75 }
76 return 2;
77 }
78
79 /**
80 * register printf() handlers for enum names
81 */
82 static void __attribute__ ((constructor))print_register()
83 {
84 register_printf_function(ENUM_PRINTF_SPEC, print_enum, print_enum_arginfo);
85 }