Fix an issue where the log option is not applied properly
[platform/upstream/connman.git] / src / log.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <dlfcn.h>
33 #include <signal.h>
34
35 #include "connman.h"
36
37 static const char *program_exec;
38 static const char *program_path;
39
40 #if defined TIZEN_EXT
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #include <stdbool.h>
44 #include <dlog.h>
45
46 #undef LOG_TAG
47 #define LOG_TAG "CONNMAN"
48
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
52
53 #define openlog __connman_log_open
54 #define closelog __connman_log_close
55 #define vsyslog __connman_log
56 #define syslog __connman_log_s
57
58 static FILE *log_file = NULL;
59 static bool dlog_logging = true;
60 static bool file_logging = true;
61 static bool simple_log = true;
62
63 bool get_simple_log_option(void)
64 {
65         return simple_log;
66 }
67
68 void set_simple_log_option(bool option)
69 {
70         simple_log = option;
71 }
72
73 void set_dlog_logging_option(bool option)
74 {
75         dlog_logging = option;
76 }
77
78 void set_file_logging_option(bool option)
79 {
80         file_logging = option;
81 }
82
83 void __connman_log_open(const char *ident, int option, int facility)
84 {
85         if (!log_file)
86                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
87 }
88
89 void __connman_log_close(void)
90 {
91         fclose(log_file);
92         log_file = NULL;
93 }
94
95 static void __connman_log_update_file_revision(int rev)
96 {
97         int next_log_rev = 0;
98         char *log_file = NULL;
99         char *next_log_file = NULL;
100
101         next_log_rev = rev + 1;
102
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);
105
106         if (next_log_rev >= MAX_LOG_COUNT)
107                 if (remove(next_log_file) != 0)
108                         goto error;
109
110         if (access(next_log_file, F_OK) == 0)
111                 __connman_log_update_file_revision(next_log_rev);
112
113         if (rename(log_file, next_log_file) != 0)
114                 remove(log_file);
115
116 error:
117         g_free(log_file);
118         g_free(next_log_file);
119 }
120
121 static int __connman_log_make_backup(void)
122 {
123         const int rev = 0;
124         char *backup = NULL;
125         int ret = 0;
126
127         backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
128
129         if (access(backup, F_OK) == 0)
130                 __connman_log_update_file_revision(rev);
131
132         if (rename(LOG_FILE_PATH, backup) != 0)
133                 if (remove(LOG_FILE_PATH) != 0)
134                         ret = -1;
135
136         g_free(backup);
137         return ret;
138 }
139
140 static void __connman_log_get_local_time(char *strtime, const int size)
141 {
142         struct timeval tv;
143         struct tm *local_ptm;
144         char buf[32];
145
146         gettimeofday(&tv, NULL);
147         local_ptm = localtime(&tv.tv_sec);
148
149         strftime(buf, sizeof(buf), "%m/%d %H:%M:%S", local_ptm);
150         snprintf(strtime, size, "%s.%03ld", buf, tv.tv_usec / 1000);
151 }
152
153 void __connman_log(const int log_priority, const char *format, va_list ap)
154 {
155         int log_size = 0;
156         struct stat buf;
157         char str[256];
158         char strtime[64];
159
160         if (!log_file)
161                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
162
163         if (!log_file)
164                 return;
165
166         if (fstat(fileno(log_file), &buf) < 0) {
167                 fclose(log_file);
168                 log_file = NULL;
169                 return;
170         }
171
172         log_size = buf.st_size;
173
174         if (log_size >= MAX_LOG_SIZE) {
175                 fclose(log_file);
176                 log_file = NULL;
177
178                 if (__connman_log_make_backup() != 0)
179                         return;
180
181                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
182
183                 if (!log_file)
184                         return;
185         }
186
187         __connman_log_get_local_time(strtime, sizeof(strtime));
188
189         if (vsnprintf(str, sizeof(str), format, ap) > 0)
190                 fprintf(log_file, "%s %s\n", strtime, str);
191 }
192
193 void __connman_log_s(int log_priority, const char *format, ...)
194 {
195         va_list ap;
196
197         va_start(ap, format);
198
199         if (dlog_logging)
200                 dlog_vprint(DLOG_DEBUG, LOG_TAG, format, ap);
201
202         if (file_logging)
203                 vsyslog(LOG_DEBUG, format, ap);
204
205         va_end(ap);
206 }
207 #endif
208
209 /* This makes sure we always have a __debug section. */
210 CONNMAN_DEBUG_DEFINE(dummy);
211
212 /**
213  * connman_info:
214  * @format: format string
215  * @Varargs: list of arguments
216  *
217  * Output general information
218  */
219 void connman_info(const char *format, ...)
220 {
221         va_list ap;
222
223         va_start(ap, format);
224 #if defined TIZEN_EXT
225         if (dlog_logging)
226                 dlog_vprint(DLOG_INFO, LOG_TAG, format, ap);
227
228         if (file_logging)
229 #endif
230         vsyslog(LOG_INFO, format, ap);
231
232         va_end(ap);
233 }
234
235 /**
236  * connman_warn:
237  * @format: format string
238  * @Varargs: list of arguments
239  *
240  * Output warning messages
241  */
242 void connman_warn(const char *format, ...)
243 {
244         va_list ap;
245
246         va_start(ap, format);
247 #if defined TIZEN_EXT
248         if (dlog_logging)
249                 dlog_vprint(DLOG_WARN, LOG_TAG, format, ap);
250
251         if (file_logging)
252 #endif
253         vsyslog(LOG_WARNING, format, ap);
254
255         va_end(ap);
256 }
257
258 /**
259  * connman_error:
260  * @format: format string
261  * @varargs: list of arguments
262  *
263  * Output error messages
264  */
265 void connman_error(const char *format, ...)
266 {
267         va_list ap;
268
269         va_start(ap, format);
270 #if defined TIZEN_EXT
271         if (dlog_logging)
272                 dlog_vprint(DLOG_ERROR, LOG_TAG, format, ap);
273
274         if (file_logging)
275 #endif
276         vsyslog(LOG_ERR, format, ap);
277
278         va_end(ap);
279 }
280
281 /**
282  * connman_debug:
283  * @format: format string
284  * @varargs: list of arguments
285  *
286  * Output debug message
287  */
288 void connman_debug(const char *format, ...)
289 {
290         va_list ap;
291
292         va_start(ap, format);
293 #if defined TIZEN_EXT
294         if (dlog_logging)
295                 dlog_vprint(DLOG_DEBUG, LOG_TAG, format, ap);
296
297         if (file_logging)
298 #endif
299         vsyslog(LOG_DEBUG, format, ap);
300
301         va_end(ap);
302 }
303
304 static void signal_handler(int signo)
305 {
306         connman_error("Aborting (signal %d) [%s]", signo, program_exec);
307
308         print_backtrace(program_path, program_exec, 2);
309
310         exit(EXIT_FAILURE);
311 }
312
313 static void signal_setup(sighandler_t handler)
314 {
315         struct sigaction sa;
316         sigset_t mask;
317
318         sigemptyset(&mask);
319         sa.sa_handler = handler;
320         sa.sa_mask = mask;
321         sa.sa_flags = 0;
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);
328 }
329
330 extern struct connman_debug_desc __start___debug[];
331 extern struct connman_debug_desc __stop___debug[];
332
333 static gchar **enabled = NULL;
334
335 static bool is_enabled(struct connman_debug_desc *desc)
336 {
337         int i;
338
339         if (!enabled)
340                 return false;
341
342         for (i = 0; enabled[i]; i++) {
343                 if (desc->name && g_pattern_match_simple(enabled[i],
344                                                         desc->name))
345                         return true;
346                 if (desc->file && g_pattern_match_simple(enabled[i],
347                                                         desc->file))
348                         return true;
349         }
350
351         return false;
352 }
353
354 void __connman_log_enable(struct connman_debug_desc *start,
355                                         struct connman_debug_desc *stop)
356 {
357         struct connman_debug_desc *desc;
358         const char *name = NULL, *file = NULL;
359
360         if (!start || !stop)
361                 return;
362
363         for (desc = start; desc < stop; desc++) {
364                 if (desc->flags & CONNMAN_DEBUG_FLAG_ALIAS) {
365                         file = desc->file;
366                         name = desc->name;
367                         continue;
368                 }
369
370                 if (file || name) {
371                         if (g_strcmp0(desc->file, file) == 0) {
372                                 if (!desc->name)
373                                         desc->name = name;
374                         } else
375                                 file = NULL;
376                 }
377
378                 if (is_enabled(desc))
379                         desc->flags |= CONNMAN_DEBUG_FLAG_PRINT;
380         }
381 }
382
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)
386 {
387         static char path[PATH_MAX];
388         int option = LOG_NDELAY | LOG_PID;
389
390         program_exec = program;
391         program_path = getcwd(path, sizeof(path));
392
393         if (debug)
394                 enabled = g_strsplit_set(debug, ":, ", 0);
395
396         __connman_log_enable(__start___debug, __stop___debug);
397
398         if (!detach)
399                 option |= LOG_PERROR;
400
401         if (backtrace)
402                 signal_setup(signal_handler);
403
404         openlog(basename(program), option, LOG_DAEMON);
405
406         syslog(LOG_INFO, "%s version %s", program_name, program_version);
407
408         return 0;
409 }
410
411 void __connman_log_cleanup(gboolean backtrace)
412 {
413         syslog(LOG_INFO, "Exit");
414
415         closelog();
416
417         if (backtrace)
418                 signal_setup(SIG_DFL);
419
420         g_strfreev(enabled);
421 }