5 * Copyright (C) 2007-2013 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;
45 #define LOG_FILE_PATH "/opt/usr/data/network/connman.log"
46 #define MAX_LOG_SIZE 1 * 1024 * 1024
47 #define MAX_LOG_COUNT 15
49 #define openlog __connman_log_open
50 #define closelog __connman_log_close
51 #define vsyslog __connman_log
52 #define syslog __connman_log_s
54 static FILE *log_file = NULL;
56 void __connman_log_open(const char *ident, int option, int facility)
59 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
62 void __connman_log_close(void)
68 static void __connman_log_update_file_revision(int rev)
71 char *log_file = NULL;
72 char *next_log_file = NULL;
74 next_log_rev = rev + 1;
76 log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
77 next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
79 if (next_log_rev >= MAX_LOG_COUNT)
80 remove(next_log_file);
82 if (access(next_log_file, F_OK) == 0)
83 __connman_log_update_file_revision(next_log_rev);
85 if (rename(log_file, next_log_file) != 0)
89 g_free(next_log_file);
92 static void __connman_log_make_backup(void)
97 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
99 if (access(backup, F_OK) == 0)
100 __connman_log_update_file_revision(rev);
102 if (rename(LOG_FILE_PATH, backup) != 0)
103 remove(LOG_FILE_PATH);
108 static void __connman_log_get_local_time(char *strtime, const int size)
111 struct tm *local_ptm;
114 gettimeofday(&tv, NULL);
115 local_ptm = localtime(&tv.tv_sec);
117 strftime(buf, sizeof(buf), "%m/%d %H:%M:%S", local_ptm);
118 snprintf(strtime, size, "%s.%03ld", buf, tv.tv_usec / 1000);
121 void __connman_log(const int log_priority, const char *format, va_list ap)
129 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
134 fstat(fileno(log_file), &buf);
135 log_size = buf.st_size;
137 if (log_size >= MAX_LOG_SIZE) {
141 __connman_log_make_backup();
143 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
149 __connman_log_get_local_time(strtime, sizeof(strtime));
151 if (vsnprintf(str, sizeof(str), format, ap) > 0)
152 fprintf(log_file, "%s %s\n", strtime, str);
155 void __connman_log_s(int log_priority, const char *format, ...)
159 va_start(ap, format);
161 vsyslog(LOG_DEBUG, format, ap);
169 * @format: format string
170 * @Varargs: list of arguments
172 * Output general information
174 void connman_info(const char *format, ...)
178 va_start(ap, format);
180 vsyslog(LOG_INFO, format, ap);
187 * @format: format string
188 * @Varargs: list of arguments
190 * Output warning messages
192 void connman_warn(const char *format, ...)
196 va_start(ap, format);
198 vsyslog(LOG_WARNING, format, ap);
205 * @format: format string
206 * @varargs: list of arguments
208 * Output error messages
210 void connman_error(const char *format, ...)
214 va_start(ap, format);
216 vsyslog(LOG_ERR, format, ap);
224 * @format: format string
225 * @varargs: list of arguments
227 * Output debug message
229 void connman_debug(const char *format, ...)
233 va_start(ap, format);
235 vsyslog(LOG_DEBUG, format, ap);
241 static void print_backtrace(unsigned int offset)
246 int outfd[2], infd[2];
253 pathlen = strlen(program_path);
255 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
262 if (pipe(infd) < 0) {
281 dup2(outfd[0], STDIN_FILENO);
282 dup2(infd[1], STDOUT_FILENO);
284 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
292 connman_error("++++++++ backtrace ++++++++");
294 for (i = offset; i < n_ptrs - 1; i++) {
296 char addr[20], buf[PATH_MAX * 2];
300 dladdr(frames[i], &info);
302 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
306 written = write(outfd[1], addr, len);
310 len = read(infd[0], buf, sizeof(buf) - 1);
316 pos = strchr(buf, '\n');
317 #if defined TIZEN_EXT
322 if (strcmp(buf, "??") == 0) {
323 connman_error("#%-2u %p in %s", i - offset,
324 frames[i], info.dli_fname);
328 ptr = strchr(pos, '\n');
331 if (strncmp(pos, program_path, pathlen) == 0)
334 connman_error("#%-2u %p in %s() at %s", i - offset,
335 frames[i], buf, pos);
336 #if defined TIZEN_EXT
341 connman_error("+++++++++++++++++++++++++++");
349 static void signal_handler(int signo)
351 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
358 static void signal_setup(sighandler_t handler)
364 sa.sa_handler = handler;
367 sigaction(SIGBUS, &sa, NULL);
368 sigaction(SIGILL, &sa, NULL);
369 sigaction(SIGFPE, &sa, NULL);
370 sigaction(SIGSEGV, &sa, NULL);
371 sigaction(SIGABRT, &sa, NULL);
372 sigaction(SIGPIPE, &sa, NULL);
375 extern struct connman_debug_desc __start___debug[];
376 extern struct connman_debug_desc __stop___debug[];
378 static gchar **enabled = NULL;
380 static bool is_enabled(struct connman_debug_desc *desc)
387 for (i = 0; enabled[i]; i++) {
388 if (desc->name && g_pattern_match_simple(enabled[i],
391 if (desc->file && g_pattern_match_simple(enabled[i],
399 void __connman_log_enable(struct connman_debug_desc *start,
400 struct connman_debug_desc *stop)
402 struct connman_debug_desc *desc;
403 const char *name = NULL, *file = NULL;
408 for (desc = start; desc < stop; desc++) {
409 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
416 if (g_strcmp0(desc->file, file) == 0) {
423 if (is_enabled(desc))
424 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
428 int __connman_log_init(const char *program, const char *debug,
429 gboolean detach, gboolean backtrace,
430 const char *program_name, const char *program_version)
432 static char path[PATH_MAX];
433 int option = LOG_NDELAY | LOG_PID;
435 program_exec = program;
436 program_path = getcwd(path, sizeof(path));
439 enabled = g_strsplit_set(debug, ":, ", 0);
441 __connman_log_enable(__start___debug, __stop___debug);
444 option |= LOG_PERROR;
447 signal_setup(signal_handler);
449 openlog(basename(program), option, LOG_DAEMON);
451 syslog(LOG_INFO, "%s version %s", program_name, program_version);
456 void __connman_log_cleanup(gboolean backtrace)
458 syslog(LOG_INFO, "Exit");
463 signal_setup(SIG_DFL);