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 #if !defined TIZEN_EXT
114 static void print_backtrace(unsigned int offset)
119 int outfd[2], infd[2];
123 if (program_exec == NULL)
126 pathlen = strlen(program_path);
128 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
135 if (pipe(infd) < 0) {
154 dup2(outfd[0], STDIN_FILENO);
155 dup2(infd[1], STDOUT_FILENO);
157 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
165 connman_error("++++++++ backtrace ++++++++");
167 for (i = offset; i < n_ptrs - 1; i++) {
169 char addr[20], buf[PATH_MAX * 2];
173 dladdr(frames[i], &info);
175 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
179 written = write(outfd[1], addr, len);
183 len = read(infd[0], buf, sizeof(buf));
189 pos = strchr(buf, '\n');
192 if (strcmp(buf, "??") == 0) {
193 connman_error("#%-2u %p in %s", i - offset,
194 frames[i], info.dli_fname);
198 ptr = strchr(pos, '\n');
201 if (strncmp(pos, program_path, pathlen) == 0)
204 connman_error("#%-2u %p in %s() at %s", i - offset,
205 frames[i], buf, pos);
208 connman_error("+++++++++++++++++++++++++++");
216 static void signal_handler(int signo)
218 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
225 static void signal_setup(sighandler_t handler)
231 sa.sa_handler = handler;
234 sigaction(SIGBUS, &sa, NULL);
235 sigaction(SIGILL, &sa, NULL);
236 sigaction(SIGFPE, &sa, NULL);
237 sigaction(SIGSEGV, &sa, NULL);
238 sigaction(SIGABRT, &sa, NULL);
239 sigaction(SIGPIPE, &sa, NULL);
243 extern struct connman_debug_desc __start___debug[];
244 extern struct connman_debug_desc __stop___debug[];
246 static gchar **enabled = NULL;
248 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
255 for (i = 0; enabled[i] != NULL; i++) {
256 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
259 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
267 void __connman_log_enable(struct connman_debug_desc *start,
268 struct connman_debug_desc *stop)
270 struct connman_debug_desc *desc;
271 const char *name = NULL, *file = NULL;
273 if (start == NULL || stop == NULL)
276 for (desc = start; desc < stop; desc++) {
277 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
283 if (file != NULL || name != NULL) {
284 if (g_strcmp0(desc->file, file) == 0) {
285 if (desc->name == NULL)
291 if (is_enabled(desc) == TRUE)
292 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
296 int __connman_log_init(const char *program, const char *debug,
297 connman_bool_t detach)
299 static char path[PATH_MAX];
300 int option = LOG_NDELAY | LOG_PID;
302 program_exec = program;
303 program_path = getcwd(path, sizeof(path));
306 enabled = g_strsplit_set(debug, ":, ", 0);
308 __connman_log_enable(__start___debug, __stop___debug);
311 option |= LOG_PERROR;
313 #if !defined TIZEN_EXT
314 signal_setup(signal_handler);
317 openlog(basename(program), option, LOG_DAEMON);
319 syslog(LOG_INFO, "Connection Manager version %s", VERSION);
324 void __connman_log_cleanup(void)
326 syslog(LOG_INFO, "Exit");
330 #if !defined TIZEN_EXT
331 signal_setup(SIG_DFL);