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;
44 #define LOG_FILE_PATH "/opt/usr/data/network/connman.log"
45 #define MAX_LOG_SIZE 1 * 1024 * 1024
46 #define MAX_LOG_COUNT 15
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 if (remove(next_log_file) != 0)
82 if (access(next_log_file, F_OK) == 0)
83 __connman_log_update_file_revision(next_log_rev);
85 if (rename(log_file, next_log_file) != 0)
90 g_free(next_log_file);
93 static int __connman_log_make_backup(void)
99 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
101 if (access(backup, F_OK) == 0)
102 __connman_log_update_file_revision(rev);
104 if (rename(LOG_FILE_PATH, backup) != 0)
105 if (remove(LOG_FILE_PATH) != 0)
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 if (fstat(fileno(log_file), &buf) < 0) {
144 log_size = buf.st_size;
146 if (log_size >= MAX_LOG_SIZE) {
150 if (__connman_log_make_backup() != 0)
153 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
159 __connman_log_get_local_time(strtime, sizeof(strtime));
161 if (vsnprintf(str, sizeof(str), format, ap) > 0)
162 fprintf(log_file, "%s %s\n", strtime, str);
165 void __connman_log_s(int log_priority, const char *format, ...)
169 va_start(ap, format);
171 vsyslog(LOG_DEBUG, format, ap);
177 /* This makes sure we always have a __debug section. */
178 CONNMAN_DEBUG_DEFINE(dummy);
182 * @format: format string
183 * @Varargs: list of arguments
185 * Output general information
187 void connman_info(const char *format, ...)
191 va_start(ap, format);
193 vsyslog(LOG_INFO, format, ap);
200 * @format: format string
201 * @Varargs: list of arguments
203 * Output warning messages
205 void connman_warn(const char *format, ...)
209 va_start(ap, format);
211 vsyslog(LOG_WARNING, format, ap);
218 * @format: format string
219 * @varargs: list of arguments
221 * Output error messages
223 void connman_error(const char *format, ...)
227 va_start(ap, format);
229 vsyslog(LOG_ERR, format, ap);
236 * @format: format string
237 * @varargs: list of arguments
239 * Output debug message
241 void connman_debug(const char *format, ...)
245 va_start(ap, format);
247 vsyslog(LOG_DEBUG, format, ap);
252 static void signal_handler(int signo)
254 connman_error("Aborting (signal %d) [%s]", signo, program_exec);
256 print_backtrace(program_path, program_exec, 2);
261 static void signal_setup(sighandler_t handler)
267 sa.sa_handler = handler;
270 sigaction(SIGBUS, &sa, NULL);
271 sigaction(SIGILL, &sa, NULL);
272 sigaction(SIGFPE, &sa, NULL);
273 sigaction(SIGSEGV, &sa, NULL);
274 sigaction(SIGABRT, &sa, NULL);
275 sigaction(SIGPIPE, &sa, NULL);
278 extern struct connman_debug_desc __start___debug[];
279 extern struct connman_debug_desc __stop___debug[];
281 static gchar **enabled = NULL;
283 static bool is_enabled(struct connman_debug_desc *desc)
290 for (i = 0; enabled[i]; i++) {
291 if (desc->name && g_pattern_match_simple(enabled[i],
294 if (desc->file && g_pattern_match_simple(enabled[i],
302 void __connman_log_enable(struct connman_debug_desc *start,
303 struct connman_debug_desc *stop)
305 struct connman_debug_desc *desc;
306 const char *name = NULL, *file = NULL;
311 for (desc = start; desc < stop; desc++) {
312 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
319 if (g_strcmp0(desc->file, file) == 0) {
326 if (is_enabled(desc))
327 desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
331 int __connman_log_init(const char *program, const char *debug,
332 gboolean detach, gboolean backtrace,
333 const char *program_name, const char *program_version)
335 static char path[PATH_MAX];
336 int option = LOG_NDELAY | LOG_PID;
338 program_exec = program;
339 program_path = getcwd(path, sizeof(path));
342 enabled = g_strsplit_set(debug, ":, ", 0);
344 __connman_log_enable(__start___debug, __stop___debug);
347 option |= LOG_PERROR;
350 signal_setup(signal_handler);
352 openlog(basename(program), option, LOG_DAEMON);
354 syslog(LOG_INFO, "%s version %s", program_name, program_version);
359 void __connman_log_cleanup(gboolean backtrace)
361 syslog(LOG_INFO, "Exit");
366 signal_setup(SIG_DFL);