Add packaging directory
[platform/upstream/neard.git] / src / log.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2011  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 <stdarg.h>
27 #include <syslog.h>
28
29 #include "near.h"
30
31 void near_info(const char *format, ...)
32 {
33         va_list ap;
34
35         va_start(ap, format);
36
37         vsyslog(LOG_INFO, format, ap);
38
39         va_end(ap);
40 }
41
42 void near_warn(const char *format, ...)
43 {
44         va_list ap;
45
46         va_start(ap, format);
47
48         vsyslog(LOG_WARNING, format, ap);
49
50         va_end(ap);
51 }
52
53 void near_error(const char *format, ...)
54 {
55         va_list ap;
56
57         va_start(ap, format);
58
59         vsyslog(LOG_ERR, format, ap);
60
61         va_end(ap);
62 }
63
64 void near_debug(const char *format, ...)
65 {
66         va_list ap;
67
68         va_start(ap, format);
69
70         vsyslog(LOG_DEBUG, format, ap);
71
72         va_end(ap);
73 }
74
75 extern struct near_debug_desc __start___debug[];
76 extern struct near_debug_desc __stop___debug[];
77
78 static gchar **enabled = NULL;
79
80 static bool is_enabled(struct near_debug_desc *desc)
81 {
82         int i;
83
84         if (!enabled)
85                 return false;
86
87         for (i = 0; enabled[i]; i++) {
88                 if (desc->name && g_pattern_match_simple(enabled[i],
89                                                         desc->name))
90                         return true;
91                 if (desc->file && g_pattern_match_simple(enabled[i],
92                                                         desc->file))
93                         return true;
94         }
95
96         return false;
97 }
98
99 int __near_log_init(const char *debug, gboolean detach)
100 {
101         int option = LOG_NDELAY | LOG_PID;
102         struct near_debug_desc *desc;
103         const char *name = NULL, *file = NULL;
104
105         if (debug)
106                 enabled = g_strsplit_set(debug, ":, ", 0);
107
108         for (desc = __start___debug; desc < __stop___debug; desc++) {
109                 if (file || name) {
110                         if (g_strcmp0(desc->file, file) == 0) {
111                                 if (!desc->name)
112                                         desc->name = name;
113                         } else
114                                 file = NULL;
115                 }
116
117                 if (is_enabled(desc))
118                         desc->flags |= NEAR_DEBUG_FLAG_PRINT;
119         }
120
121         if (!detach)
122                 option |= LOG_PERROR;
123
124         openlog("neard", option, LOG_DAEMON);
125
126         syslog(LOG_INFO, "NEAR daemon version %s", VERSION);
127
128         return 0;
129 }
130
131 void __near_log_cleanup(void)
132 {
133         syslog(LOG_INFO, "Exit");
134
135         closelog();
136
137         g_strfreev(enabled);
138 }