Add support for dynamic debug framework
[framework/connectivity/connman.git] / src / log.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdarg.h>
27 #include <syslog.h>
28
29 #include "connman.h"
30
31 /**
32  * connman_info:
33  * @format: format string
34  * @Varargs: list of arguments
35  *
36  * Output general information
37  */
38 void connman_info(const char *format, ...)
39 {
40         va_list ap;
41
42         va_start(ap, format);
43
44         vsyslog(LOG_INFO, format, ap);
45
46         va_end(ap);
47 }
48
49 /**
50  * connman_warn:
51  * @format: format string
52  * @Varargs: list of arguments
53  *
54  * Output warning messages
55  */
56 void connman_warn(const char *format, ...)
57 {
58         va_list ap;
59
60         va_start(ap, format);
61
62         vsyslog(LOG_WARNING, format, ap);
63
64         va_end(ap);
65 }
66
67 /**
68  * connman_error:
69  * @format: format string
70  * @varargs: list of arguments
71  *
72  * Output error messages
73  */
74 void connman_error(const char *format, ...)
75 {
76         va_list ap;
77
78         va_start(ap, format);
79
80         vsyslog(LOG_ERR, format, ap);
81
82         va_end(ap);
83 }
84
85 /**
86  * connman_debug:
87  * @format: format string
88  * @varargs: list of arguments
89  *
90  * Output debug message
91  */
92 void connman_debug(const char *format, ...)
93 {
94         va_list ap;
95
96         va_start(ap, format);
97
98         vsyslog(LOG_DEBUG, format, ap);
99
100         va_end(ap);
101 }
102
103 extern struct connman_debug_desc __start___debug[];
104 extern struct connman_debug_desc __stop___debug[];
105
106 void __connman_debug_list_available(DBusMessageIter *iter)
107 {
108         struct connman_debug_desc *desc;
109
110         for (desc = __start___debug; desc < __stop___debug; desc++) {
111                 if ((desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) &&
112                                                 desc->name != NULL)
113                         dbus_message_iter_append_basic(iter,
114                                         DBUS_TYPE_STRING, &desc->name);
115         }
116 }
117
118 static gchar **enabled = NULL;
119
120 void __connman_debug_list_enabled(DBusMessageIter *iter)
121 {
122         int i;
123
124         if (enabled == NULL)
125                 return;
126
127         for (i = 0; enabled[i] != NULL; i++)
128                 dbus_message_iter_append_basic(iter,
129                                         DBUS_TYPE_STRING, &enabled[i]);
130 }
131
132 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
133 {
134         int i;
135
136         if (enabled == NULL)
137                 return FALSE;
138
139         for (i = 0; enabled[i] != NULL; i++) {
140                 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
141                                                         desc->name) == TRUE)
142                         return TRUE;
143                 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
144                                                         desc->file) == TRUE)
145                         return TRUE;
146         }
147
148         return FALSE;
149 }
150
151 int __connman_log_init(const char *debug, connman_bool_t detach)
152 {
153         int option = LOG_NDELAY | LOG_PID;
154         struct connman_debug_desc *desc;
155         const char *name = NULL, *file = NULL;
156
157         if (debug != NULL)
158                 enabled = g_strsplit_set(debug, ":, ", 0);
159
160         for (desc = __start___debug; desc < __stop___debug; desc++) {
161                 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
162                         file = desc->file;
163                         name = desc->name;
164                         continue;
165                 }
166
167                 if (file != NULL || name != NULL) {
168                         if (g_strcmp0(desc->file, file) == 0) {
169                                 if (desc->name == NULL)
170                                         desc->name = name;
171                         } else
172                                 file = NULL;
173                 }
174
175                 if (is_enabled(desc) == TRUE)
176                         desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
177         }
178
179         if (detach == FALSE)
180                 option |= LOG_PERROR;
181
182         openlog("connmand", option, LOG_DAEMON);
183
184         syslog(LOG_INFO, "Connection Manager version %s", VERSION);
185
186         return 0;
187 }
188
189 void __connman_log_cleanup(void)
190 {
191         syslog(LOG_INFO, "Exit");
192
193         closelog();
194
195         g_strfreev(enabled);
196 }