ipconfig: Add Function to Stringify ipconfig Type
[framework/connectivity/connman.git] / src / log.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 #define _GNU_SOURCE
27 #include <stdarg.h>
28 #include <syslog.h>
29 #include <stdlib.h>
30 #include <execinfo.h>
31 #include <dlfcn.h>
32
33 #include "connman.h"
34
35 /**
36  * connman_info:
37  * @format: format string
38  * @Varargs: list of arguments
39  *
40  * Output general information
41  */
42 void connman_info(const char *format, ...)
43 {
44         va_list ap;
45
46         va_start(ap, format);
47
48         vsyslog(LOG_INFO, format, ap);
49
50         va_end(ap);
51 }
52
53 /**
54  * connman_warn:
55  * @format: format string
56  * @Varargs: list of arguments
57  *
58  * Output warning messages
59  */
60 void connman_warn(const char *format, ...)
61 {
62         va_list ap;
63
64         va_start(ap, format);
65
66         vsyslog(LOG_WARNING, format, ap);
67
68         va_end(ap);
69 }
70
71 /**
72  * connman_error:
73  * @format: format string
74  * @varargs: list of arguments
75  *
76  * Output error messages
77  */
78 void connman_error(const char *format, ...)
79 {
80         va_list ap;
81
82         va_start(ap, format);
83
84         vsyslog(LOG_ERR, format, ap);
85
86         va_end(ap);
87 }
88
89 /**
90  * connman_debug:
91  * @format: format string
92  * @varargs: list of arguments
93  *
94  * Output debug message
95  */
96 void connman_debug(const char *format, ...)
97 {
98         va_list ap;
99
100         va_start(ap, format);
101
102         vsyslog(LOG_DEBUG, format, ap);
103
104         va_end(ap);
105 }
106
107 static void signal_handler(int signo)
108 {
109         void *frames[64];
110         char **symbols;
111         size_t n_ptrs;
112         unsigned int i;
113
114         n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
115         symbols = backtrace_symbols(frames, n_ptrs);
116         if (symbols == NULL) {
117                 connman_error("No backtrace symbols");
118                 exit(1);
119         }
120
121         connman_error("Aborting (signal %d)", signo);
122         connman_error("++++++++ backtrace ++++++++");
123
124         for (i = 1; i < n_ptrs; i++)
125                 connman_error("[%d]: %s", i - 1, symbols[i]);
126
127         connman_error("+++++++++++++++++++++++++++");
128
129         g_free(symbols);
130         exit(1);
131 }
132
133 static void signal_setup(sighandler_t handler)
134 {
135         struct sigaction sa;
136         sigset_t mask;
137
138         sigemptyset(&mask);
139         sa.sa_handler = handler;
140         sa.sa_mask = mask;
141         sa.sa_flags = 0;
142         sigaction(SIGBUS, &sa, NULL);
143         sigaction(SIGILL, &sa, NULL);
144         sigaction(SIGFPE, &sa, NULL);
145         sigaction(SIGSEGV, &sa, NULL);
146         sigaction(SIGABRT, &sa, NULL);
147         sigaction(SIGPIPE, &sa, NULL);
148 }
149
150 extern struct connman_debug_desc __start___debug[];
151 extern struct connman_debug_desc __stop___debug[];
152
153 void __connman_debug_list_available(DBusMessageIter *iter, void *user_data)
154 {
155         struct connman_debug_desc *desc;
156
157         for (desc = __start___debug; desc < __stop___debug; desc++) {
158                 if ((desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) &&
159                                                 desc->name != NULL)
160                         dbus_message_iter_append_basic(iter,
161                                         DBUS_TYPE_STRING, &desc->name);
162         }
163 }
164
165 static gchar **enabled = NULL;
166
167 void __connman_debug_list_enabled(DBusMessageIter *iter, void *user_data)
168 {
169         int i;
170
171         if (enabled == NULL)
172                 return;
173
174         for (i = 0; enabled[i] != NULL; i++)
175                 dbus_message_iter_append_basic(iter,
176                                         DBUS_TYPE_STRING, &enabled[i]);
177 }
178
179 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
180 {
181         int i;
182
183         if (enabled == NULL)
184                 return FALSE;
185
186         for (i = 0; enabled[i] != NULL; i++) {
187                 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
188                                                         desc->name) == TRUE)
189                         return TRUE;
190                 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
191                                                         desc->file) == TRUE)
192                         return TRUE;
193         }
194
195         return FALSE;
196 }
197
198 int __connman_log_init(const char *debug, connman_bool_t detach)
199 {
200         int option = LOG_NDELAY | LOG_PID;
201         struct connman_debug_desc *desc;
202         const char *name = NULL, *file = NULL;
203
204         if (debug != NULL)
205                 enabled = g_strsplit_set(debug, ":, ", 0);
206
207         for (desc = __start___debug; desc < __stop___debug; desc++) {
208                 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
209                         file = desc->file;
210                         name = desc->name;
211                         continue;
212                 }
213
214                 if (file != NULL || name != NULL) {
215                         if (g_strcmp0(desc->file, file) == 0) {
216                                 if (desc->name == NULL)
217                                         desc->name = name;
218                         } else
219                                 file = NULL;
220                 }
221
222                 if (is_enabled(desc) == TRUE)
223                         desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
224         }
225
226         if (detach == FALSE)
227                 option |= LOG_PERROR;
228
229         signal_setup(signal_handler);
230
231         openlog("connmand", option, LOG_DAEMON);
232
233         syslog(LOG_INFO, "Connection Manager version %s", VERSION);
234
235         return 0;
236 }
237
238 void __connman_log_cleanup(void)
239 {
240         syslog(LOG_INFO, "Exit");
241
242         closelog();
243
244         signal_setup(SIG_DFL);
245
246         g_strfreev(enabled);
247 }