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;
44 #define LOG_FILE_PATH "/var/log/connman.log"
45 #define MAX_LOG_SIZE 2 * 1024 * 1024
46 #define MAX_LOG_COUNT 9
48 #define openlog __connman_log_open
49 #define closelog __connman_log_close
50 #define vsyslog __connman_log
51 #define syslog __connman_log_s
53 static FILE *log_file = NULL;
55 void __connman_log_open(const char *ident, int option, int facility)
58 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
61 void __connman_log_close(void)
67 static void __connman_log_update_file_revision(int rev)
70 char *log_file = NULL;
71 char *next_log_file = NULL;
73 next_log_rev = rev + 1;
75 log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
76 next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
78 if (next_log_rev >= MAX_LOG_COUNT)
79 remove(next_log_file);
81 if (access(next_log_file, F_OK) == 0)
82 __connman_log_update_file_revision(next_log_rev);
84 if (rename(log_file, next_log_file) != 0)
88 g_free(next_log_file);
91 static void __connman_log_make_backup(void)
96 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
98 if (access(backup, F_OK) == 0)
99 __connman_log_update_file_revision(rev);
101 if (rename(LOG_FILE_PATH, backup) != 0)
102 remove(LOG_FILE_PATH);
107 static void __connman_log_get_local_time(char *strtime, const int size)
110 struct tm *local_ptm;
114 local_ptm = localtime(&buf);
116 strftime(strtime, size, "%D %H:%M:%S", local_ptm);
119 void __connman_log(const int log_priority, const char *format, va_list ap)
126 if (log_file == NULL)
127 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
129 if (log_file == NULL)
132 fstat(fileno(log_file), &buf);
133 log_size = buf.st_size;
135 if (log_size >= MAX_LOG_SIZE) {
139 __connman_log_make_backup();
141 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
143 if (log_file == NULL)
147 __connman_log_get_local_time(strtime, sizeof(strtime));
149 if (vsnprintf(str, sizeof(str), format, ap) > 0)
150 fprintf(log_file, "%s %s\n", strtime, str);
153 void __connman_log_s(int log_priority, const char *format, ...)
157 va_start(ap, format);
159 vsyslog(LOG_DEBUG, format, ap);
167 * @format: format string
168 * @Varargs: list of arguments
170 * Output general information
172 void connman_info(const char *format, ...)
176 va_start(ap, format);
178 vsyslog(LOG_INFO, format, ap);
185 * @format: format string
186 * @Varargs: list of arguments
188 * Output warning messages
190 void connman_warn(const char *format, ...)
194 va_start(ap, format);
196 vsyslog(LOG_WARNING, format, ap);
203 * @format: format string
204 * @varargs: list of arguments
206 * Output error messages
208 void connman_error(const char *format, ...)
212 va_start(ap, format);
214 vsyslog(LOG_ERR, format, ap);
221 * @format: format string
222 * @varargs: list of arguments
224 * Output debug message
226 void connman_debug(const char *format, ...)
230 va_start(ap, format);
232 vsyslog(LOG_DEBUG, format, ap);
237 #if !defined TIZEN_EXT
238 static void print_backtrace(unsigned int offset)
243 int outfd[2], infd[2];
247 if (program_exec == NULL)
250 pathlen = strlen(program_path);
252 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
259 if (pipe(infd) < 0) {
278 dup2(outfd[0], STDIN_FILENO);
279 dup2(infd[1], STDOUT_FILENO);
281 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
289 connman_error("++++++++ backtrace ++++++++");
291 for (i = offset; i < n_ptrs - 1; i++) {
293 char addr[20], buf[PATH_MAX * 2];
297 dladdr(frames[i], &info);
299 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
303 written = write(outfd[1], addr, len);
307 len = read(infd[0], buf, sizeof(buf));
313 pos = strchr(buf, '\n');
316 if (strcmp(buf, "??") == 0) {
317 connman_error("#%-2u %p in %s", i - offset,
318 frames[i], info.dli_fname);
322 ptr = strchr(pos, '\n');
325 if (strncmp(pos, program_path, pathlen) == 0)
328 connman_error("#%-2u %p in %s() at %s", i - offset,
329 frames[i], buf, pos);
332 connman_error("+++++++++++++++++++++++++++");
340 static void signal_handler(int signo)
342 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
349 static void signal_setup(sighandler_t handler)
355 sa.sa_handler = handler;
358 sigaction(SIGBUS, &sa, NULL);
359 sigaction(SIGILL, &sa, NULL);
360 sigaction(SIGFPE, &sa, NULL);
361 sigaction(SIGSEGV, &sa, NULL);
362 sigaction(SIGABRT, &sa, NULL);
363 sigaction(SIGPIPE, &sa, NULL);
367 extern struct connman_debug_desc __start___debug[];
368 extern struct connman_debug_desc __stop___debug[];
370 void __connman_debug_list_available(DBusMessageIter *iter, void *user_data)
372 struct connman_debug_desc *desc;
374 for (desc = __start___debug; desc < __stop___debug; desc++) {
375 if ((desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) &&
377 dbus_message_iter_append_basic(iter,
378 DBUS_TYPE_STRING, &desc->name);
382 static gchar **enabled = NULL;
384 void __connman_debug_list_enabled(DBusMessageIter *iter, void *user_data)
391 for (i = 0; enabled[i] != NULL; i++)
392 dbus_message_iter_append_basic(iter,
393 DBUS_TYPE_STRING, &enabled[i]);
396 static connman_bool_t is_enabled(struct connman_debug_desc *desc)
403 for (i = 0; enabled[i] != NULL; i++) {
404 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
407 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
415 void __connman_log_enable(struct connman_debug_desc *start,
416 struct connman_debug_desc *stop)
418 struct connman_debug_desc *desc;
419 const char *name = NULL, *file = NULL;
421 if (start == NULL || stop == NULL)
424 for (desc = start; desc < stop; desc++) {
425 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
431 if (file != NULL || name != NULL) {
432 if (g_strcmp0(desc->file, file) == 0) {
433 if (desc->name == NULL)
439 if (is_enabled(desc) == TRUE)
440 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
444 int __connman_log_init(const char *program, const char *debug,
445 connman_bool_t detach)
447 static char path[PATH_MAX];
448 int option = LOG_NDELAY | LOG_PID;
450 program_exec = program;
451 program_path = getcwd(path, sizeof(path));
454 enabled = g_strsplit_set(debug, ":, ", 0);
456 __connman_log_enable(__start___debug, __stop___debug);
459 option |= LOG_PERROR;
461 #if !defined TIZEN_EXT
462 signal_setup(signal_handler);
465 openlog(basename(program), option, LOG_DAEMON);
467 syslog(LOG_INFO, "Connection Manager version %s", VERSION);
472 void __connman_log_cleanup(void)
474 syslog(LOG_INFO, "Exit");
478 #if !defined TIZEN_EXT
479 signal_setup(SIG_DFL);