Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / obexd / src / log.c
1 /*
2  *
3  *  OBEX Server
4  *
5  *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <syslog.h>
31
32 #include <glib.h>
33
34 #include "log.h"
35
36 void info(const char *format, ...)
37 {
38         va_list ap;
39
40         va_start(ap, format);
41
42         vsyslog(LOG_INFO, format, ap);
43
44         va_end(ap);
45 }
46
47 void error(const char *format, ...)
48 {
49         va_list ap;
50
51         va_start(ap, format);
52
53         vsyslog(LOG_ERR, format, ap);
54
55         va_end(ap);
56 }
57
58 void obex_debug(const char *format, ...)
59 {
60         va_list ap;
61
62         va_start(ap, format);
63
64         vsyslog(LOG_DEBUG, format, ap);
65
66         va_end(ap);
67 }
68
69 extern struct obex_debug_desc __start___debug[];
70 extern struct obex_debug_desc __stop___debug[];
71
72 static char **enabled = NULL;
73
74 static gboolean is_enabled(struct obex_debug_desc *desc)
75 {
76         int i;
77
78         if (enabled == NULL)
79                 return 0;
80
81         for (i = 0; enabled[i] != NULL; i++) {
82                 if (desc->name != NULL && g_pattern_match_simple(enabled[i],
83                                                         desc->name) == TRUE)
84                         return 1;
85                 if (desc->file != NULL && g_pattern_match_simple(enabled[i],
86                                                         desc->file) == TRUE)
87                         return 1;
88         }
89
90         return 0;
91 }
92
93 void __obex_log_enable_debug(void)
94 {
95         struct obex_debug_desc *desc;
96
97         for (desc = __start___debug; desc < __stop___debug; desc++)
98                 desc->flags |= OBEX_DEBUG_FLAG_PRINT;
99 }
100
101 void __obex_log_init(const char *debug, int detach)
102 {
103         int option = LOG_NDELAY | LOG_PID;
104         struct obex_debug_desc *desc;
105         const char *name = NULL, *file = NULL;
106
107         if (debug != NULL)
108                 enabled = g_strsplit_set(debug, ":, ", 0);
109
110         for (desc = __start___debug; desc < __stop___debug; desc++) {
111                 if (file != NULL || name != NULL) {
112                         if (g_strcmp0(desc->file, file) == 0) {
113                                 if (desc->name == NULL)
114                                         desc->name = name;
115                         } else
116                                 file = NULL;
117                 }
118
119                 if (is_enabled(desc))
120                         desc->flags |= OBEX_DEBUG_FLAG_PRINT;
121         }
122
123         if (!detach)
124                 option |= LOG_PERROR;
125
126         openlog("obexd", option, LOG_DAEMON);
127
128         info("OBEX daemon %s", VERSION);
129 }
130
131 void __obex_log_cleanup(void)
132 {
133         closelog();
134
135         g_strfreev(enabled);
136 }