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 if (remove(next_log_file) != 0)
82 __connman_log_s(LOG_INFO, "error: remove failed for %s", next_log_file);
84 if (access(next_log_file, F_OK) == 0)
85 __connman_log_update_file_revision(next_log_rev);
87 if (rename(log_file, next_log_file) != 0)
88 if (remove(log_file) != 0)
89 __connman_log_s(LOG_INFO, "error: remove failed for %s", log_file);
92 g_free(next_log_file);
95 static void __connman_log_make_backup(void)
100 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
102 if (access(backup, F_OK) == 0)
103 __connman_log_update_file_revision(rev);
105 if (rename(LOG_FILE_PATH, backup) != 0)
106 if (remove(LOG_FILE_PATH) != 0)
107 __connman_log_s(LOG_INFO, "error: remove failed for %s", LOG_FILE_PATH);
112 static void __connman_log_get_local_time(char *strtime, const int size)
115 struct tm *local_ptm;
118 gettimeofday(&tv, NULL);
119 local_ptm = localtime(&tv.tv_sec);
121 strftime(buf, sizeof(buf), "%m/%d %H:%M:%S", local_ptm);
122 snprintf(strtime, size, "%s.%03ld", buf, tv.tv_usec / 1000);
125 void __connman_log(const int log_priority, const char *format, va_list ap)
133 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
138 fstat(fileno(log_file), &buf);
139 log_size = buf.st_size;
141 if (log_size >= MAX_LOG_SIZE) {
145 __connman_log_make_backup();
147 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
153 __connman_log_get_local_time(strtime, sizeof(strtime));
155 if (vsnprintf(str, sizeof(str), format, ap) > 0)
156 fprintf(log_file, "%s %s\n", strtime, str);
159 void __connman_log_s(int log_priority, const char *format, ...)
163 va_start(ap, format);
165 vsyslog(LOG_DEBUG, format, ap);
173 * @format: format string
174 * @Varargs: list of arguments
176 * Output general information
178 void connman_info(const char *format, ...)
182 va_start(ap, format);
184 vsyslog(LOG_INFO, format, ap);
191 * @format: format string
192 * @Varargs: list of arguments
194 * Output warning messages
196 void connman_warn(const char *format, ...)
200 va_start(ap, format);
202 vsyslog(LOG_WARNING, format, ap);
209 * @format: format string
210 * @varargs: list of arguments
212 * Output error messages
214 void connman_error(const char *format, ...)
218 va_start(ap, format);
220 vsyslog(LOG_ERR, format, ap);
227 * @format: format string
228 * @varargs: list of arguments
230 * Output debug message
232 void connman_debug(const char *format, ...)
236 va_start(ap, format);
238 vsyslog(LOG_DEBUG, format, ap);
243 static void print_backtrace(unsigned int offset)
248 int outfd[2], infd[2];
255 pathlen = strlen(program_path);
257 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
264 if (pipe(infd) < 0) {
283 dup2(outfd[0], STDIN_FILENO);
284 dup2(infd[1], STDOUT_FILENO);
286 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
294 connman_error("++++++++ backtrace ++++++++");
296 for (i = offset; i < n_ptrs - 1; i++) {
298 char addr[20], buf[PATH_MAX * 2];
302 dladdr(frames[i], &info);
304 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
308 written = write(outfd[1], addr, len);
312 len = read(infd[0], buf, sizeof(buf) - 1);
318 pos = strchr(buf, '\n');
321 if (strcmp(buf, "??") == 0) {
322 connman_error("#%-2u %p in %s", i - offset,
323 frames[i], info.dli_fname);
327 ptr = strchr(pos, '\n');
330 if (strncmp(pos, program_path, pathlen) == 0)
333 connman_error("#%-2u %p in %s() at %s", i - offset,
334 frames[i], buf, pos);
337 connman_error("+++++++++++++++++++++++++++");
345 static void signal_handler(int signo)
347 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
354 static void signal_setup(sighandler_t handler)
360 sa.sa_handler = handler;
363 sigaction(SIGBUS, &sa, NULL);
364 sigaction(SIGILL, &sa, NULL);
365 sigaction(SIGFPE, &sa, NULL);
366 sigaction(SIGSEGV, &sa, NULL);
367 sigaction(SIGABRT, &sa, NULL);
368 sigaction(SIGPIPE, &sa, NULL);
371 extern struct connman_debug_desc __start___debug[];
372 extern struct connman_debug_desc __stop___debug[];
374 static gchar **enabled = NULL;
376 static bool is_enabled(struct connman_debug_desc *desc)
383 for (i = 0; enabled[i]; i++) {
384 if (desc->name && g_pattern_match_simple(enabled[i],
387 if (desc->file && g_pattern_match_simple(enabled[i],
395 void __connman_log_enable(struct connman_debug_desc *start,
396 struct connman_debug_desc *stop)
398 struct connman_debug_desc *desc;
399 const char *name = NULL, *file = NULL;
404 for (desc = start; desc < stop; desc++) {
405 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
412 if (g_strcmp0(desc->file, file) == 0) {
419 if (is_enabled(desc))
420 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
424 int __connman_log_init(const char *program, const char *debug,
425 gboolean detach, gboolean backtrace,
426 const char *program_name, const char *program_version)
428 static char path[PATH_MAX];
429 int option = LOG_NDELAY | LOG_PID;
431 program_exec = program;
432 program_path = getcwd(path, sizeof(path));
435 enabled = g_strsplit_set(debug, ":, ", 0);
437 __connman_log_enable(__start___debug, __stop___debug);
440 option |= LOG_PERROR;
443 signal_setup(signal_handler);
445 openlog(basename(program), option, LOG_DAEMON);
447 syslog(LOG_INFO, "%s version %s", program_name, program_version);
452 void __connman_log_cleanup(gboolean backtrace)
454 syslog(LOG_INFO, "Exit");
459 signal_setup(SIG_DFL);