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;
46 #define LOG_TAG "CONNMAN"
48 #define LOG_FILE_PATH "/opt/usr/data/network/connman.log"
49 #define MAX_LOG_SIZE 1 * 1024 * 1024
50 #define MAX_LOG_COUNT 15
52 #define openlog __connman_log_open
53 #define closelog __connman_log_close
54 #define vsyslog __connman_log
55 #define syslog __connman_log_s
57 static FILE *log_file = NULL;
59 void __connman_log_open(const char *ident, int option, int facility)
62 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
65 void __connman_log_close(void)
71 static void __connman_log_update_file_revision(int rev)
74 char *log_file = NULL;
75 char *next_log_file = NULL;
77 next_log_rev = rev + 1;
79 log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
80 next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
82 if (next_log_rev >= MAX_LOG_COUNT)
83 if (remove(next_log_file) != 0)
86 if (access(next_log_file, F_OK) == 0)
87 __connman_log_update_file_revision(next_log_rev);
89 if (rename(log_file, next_log_file) != 0)
94 g_free(next_log_file);
97 static int __connman_log_make_backup(void)
103 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
105 if (access(backup, F_OK) == 0)
106 __connman_log_update_file_revision(rev);
108 if (rename(LOG_FILE_PATH, backup) != 0)
109 if (remove(LOG_FILE_PATH) != 0)
116 static void __connman_log_get_local_time(char *strtime, const int size)
119 struct tm *local_ptm;
122 gettimeofday(&tv, NULL);
123 local_ptm = localtime(&tv.tv_sec);
125 strftime(buf, sizeof(buf), "%m/%d %H:%M:%S", local_ptm);
126 snprintf(strtime, size, "%s.%03ld", buf, tv.tv_usec / 1000);
129 void __connman_log(const int log_priority, const char *format, va_list ap)
137 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
142 if (fstat(fileno(log_file), &buf) < 0) {
148 log_size = buf.st_size;
150 if (log_size >= MAX_LOG_SIZE) {
154 if (__connman_log_make_backup() != 0)
157 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
163 __connman_log_get_local_time(strtime, sizeof(strtime));
165 if (vsnprintf(str, sizeof(str), format, ap) > 0)
166 fprintf(log_file, "%s %s\n", strtime, str);
169 void __connman_log_s(int log_priority, const char *format, ...)
173 va_start(ap, format);
176 dlog_vprint(DLOG_DEBUG, LOG_TAG, format, ap);
179 vsyslog(LOG_DEBUG, format, ap);
185 /* This makes sure we always have a __debug section. */
186 CONNMAN_DEBUG_DEFINE(dummy);
190 * @format: format string
191 * @Varargs: list of arguments
193 * Output general information
195 void connman_info(const char *format, ...)
199 va_start(ap, format);
200 #if defined TIZEN_EXT
202 dlog_vprint(DLOG_INFO, LOG_TAG, format, ap);
206 vsyslog(LOG_INFO, format, ap);
213 * @format: format string
214 * @Varargs: list of arguments
216 * Output warning messages
218 void connman_warn(const char *format, ...)
222 va_start(ap, format);
223 #if defined TIZEN_EXT
225 dlog_vprint(DLOG_WARN, LOG_TAG, format, ap);
229 vsyslog(LOG_WARNING, format, ap);
236 * @format: format string
237 * @varargs: list of arguments
239 * Output error messages
241 void connman_error(const char *format, ...)
245 va_start(ap, format);
246 #if defined TIZEN_EXT
248 dlog_vprint(DLOG_ERROR, LOG_TAG, format, ap);
252 vsyslog(LOG_ERR, format, ap);
259 * @format: format string
260 * @varargs: list of arguments
262 * Output debug message
264 void connman_debug(const char *format, ...)
268 va_start(ap, format);
269 #if defined TIZEN_EXT
271 dlog_vprint(DLOG_DEBUG, LOG_TAG, format, ap);
275 vsyslog(LOG_DEBUG, format, ap);
280 static void signal_handler(int signo)
282 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
284 print_backtrace(program_path, program_exec, 2);
289 static void signal_setup(sighandler_t handler)
295 sa.sa_handler = handler;
298 sigaction(SIGBUS, &sa, NULL);
299 sigaction(SIGILL, &sa, NULL);
300 sigaction(SIGFPE, &sa, NULL);
301 sigaction(SIGSEGV, &sa, NULL);
302 sigaction(SIGABRT, &sa, NULL);
303 sigaction(SIGPIPE, &sa, NULL);
306 extern struct connman_debug_desc __start___debug[];
307 extern struct connman_debug_desc __stop___debug[];
309 static gchar **enabled = NULL;
311 static bool is_enabled(struct connman_debug_desc *desc)
318 for (i = 0; enabled[i]; i++) {
319 if (desc->name && g_pattern_match_simple(enabled[i],
322 if (desc->file && g_pattern_match_simple(enabled[i],
330 void __connman_log_enable(struct connman_debug_desc *start,
331 struct connman_debug_desc *stop)
333 struct connman_debug_desc *desc;
334 const char *name = NULL, *file = NULL;
339 for (desc = start; desc < stop; desc++) {
340 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
347 if (g_strcmp0(desc->file, file) == 0) {
354 if (is_enabled(desc))
355 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
359 int __connman_log_init(const char *program, const char *debug,
360 gboolean detach, gboolean backtrace,
361 const char *program_name, const char *program_version)
363 static char path[PATH_MAX];
364 int option = LOG_NDELAY | LOG_PID;
366 program_exec = program;
367 program_path = getcwd(path, sizeof(path));
370 enabled = g_strsplit_set(debug, ":, ", 0);
372 __connman_log_enable(__start___debug, __stop___debug);
375 option |= LOG_PERROR;
378 signal_setup(signal_handler);
380 openlog(basename(program), option, LOG_DAEMON);
382 syslog(LOG_INFO, "%s version %s", program_name, program_version);
387 void __connman_log_cleanup(gboolean backtrace)
389 syslog(LOG_INFO, "Exit");
394 signal_setup(SIG_DFL);