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;
44 #define LOG_FILE_PATH "/opt/usr/data/network/connman.log"
45 #define MAX_LOG_SIZE 1 * 1024 * 1024
46 #define MAX_LOG_COUNT 3
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, "%m/%d %H:%M:%S", local_ptm);
119 void __connman_log(const int log_priority, const char *format, va_list ap)
127 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
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+");
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 static void print_backtrace(unsigned int offset)
242 int outfd[2], infd[2];
249 pathlen = strlen(program_path);
251 n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
258 if (pipe(infd) < 0) {
277 dup2(outfd[0], STDIN_FILENO);
278 dup2(infd[1], STDOUT_FILENO);
280 execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
288 connman_error("++++++++ backtrace ++++++++");
290 for (i = offset; i < n_ptrs - 1; i++) {
292 char addr[20], buf[PATH_MAX * 2];
296 dladdr(frames[i], &info);
298 len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
302 written = write(outfd[1], addr, len);
306 len = read(infd[0], buf, sizeof(buf) - 1);
312 pos = strchr(buf, '\n');
315 if (strcmp(buf, "??") == 0) {
316 connman_error("#%-2u %p in %s", i - offset,
317 frames[i], info.dli_fname);
321 ptr = strchr(pos, '\n');
324 if (strncmp(pos, program_path, pathlen) == 0)
327 connman_error("#%-2u %p in %s() at %s", i - offset,
328 frames[i], buf, pos);
331 connman_error("+++++++++++++++++++++++++++");
339 static void signal_handler(int signo)
341 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
348 static void signal_setup(sighandler_t handler)
354 sa.sa_handler = handler;
357 sigaction(SIGBUS, &sa, NULL);
358 sigaction(SIGILL, &sa, NULL);
359 sigaction(SIGFPE, &sa, NULL);
360 sigaction(SIGSEGV, &sa, NULL);
361 sigaction(SIGABRT, &sa, NULL);
362 sigaction(SIGPIPE, &sa, NULL);
365 extern struct connman_debug_desc __start___debug[];
366 extern struct connman_debug_desc __stop___debug[];
368 static gchar **enabled = NULL;
370 static bool is_enabled(struct connman_debug_desc *desc)
377 for (i = 0; enabled[i]; i++) {
378 if (desc->name && g_pattern_match_simple(enabled[i],
381 if (desc->file && g_pattern_match_simple(enabled[i],
389 void __connman_log_enable(struct connman_debug_desc *start,
390 struct connman_debug_desc *stop)
392 struct connman_debug_desc *desc;
393 const char *name = NULL, *file = NULL;
398 for (desc = start; desc < stop; desc++) {
399 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
406 if (g_strcmp0(desc->file, file) == 0) {
413 if (is_enabled(desc))
414 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
418 int __connman_log_init(const char *program, const char *debug,
419 gboolean detach, gboolean backtrace,
420 const char *program_name, const char *program_version)
422 static char path[PATH_MAX];
423 int option = LOG_NDELAY | LOG_PID;
425 program_exec = program;
426 program_path = getcwd(path, sizeof(path));
429 enabled = g_strsplit_set(debug, ":, ", 0);
431 __connman_log_enable(__start___debug, __stop___debug);
434 option |= LOG_PERROR;
437 signal_setup(signal_handler);
439 openlog(basename(program), option, LOG_DAEMON);
441 syslog(LOG_INFO, "%s version %s", program_name, program_version);
446 void __connman_log_cleanup(gboolean backtrace)
448 syslog(LOG_INFO, "Exit");
453 signal_setup(SIG_DFL);