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 "/var/log/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;
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');
319 if (strcmp(buf, "??") == 0) {
320 connman_error("#%-2u %p in %s", i - offset,
321 frames[i], info.dli_fname);
325 ptr = strchr(pos, '\n');
328 if (strncmp(pos, program_path, pathlen) == 0)
331 connman_error("#%-2u %p in %s() at %s", i - offset,
332 frames[i], buf, pos);
335 connman_error("+++++++++++++++++++++++++++");
343 static void signal_handler(int signo)
345 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
352 static void signal_setup(sighandler_t handler)
358 sa.sa_handler = handler;
361 sigaction(SIGBUS, &sa, NULL);
362 sigaction(SIGILL, &sa, NULL);
363 sigaction(SIGFPE, &sa, NULL);
364 sigaction(SIGSEGV, &sa, NULL);
365 sigaction(SIGABRT, &sa, NULL);
366 sigaction(SIGPIPE, &sa, NULL);
369 extern struct connman_debug_desc __start___debug[];
370 extern struct connman_debug_desc __stop___debug[];
372 static gchar **enabled = NULL;
374 static bool is_enabled(struct connman_debug_desc *desc)
381 for (i = 0; enabled[i]; i++) {
382 if (desc->name && g_pattern_match_simple(enabled[i],
385 if (desc->file && g_pattern_match_simple(enabled[i],
393 void __connman_log_enable(struct connman_debug_desc *start,
394 struct connman_debug_desc *stop)
396 struct connman_debug_desc *desc;
397 const char *name = NULL, *file = NULL;
402 for (desc = start; desc < stop; desc++) {
403 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
410 if (g_strcmp0(desc->file, file) == 0) {
417 if (is_enabled(desc))
418 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
422 int __connman_log_init(const char *program, const char *debug,
423 gboolean detach, gboolean backtrace,
424 const char *program_name, const char *program_version)
426 static char path[PATH_MAX];
427 int option = LOG_NDELAY | LOG_PID;
429 program_exec = program;
430 program_path = getcwd(path, sizeof(path));
433 enabled = g_strsplit_set(debug, ":, ", 0);
435 __connman_log_enable(__start___debug, __stop___debug);
438 option |= LOG_PERROR;
441 signal_setup(signal_handler);
443 openlog(basename(program), option, LOG_DAEMON);
445 syslog(LOG_INFO, "%s version %s", program_name, program_version);
450 void __connman_log_cleanup(gboolean backtrace)
452 syslog(LOG_INFO, "Exit");
457 signal_setup(SIG_DFL);