summaryrefslogtreecommitdiff
path: root/android/log.c
blob: ae172cf7342914ec29220417fc96b94c40ff3735 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2014  Intel Corporation. All rights reserved.
 *
 *
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "src/log.h"

#define LOG_TAG "bluetoothd"

#define LOG_DEBUG 3
#define LOG_INFO 4
#define LOG_WARN 5
#define LOG_ERR 6

#define LOG_ID_SYSTEM 3

struct logd_header {
	uint8_t id;
	uint16_t pid; /* Android logd expects only 2 bytes for PID */
	uint32_t sec;
	uint32_t nsec;
} __attribute__ ((packed));

static int log_fd = -1;
static bool legacy_log = false;

static void android_log(unsigned char level, const char *fmt, va_list ap)
{
	struct logd_header header;
	struct iovec vec[4];
	int cnt = 0;
	char *msg;
	static pid_t pid = 0;

	if (log_fd < 0)
		return;

	/* no need to call getpid all the time since we don't fork */
	if (!pid)
		pid = getpid();

	if (vasprintf(&msg, fmt, ap) < 0)
		return;

	if (!legacy_log) {
		struct timespec ts;

		clock_gettime(CLOCK_REALTIME, &ts);

		header.id = LOG_ID_SYSTEM;
		header.pid = pid;
		header.sec = ts.tv_sec;
		header.nsec = ts.tv_nsec;

		vec[0].iov_base = &header;
		vec[0].iov_len = sizeof(header);

		cnt += 1;
	}

	vec[cnt + 0].iov_base = &level;
	vec[cnt + 0].iov_len = sizeof(level);
	vec[cnt + 1].iov_base = LOG_TAG;
	vec[cnt + 1].iov_len = sizeof(LOG_TAG);
	vec[cnt + 2].iov_base  = msg;
	vec[cnt + 2].iov_len  = strlen(msg) + 1;

	cnt += 3;

	writev(log_fd, vec, cnt);

	free(msg);
}

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

	va_start(ap, format);

	android_log(LOG_INFO, format, ap);

	va_end(ap);
}

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

	va_start(ap, format);

	android_log(LOG_WARN, format, ap);

	va_end(ap);
}

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

	va_start(ap, format);

	android_log(LOG_ERR, format, ap);

	va_end(ap);
}

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

	va_start(ap, format);

	android_log(LOG_DEBUG, format, ap);

	va_end(ap);
}

static bool init_legacy_log(void)
{
	log_fd = open("/dev/log/system", O_WRONLY);
	if (log_fd < 0)
		return false;

	legacy_log = true;

	return true;
}

static bool init_logd(void)
{
	struct sockaddr_un addr;

	log_fd = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
	if (log_fd < 0)
		return false;

	if (fcntl(log_fd, F_SETFL, O_NONBLOCK) < 0)
		goto failed;

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, "/dev/socket/logdw");

	if (connect(log_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
		goto failed;

	return true;

failed:
	close(log_fd);
	log_fd = -1;

	return false;
}

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

void __btd_log_init(const char *debug, int detach)
{
	if (!init_logd() && !init_legacy_log())
		return;

	if (debug) {
		struct btd_debug_desc *desc;

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

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

void __btd_log_cleanup(void)
{
	if (log_fd < 0)
		return;

	close(log_fd);
	log_fd = -1;
}