5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
38 static const char *program_exec;
39 static const char *program_path;
43 * @format: format string
44 * @Varargs: list of arguments
46 * Output general information
48 void connman_info(const char *format, ...)
54 vsyslog(LOG_INFO, format, ap);
61 * @format: format string
62 * @Varargs: list of arguments
64 * Output warning messages
66 void connman_warn(const char *format, ...)
72 vsyslog(LOG_WARNING, format, ap);
79 * @format: format string
80 * @varargs: list of arguments
82 * Output error messages
84 void connman_error(const char *format, ...)
90 vsyslog(LOG_ERR, format, ap);
97 * @format: format string
98 * @varargs: list of arguments
100 * Output debug message
102 void connman_debug(const char *format, ...)
106 va_start(ap, format);
108 vsyslog(LOG_DEBUG, format, ap);
113 static void print_backtrace(unsigned int offset)
118 int outfd[2], infd[2];
122 if (program_exec == NULL)
125 pathlen = strlen(program_path);
127 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
134 if (pipe(infd) < 0) {
153 dup2(outfd[0], STDIN_FILENO);
154 dup2(infd[1], STDOUT_FILENO);
156 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
164 connman_error("++++++++ backtrace ++++++++");
166 for (i = offset; i < n_ptrs - 1; i++) {
168 char addr[20], buf[PATH_MAX * 2];
172 dladdr(frames[i], &info);
174 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
178 written = write(outfd[1], addr, len);
182 len = read(infd[0], buf, sizeof(buf));
188 pos = strchr(buf, '\n');
191 if (strcmp(buf, "??") == 0) {
192 connman_error("#%-2u %p in %s", i - offset,
193 frames[i], info.dli_fname);
197 ptr = strchr(pos, '\n');
200 if (strncmp(pos, program_path, pathlen) == 0)
203 connman_error("#%-2u %p in %s() at %s", i - offset,
204 frames[i], buf, pos);
207 connman_error("+++++++++++++++++++++++++++");
215 static void signal_handler(int signo)
217 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
224 static void signal_setup(sighandler_t handler)
230 sa.sa_handler = handler;
233 sigaction(SIGBUS, &sa, NULL);
234 sigaction(SIGILL, &sa, NULL);
235 sigaction(SIGFPE, &sa, NULL);
236 sigaction(SIGSEGV, &sa, NULL);
237 sigaction(SIGABRT, &sa, NULL);
238 sigaction(SIGPIPE, &sa, NULL);
241 extern struct connman_debug_desc __start___debug[];
242 extern struct connman_debug_desc __stop___debug[];
244 void __connman_debug_list_available(DBusMessageIter *iter, void *user_data)
246 struct connman_debug_desc *desc;
248 for (desc = __start___debug; desc < __stop___debug; desc++) {
249 if ((desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) &&
251 dbus_message_iter_append_basic(iter,
252 DBUS_TYPE_STRING, &desc->name);
256 static gchar **enabled = NULL;
258 void __connman_debug_list_enabled(DBusMessageIter *iter, void *user_data)
265 for (i = 0; enabled[i] != NULL; i++)
266 dbus_message_iter_append_basic(iter,
267 DBUS_TYPE_STRING, &enabled[i]);
270 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
277 for (i = 0; enabled[i] != NULL; i++) {
278 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
281 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
289 void __connman_log_enable(struct connman_debug_desc *start,
290 struct connman_debug_desc *stop)
292 struct connman_debug_desc *desc;
293 const char *name = NULL, *file = NULL;
295 if (start == NULL || stop == NULL)
298 for (desc = start; desc < stop; desc++) {
299 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
305 if (file != NULL || name != NULL) {
306 if (g_strcmp0(desc->file, file) == 0) {
307 if (desc->name == NULL)
313 if (is_enabled(desc) == TRUE)
314 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
318 int __connman_log_init(const char *program, const char *debug,
319 connman_bool_t detach)
321 static char path[PATH_MAX];
322 int option = LOG_NDELAY | LOG_PID;
324 program_exec = program;
325 program_path = getcwd(path, sizeof(path));
328 enabled = g_strsplit_set(debug, ":, ", 0);
330 __connman_log_enable(__start___debug, __stop___debug);
333 option |= LOG_PERROR;
335 signal_setup(signal_handler);
337 openlog(basename(program), option, LOG_DAEMON);
339 syslog(LOG_INFO, "Connection Manager version %s", VERSION);
344 void __connman_log_cleanup(void)
346 syslog(LOG_INFO, "Exit");
350 signal_setup(SIG_DFL);