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 vsyslog(LOG_DEBUG, format, ap);
209 /* This makes sure we always have a __debug section. */
210 CONNMAN_DEBUG_DEFINE(dummy);
214 * @format: format string
215 * @Varargs: list of arguments
217 * Output general information
219 void connman_info(const char *format, ...)
223 va_start(ap, format);
224 #if defined TIZEN_EXT
226 dlog_vprint(DLOG_INFO, LOG_TAG, format, ap);
230 vsyslog(LOG_INFO, format, ap);
237 * @format: format string
238 * @Varargs: list of arguments
240 * Output warning messages
242 void connman_warn(const char *format, ...)
246 va_start(ap, format);
247 #if defined TIZEN_EXT
249 dlog_vprint(DLOG_WARN, LOG_TAG, format, ap);
253 vsyslog(LOG_WARNING, format, ap);
260 * @format: format string
261 * @varargs: list of arguments
263 * Output error messages
265 void connman_error(const char *format, ...)
269 va_start(ap, format);
270 #if defined TIZEN_EXT
272 dlog_vprint(DLOG_ERROR, LOG_TAG, format, ap);
276 vsyslog(LOG_ERR, format, ap);
283 * @format: format string
284 * @varargs: list of arguments
286 * Output debug message
288 void connman_debug(const char *format, ...)
292 va_start(ap, format);
293 #if defined TIZEN_EXT
295 dlog_vprint(DLOG_DEBUG, LOG_TAG, format, ap);
299 vsyslog(LOG_DEBUG, format, ap);
304 static void signal_handler(int signo)
306 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
308 print_backtrace(program_path, program_exec, 2);
313 static void signal_setup(sighandler_t handler)
319 sa.sa_handler = handler;
322 sigaction(SIGBUS, &sa, NULL);
323 sigaction(SIGILL, &sa, NULL);
324 sigaction(SIGFPE, &sa, NULL);
325 sigaction(SIGSEGV, &sa, NULL);
326 sigaction(SIGABRT, &sa, NULL);
327 sigaction(SIGPIPE, &sa, NULL);
330 extern struct connman_debug_desc __start___debug[];
331 extern struct connman_debug_desc __stop___debug[];
333 static gchar **enabled = NULL;
335 static bool is_enabled(struct connman_debug_desc *desc)
342 for (i = 0; enabled[i]; i++) {
343 if (desc->name && g_pattern_match_simple(enabled[i],
346 if (desc->file && g_pattern_match_simple(enabled[i],
354 void __connman_log_enable(struct connman_debug_desc *start,
355 struct connman_debug_desc *stop)
357 struct connman_debug_desc *desc;
358 const char *name = NULL, *file = NULL;
363 for (desc = start; desc < stop; desc++) {
364 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
371 if (g_strcmp0(desc->file, file) == 0) {
378 if (is_enabled(desc))
379 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
383 int __connman_log_init(const char *program, const char *debug,
384 gboolean detach, gboolean backtrace,
385 const char *program_name, const char *program_version)
387 static char path[PATH_MAX];
388 int option = LOG_NDELAY | LOG_PID;
390 program_exec = program;
391 program_path = getcwd(path, sizeof(path));
394 enabled = g_strsplit_set(debug, ":, ", 0);
396 __connman_log_enable(__start___debug, __stop___debug);
399 option |= LOG_PERROR;
402 signal_setup(signal_handler);
404 openlog(basename(program), option, LOG_DAEMON);
406 syslog(LOG_INFO, "%s version %s", program_name, program_version);
411 void __connman_log_cleanup(gboolean backtrace)
413 syslog(LOG_INFO, "Exit");
418 signal_setup(SIG_DFL);