3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2011 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 ofono_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 ofono_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 ofono_error(const char *format, ...)
90 vsyslog(LOG_ERR, format, ap);
97 * @format: format string
98 * @varargs: list of arguments
100 * Output debug message
102 * The actual output of the debug message is controlled via a command line
103 * switch. If not enabled, these messages will be ignored.
105 void ofono_debug(const char *format, ...)
109 va_start(ap, format);
111 vsyslog(LOG_DEBUG, format, ap);
116 static void print_backtrace(unsigned int offset)
121 int outfd[2], infd[2];
125 if (program_exec == NULL)
128 pathlen = strlen(program_path);
130 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
137 if (pipe(infd) < 0) {
156 dup2(outfd[0], STDIN_FILENO);
157 dup2(infd[1], STDOUT_FILENO);
159 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
167 ofono_error("++++++++ backtrace ++++++++");
169 for (i = offset; i < n_ptrs - 1; i++) {
171 char addr[20], buf[PATH_MAX * 2];
175 dladdr(frames[i], &info);
177 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
181 written = write(outfd[1], addr, len);
185 len = read(infd[0], buf, sizeof(buf));
191 pos = strchr(buf, '\n');
194 if (strcmp(buf, "??") == 0) {
195 ofono_error("#%-2u %p in %s", i - offset,
196 frames[i], info.dli_fname);
200 ptr = strchr(pos, '\n');
203 if (strncmp(pos, program_path, pathlen) == 0)
206 ofono_error("#%-2u %p in %s() at %s", i - offset,
207 frames[i], buf, pos);
210 ofono_error("+++++++++++++++++++++++++++");
218 static void signal_handler(int signo)
220 ofono_error("Aborting (signal %d) [%s]", signo, program_exec);
227 static void signal_setup(sighandler_t handler)
233 sa.sa_handler = handler;
236 sigaction(SIGBUS, &sa, NULL);
237 sigaction(SIGILL, &sa, NULL);
238 sigaction(SIGFPE, &sa, NULL);
239 sigaction(SIGSEGV, &sa, NULL);
240 sigaction(SIGABRT, &sa, NULL);
241 sigaction(SIGPIPE, &sa, NULL);
244 extern struct ofono_debug_desc __start___debug[];
245 extern struct ofono_debug_desc __stop___debug[];
247 static gchar **enabled = NULL;
249 static ofono_bool_t is_enabled(struct ofono_debug_desc *desc)
256 for (i = 0; enabled[i] != NULL; i++) {
257 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
260 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
268 void __ofono_log_enable(struct ofono_debug_desc *start,
269 struct ofono_debug_desc *stop)
271 struct ofono_debug_desc *desc;
272 const char *name = NULL, *file = NULL;
274 if (start == NULL || stop == NULL)
277 for (desc = start; desc < stop; desc++) {
278 if (file != NULL || name != NULL) {
279 if (g_strcmp0(desc->file, file) == 0) {
280 if (desc->name == NULL)
286 if (is_enabled(desc) == TRUE)
287 desc->flags |= OFONO_DEBUG_FLAG_PRINT;
291 int __ofono_log_init(const char *program, const char *debug,
294 static char path[PATH_MAX];
295 int option = LOG_NDELAY | LOG_PID;
297 program_exec = program;
298 program_path = getcwd(path, sizeof(path));
301 enabled = g_strsplit_set(debug, ":, ", 0);
303 __ofono_log_enable(__start___debug, __stop___debug);
306 option |= LOG_PERROR;
308 signal_setup(signal_handler);
310 openlog(basename(program), option, LOG_DAEMON);
312 syslog(LOG_INFO, "oFono version %s", VERSION);
317 void __ofono_log_cleanup(void)
319 syslog(LOG_INFO, "Exit");
323 signal_setup(SIG_DFL);