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 1
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;
55 void __connman_log_s(int log_priority, const char *format, ...);
57 void __connman_log_open(const char *ident, int option, int facility)
60 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
63 void __connman_log_close(void)
69 static void __connman_log_update_file_revision(int rev)
72 char *log_file = NULL;
73 char *next_log_file = NULL;
75 next_log_rev = rev + 1;
77 log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
78 next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
80 if (next_log_rev >= MAX_LOG_COUNT)
81 remove(next_log_file);
83 if (access(next_log_file, F_OK) == 0)
84 __connman_log_update_file_revision(next_log_rev);
86 if (rename(log_file, next_log_file) != 0)
90 g_free(next_log_file);
93 static void __connman_log_make_backup(void)
98 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
100 if (access(backup, F_OK) == 0)
101 __connman_log_update_file_revision(rev);
103 if (rename(LOG_FILE_PATH, backup) != 0)
104 remove(LOG_FILE_PATH);
109 static void __connman_log_get_local_time(char *strtime, const int size)
112 struct tm *local_ptm;
115 gettimeofday(&tv, NULL);
116 local_ptm = localtime(&tv.tv_sec);
118 strftime(buf, sizeof(buf), "%m/%d %H:%M:%S", local_ptm);
119 snprintf(strtime, size, "%s.%03ld", buf, tv.tv_usec / 1000);
122 void __connman_log(const int log_priority, const char *format, va_list ap)
130 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
135 fstat(fileno(log_file), &buf);
136 log_size = buf.st_size;
138 if (log_size >= MAX_LOG_SIZE) {
142 __connman_log_make_backup();
144 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
150 __connman_log_get_local_time(strtime, sizeof(strtime));
152 if (vsnprintf(str, sizeof(str), format, ap) > 0)
153 fprintf(log_file, "%s %s\n", strtime, str);
156 void __connman_log_s(int log_priority, const char *format, ...)
160 va_start(ap, format);
162 vsyslog(LOG_DEBUG, format, ap);
170 * @format: format string
171 * @Varargs: list of arguments
173 * Output general information
175 void connman_info(const char *format, ...)
179 va_start(ap, format);
181 vsyslog(LOG_INFO, format, ap);
188 * @format: format string
189 * @Varargs: list of arguments
191 * Output warning messages
193 void connman_warn(const char *format, ...)
197 va_start(ap, format);
199 vsyslog(LOG_WARNING, format, ap);
206 * @format: format string
207 * @varargs: list of arguments
209 * Output error messages
211 void connman_error(const char *format, ...)
215 va_start(ap, format);
217 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);
240 static void print_backtrace(unsigned int offset)
245 int outfd[2], infd[2];
252 pathlen = strlen(program_path);
254 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
261 if (pipe(infd) < 0) {
280 dup2(outfd[0], STDIN_FILENO);
281 dup2(infd[1], STDOUT_FILENO);
283 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
291 connman_error("++++++++ backtrace ++++++++");
293 for (i = offset; i < n_ptrs - 1; i++) {
295 char addr[20], buf[PATH_MAX * 2];
299 dladdr(frames[i], &info);
301 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
305 written = write(outfd[1], addr, len);
309 len = read(infd[0], buf, sizeof(buf) - 1);
315 pos = strchr(buf, '\n');
318 if (strcmp(buf, "??") == 0) {
319 connman_error("#%-2u %p in %s", i - offset,
320 frames[i], info.dli_fname);
324 ptr = strchr(pos, '\n');
327 if (strncmp(pos, program_path, pathlen) == 0)
330 connman_error("#%-2u %p in %s() at %s", i - offset,
331 frames[i], buf, pos);
334 connman_error("+++++++++++++++++++++++++++");
342 static void signal_handler(int signo)
344 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
351 static void signal_setup(sighandler_t handler)
357 sa.sa_handler = handler;
360 sigaction(SIGBUS, &sa, NULL);
361 sigaction(SIGILL, &sa, NULL);
362 sigaction(SIGFPE, &sa, NULL);
363 sigaction(SIGSEGV, &sa, NULL);
364 sigaction(SIGABRT, &sa, NULL);
365 sigaction(SIGPIPE, &sa, NULL);
368 extern struct connman_debug_desc __start___debug[];
369 extern struct connman_debug_desc __stop___debug[];
371 static gchar **enabled = NULL;
373 static bool is_enabled(struct connman_debug_desc *desc)
380 for (i = 0; enabled[i]; i++) {
381 if (desc->name && g_pattern_match_simple(enabled[i],
384 if (desc->file && g_pattern_match_simple(enabled[i],
392 void __connman_log_enable(struct connman_debug_desc *start,
393 struct connman_debug_desc *stop)
395 struct connman_debug_desc *desc;
396 const char *name = NULL, *file = NULL;
401 for (desc = start; desc < stop; desc++) {
402 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
409 if (g_strcmp0(desc->file, file) == 0) {
416 if (is_enabled(desc))
417 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
421 int __connman_log_init(const char *program, const char *debug,
422 gboolean detach, gboolean backtrace,
423 const char *program_name, const char *program_version)
425 static char path[PATH_MAX];
426 int option = LOG_NDELAY | LOG_PID;
428 program_exec = program;
429 program_path = getcwd(path, sizeof(path));
432 enabled = g_strsplit_set(debug, ":, ", 0);
434 __connman_log_enable(__start___debug, __stop___debug);
437 option |= LOG_PERROR;
440 signal_setup(signal_handler);
442 openlog(basename(program), option, LOG_DAEMON);
444 syslog(LOG_INFO, "%s version %s", program_name, program_version);
449 void __connman_log_cleanup(gboolean backtrace)
451 syslog(LOG_INFO, "Exit");
456 signal_setup(SIG_DFL);