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
37 static const char *program_exec;
38 static const char *program_path;
47 #define LOG_TAG "CONNMAN"
49 #define LOG_FILE_PATH "/opt/usr/data/network/connman.log"
50 #define MAX_LOG_SIZE 1 * 1024 * 1024
51 #define MAX_LOG_COUNT 15
53 #define openlog __connman_log_open
54 #define closelog __connman_log_close
55 #define vsyslog __connman_log
56 #define syslog __connman_log_s
58 static FILE *log_file = NULL;
59 static bool dlog_logging = true;
60 static bool file_logging = true;
61 static bool simple_log = true;
63 bool get_simple_log_option(void)
68 void set_simple_log_option(bool option)
73 void set_dlog_logging_option(bool option)
75 dlog_logging = option;
78 void set_file_logging_option(bool option)
80 file_logging = option;
83 void __connman_log_open(const char *ident, int option, int facility)
86 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
89 void __connman_log_close(void)
95 static void __connman_log_update_file_revision(int rev)
98 char *log_file = NULL;
99 char *next_log_file = NULL;
101 next_log_rev = rev + 1;
103 log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
104 next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
106 if (next_log_rev >= MAX_LOG_COUNT)
107 if (remove(next_log_file) != 0)
110 if (access(next_log_file, F_OK) == 0)
111 __connman_log_update_file_revision(next_log_rev);
113 if (rename(log_file, next_log_file) != 0)
118 g_free(next_log_file);
121 static int __connman_log_make_backup(void)
127 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
129 if (access(backup, F_OK) == 0)
130 __connman_log_update_file_revision(rev);
132 if (rename(LOG_FILE_PATH, backup) != 0)
133 if (remove(LOG_FILE_PATH) != 0)
140 static void __connman_log_get_local_time(char *strtime, const int size)
143 struct tm *local_ptm;
146 gettimeofday(&tv, NULL);
147 local_ptm = localtime(&tv.tv_sec);
149 strftime(buf, sizeof(buf), "%m/%d %H:%M:%S", local_ptm);
150 snprintf(strtime, size, "%s.%03ld", buf, tv.tv_usec / 1000);
153 void __connman_log(const int log_priority, const char *format, va_list ap)
161 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
166 if (fstat(fileno(log_file), &buf) < 0) {
172 log_size = buf.st_size;
174 if (log_size >= MAX_LOG_SIZE) {
178 if (__connman_log_make_backup() != 0)
181 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
187 __connman_log_get_local_time(strtime, sizeof(strtime));
189 if (vsnprintf(str, sizeof(str), format, ap) > 0)
190 fprintf(log_file, "%s %s\n", strtime, str);
193 void __connman_log_s(int log_priority, const char *format, ...)
197 va_start(ap, format);
200 dlog_vprint(DLOG_DEBUG, LOG_TAG, format, ap);
203 va_start(ap, format);
206 vsyslog(LOG_DEBUG, format, ap);
212 /* This makes sure we always have a __debug section. */
213 CONNMAN_DEBUG_DEFINE(dummy);
217 * @format: format string
218 * @Varargs: list of arguments
220 * Output general information
222 void connman_info(const char *format, ...)
226 va_start(ap, format);
227 #if defined TIZEN_EXT
229 dlog_vprint(DLOG_INFO, LOG_TAG, format, ap);
232 va_start(ap, format);
236 vsyslog(LOG_INFO, format, ap);
243 * @format: format string
244 * @Varargs: list of arguments
246 * Output warning messages
248 void connman_warn(const char *format, ...)
252 va_start(ap, format);
253 #if defined TIZEN_EXT
255 dlog_vprint(DLOG_WARN, LOG_TAG, format, ap);
258 va_start(ap, format);
262 vsyslog(LOG_WARNING, format, ap);
269 * @format: format string
270 * @varargs: list of arguments
272 * Output error messages
274 void connman_error(const char *format, ...)
278 va_start(ap, format);
279 #if defined TIZEN_EXT
281 dlog_vprint(DLOG_ERROR, LOG_TAG, format, ap);
284 va_start(ap, format);
288 vsyslog(LOG_ERR, format, ap);
295 * @format: format string
296 * @varargs: list of arguments
298 * Output debug message
300 void connman_debug(const char *format, ...)
304 va_start(ap, format);
305 #if defined TIZEN_EXT
307 dlog_vprint(DLOG_DEBUG, LOG_TAG, format, ap);
310 va_start(ap, format);
314 vsyslog(LOG_DEBUG, format, ap);
319 static void signal_handler(int signo)
321 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
323 print_backtrace(program_path, program_exec, 2);
328 static void signal_setup(sighandler_t handler)
334 sa.sa_handler = handler;
337 sigaction(SIGBUS, &sa, NULL);
338 sigaction(SIGILL, &sa, NULL);
339 sigaction(SIGFPE, &sa, NULL);
340 sigaction(SIGSEGV, &sa, NULL);
341 sigaction(SIGABRT, &sa, NULL);
342 sigaction(SIGPIPE, &sa, NULL);
345 extern struct connman_debug_desc __start___debug[];
346 extern struct connman_debug_desc __stop___debug[];
348 static gchar **enabled = NULL;
350 static bool is_enabled(struct connman_debug_desc *desc)
357 for (i = 0; enabled[i]; i++) {
358 if (desc->name && g_pattern_match_simple(enabled[i],
361 if (desc->file && g_pattern_match_simple(enabled[i],
369 void __connman_log_enable(struct connman_debug_desc *start,
370 struct connman_debug_desc *stop)
372 struct connman_debug_desc *desc;
373 const char *name = NULL, *file = NULL;
378 for (desc = start; desc < stop; desc++) {
379 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
386 if (g_strcmp0(desc->file, file) == 0) {
393 if (is_enabled(desc))
394 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
398 int __connman_log_init(const char *program, const char *debug,
399 gboolean detach, gboolean backtrace,
400 const char *program_name, const char *program_version)
402 static char path[PATH_MAX];
403 int option = LOG_NDELAY | LOG_PID;
405 program_exec = program;
406 program_path = getcwd(path, sizeof(path));
409 enabled = g_strsplit_set(debug, ":, ", 0);
411 __connman_log_enable(__start___debug, __stop___debug);
414 option |= LOG_PERROR;
417 signal_setup(signal_handler);
419 openlog(basename(program), option, LOG_DAEMON);
421 syslog(LOG_INFO, "%s version %s", program_name, program_version);
426 void __connman_log_cleanup(gboolean backtrace)
428 syslog(LOG_INFO, "Exit");
433 signal_setup(SIG_DFL);