summaryrefslogtreecommitdiff
path: root/src/log.c
blob: 0155a6bbafc87f26282ed2c70959d6f1a6b1c346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>

#include <glib.h>

#include "lib/bluetooth.h"
#include "lib/hci.h"

#include "src/shared/util.h"
#include "src/shared/log.h"
#include "log.h"

#define LOG_IDENT "bluetoothd"

static void monitor_log(uint16_t index, int priority,
					const char *format, va_list ap)
{
	bt_log_vprintf(index, LOG_IDENT, priority, format, ap);
}

void info(const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vsyslog(LOG_INFO, format, ap);
	va_end(ap);

	va_start(ap, format);
	monitor_log(HCI_DEV_NONE, LOG_INFO, format, ap);
	va_end(ap);
}

void btd_log(uint16_t index, int priority, const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vsyslog(priority, format, ap);
	va_end(ap);

	va_start(ap, format);
	monitor_log(index, priority, format, ap);
	va_end(ap);
}

void btd_error(uint16_t index, const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vsyslog(LOG_ERR, format, ap);
	va_end(ap);

	va_start(ap, format);
	monitor_log(index, LOG_ERR, format, ap);
	va_end(ap);
}

void btd_warn(uint16_t index, const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vsyslog(LOG_WARNING, format, ap);
	va_end(ap);

	va_start(ap, format);
	monitor_log(index, LOG_WARNING, format, ap);
	va_end(ap);
}

void btd_info(uint16_t index, const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vsyslog(LOG_INFO, format, ap);
	va_end(ap);

	va_start(ap, format);
	monitor_log(index, LOG_INFO, format, ap);
	va_end(ap);
}

void btd_debug(uint16_t index, const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vsyslog(LOG_DEBUG, format, ap);
	va_end(ap);

	va_start(ap, format);
	monitor_log(index, LOG_DEBUG, format, ap);
	va_end(ap);
}

extern struct btd_debug_desc __start___debug[];
extern struct btd_debug_desc __stop___debug[];

static char **enabled = NULL;

static gboolean is_enabled(struct btd_debug_desc *desc)
{
	int i;

	if (enabled == NULL)
		return 0;

	for (i = 0; enabled[i] != NULL; i++)
		if (desc->file != NULL && g_pattern_match_simple(enabled[i],
							desc->file) == TRUE)
			return 1;

	return 0;
}

void __btd_enable_debug(struct btd_debug_desc *start,
					struct btd_debug_desc *stop)
{
	struct btd_debug_desc *desc;

	if (start == NULL || stop == NULL)
		return;

	for (desc = start; desc < stop; desc++) {
		if (is_enabled(desc))
			desc->flags |= BTD_DEBUG_FLAG_PRINT;
	}
}

void __btd_toggle_debug(void)
{
	struct btd_debug_desc *desc;

	for (desc = __start___debug; desc < __stop___debug; desc++)
		desc->flags |= BTD_DEBUG_FLAG_PRINT;
}

void __btd_log_init(const char *debug, int detach)
{
	int option = LOG_NDELAY | LOG_PID;

	if (debug != NULL)
		enabled = g_strsplit_set(debug, ":, ", 0);

	__btd_enable_debug(__start___debug, __stop___debug);

	bt_log_open();

	if (!detach)
		option |= LOG_PERROR;

	openlog(LOG_IDENT, option, LOG_DAEMON);

	info("Bluetooth daemon %s", VERSION);
}

void __btd_log_cleanup(void)
{
	closelog();

	bt_log_close();

	g_strfreev(enabled);
}