5 * Copyright (C) 2007-2012 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 static gchar **enabled = NULL;
246 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
253 for (i = 0; enabled[i] != NULL; i++) {
254 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
257 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
265 void __connman_log_enable(struct connman_debug_desc *start,
266 struct connman_debug_desc *stop)
268 struct connman_debug_desc *desc;
269 const char *name = NULL, *file = NULL;
271 if (start == NULL || stop == NULL)
274 for (desc = start; desc < stop; desc++) {
275 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
281 if (file != NULL || name != NULL) {
282 if (g_strcmp0(desc->file, file) == 0) {
283 if (desc->name == NULL)
289 if (is_enabled(desc) == TRUE)
290 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
294 int __connman_log_init(const char *program, const char *debug,
295 connman_bool_t detach)
297 static char path[PATH_MAX];
298 int option = LOG_NDELAY | LOG_PID;
300 program_exec = program;
301 program_path = getcwd(path, sizeof(path));
304 enabled = g_strsplit_set(debug, ":, ", 0);
306 __connman_log_enable(__start___debug, __stop___debug);
309 option |= LOG_PERROR;
311 signal_setup(signal_handler);
313 openlog(basename(program), option, LOG_DAEMON);
315 syslog(LOG_INFO, "Connection Manager version %s", VERSION);
320 void __connman_log_cleanup(void)
322 syslog(LOG_INFO, "Exit");
326 signal_setup(SIG_DFL);