configure libimcv debug output via strongswan.conf
[strongswan.git] / src / libimcv / imcv.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imcv.h"
16
17 #include "utils.h"
18 #include <debug.h>
19
20 #include <syslog.h>
21
22 #define IMCV_DEBUG_LEVEL 1
23
24 /**
25 * Reference count for IMC/IMV instances
26 */
27 refcount_t ref = 0;
28
29 /**
30 * Global configuration of imcv dbg function
31 */
32 static int imcv_debug_level;
33 static bool imcv_stderr_quiet;
34
35 /**
36 * imvc dbg function
37 */
38 static void imcv_dbg(debug_t group, level_t level, char *fmt, ...)
39 {
40 int priority = LOG_INFO;
41 char buffer[8192];
42 char *current = buffer, *next;
43 va_list args;
44
45 if (level <= imcv_debug_level)
46 {
47 if (!imcv_stderr_quiet)
48 {
49 va_start(args, fmt);
50 vfprintf(stderr, fmt, args);
51 fprintf(stderr, "\n");
52 va_end(args);
53 }
54
55 /* write in memory buffer first */
56 va_start(args, fmt);
57 vsnprintf(buffer, sizeof(buffer), fmt, args);
58 va_end(args);
59
60 /* do a syslog with every line */
61 while (current)
62 {
63 next = strchr(current, '\n');
64 if (next)
65 {
66 *(next++) = '\0';
67 }
68 syslog(priority, "%s\n", current);
69 current = next;
70 }
71 }
72 }
73
74 /**
75 * Described in header.
76 */
77 bool libimcv_init(void)
78 {
79 /* initialize libstrongswan library only once */
80 if (lib)
81 {
82 /* did main program initialize libstrongswan? */
83 if (ref == 0)
84 {
85 ref_get(&ref);
86 }
87 }
88 else
89 {
90 /* we are the first to initialize libstrongswan */
91 if (!library_init(NULL))
92 {
93 return FALSE;
94 }
95
96 if (!lib->plugins->load(lib->plugins, NULL, "random"))
97 {
98 library_deinit();
99 return FALSE;
100 }
101
102 /* set the debug level and stderr output */
103 imcv_debug_level = lib->settings->get_int(lib->settings,
104 "libimcv.debug_level", IMCV_DEBUG_LEVEL);
105 imcv_stderr_quiet = lib->settings->get_int(lib->settings,
106 "libimcv.debug_level", FALSE);
107
108 /* activate the imcv debugging hook */
109 dbg = imcv_dbg;
110 openlog("imcv", 0, LOG_DAEMON);
111
112 DBG1(DBG_LIB, "libimcv initialized");
113 }
114 ref_get(&ref);
115
116 return TRUE;
117 }
118
119 /**
120 * Described in header.
121 */
122 void libimcv_deinit(void)
123 {
124 if (ref_put(&ref))
125 {
126 DBG1(DBG_LIB, "libimcv terminated");
127 library_deinit();
128 }
129 }
130
131