Blame src/log.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#define _GNU_SOURCE
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <errno.h>
Packit 34410b
#include <syslog.h>
Packit 34410b
#include <stdarg.h>
Packit 34410b
#include <stdint.h>
Packit 34410b
#include <stdlib.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <string.h>
Packit 34410b
#include <sys/socket.h>
Packit 34410b
Packit 34410b
#include <glib.h>
Packit 34410b
Packit 34410b
#include "lib/bluetooth.h"
Packit 34410b
#include "lib/hci.h"
Packit 34410b
Packit 34410b
#include "src/shared/util.h"
Packit 34410b
#include "src/shared/log.h"
Packit 34410b
#include "log.h"
Packit 34410b
Packit 34410b
#define LOG_IDENT "bluetoothd"
Packit 34410b
Packit 34410b
static void monitor_log(uint16_t index, int priority,
Packit 34410b
					const char *format, va_list ap)
Packit 34410b
{
Packit 34410b
	bt_log_vprintf(index, LOG_IDENT, priority, format, ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void error(const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(LOG_ERR, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(HCI_DEV_NONE, LOG_ERR, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void warn(const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(LOG_WARNING, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(HCI_DEV_NONE, LOG_WARNING, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void info(const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(LOG_INFO, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(HCI_DEV_NONE, LOG_INFO, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void btd_log(uint16_t index, int priority, const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(priority, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(index, priority, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void btd_error(uint16_t index, const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(LOG_ERR, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(index, LOG_ERR, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void btd_warn(uint16_t index, const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(LOG_WARNING, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(index, LOG_WARNING, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void btd_info(uint16_t index, const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(LOG_INFO, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(index, LOG_INFO, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
void btd_debug(uint16_t index, const char *format, ...)
Packit 34410b
{
Packit 34410b
	va_list ap;
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	vsyslog(LOG_DEBUG, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
Packit 34410b
	va_start(ap, format);
Packit 34410b
	monitor_log(index, LOG_DEBUG, format, ap);
Packit 34410b
	va_end(ap);
Packit 34410b
}
Packit 34410b
Packit 34410b
extern struct btd_debug_desc __start___debug[];
Packit 34410b
extern struct btd_debug_desc __stop___debug[];
Packit 34410b
Packit 34410b
static char **enabled = NULL;
Packit 34410b
Packit 34410b
static gboolean is_enabled(struct btd_debug_desc *desc)
Packit 34410b
{
Packit 34410b
	int i;
Packit 34410b
Packit 34410b
	if (enabled == NULL)
Packit 34410b
		return 0;
Packit 34410b
Packit 34410b
	for (i = 0; enabled[i] != NULL; i++)
Packit 34410b
		if (desc->file != NULL && g_pattern_match_simple(enabled[i],
Packit 34410b
							desc->file) == TRUE)
Packit 34410b
			return 1;
Packit 34410b
Packit 34410b
	return 0;
Packit 34410b
}
Packit 34410b
Packit 34410b
void __btd_enable_debug(struct btd_debug_desc *start,
Packit 34410b
					struct btd_debug_desc *stop)
Packit 34410b
{
Packit 34410b
	struct btd_debug_desc *desc;
Packit 34410b
Packit 34410b
	if (start == NULL || stop == NULL)
Packit 34410b
		return;
Packit 34410b
Packit 34410b
	for (desc = start; desc < stop; desc++) {
Packit 34410b
		if (is_enabled(desc))
Packit 34410b
			desc->flags |= BTD_DEBUG_FLAG_PRINT;
Packit 34410b
	}
Packit 34410b
}
Packit 34410b
Packit 34410b
void __btd_toggle_debug(void)
Packit 34410b
{
Packit 34410b
	struct btd_debug_desc *desc;
Packit 34410b
Packit 34410b
	for (desc = __start___debug; desc < __stop___debug; desc++)
Packit 34410b
		desc->flags |= BTD_DEBUG_FLAG_PRINT;
Packit 34410b
}
Packit 34410b
Packit 34410b
void __btd_log_init(const char *debug, int detach)
Packit 34410b
{
Packit 34410b
	int option = LOG_NDELAY | LOG_PID;
Packit 34410b
Packit 34410b
	if (debug != NULL)
Packit 34410b
		enabled = g_strsplit_set(debug, ":, ", 0);
Packit 34410b
Packit 34410b
	__btd_enable_debug(__start___debug, __stop___debug);
Packit 34410b
Packit 34410b
	bt_log_open();
Packit 34410b
Packit 34410b
	if (!detach)
Packit 34410b
		option |= LOG_PERROR;
Packit 34410b
Packit 34410b
	openlog(LOG_IDENT, option, LOG_DAEMON);
Packit 34410b
Packit 34410b
	info("Bluetooth daemon %s", VERSION);
Packit 34410b
}
Packit 34410b
Packit 34410b
void __btd_log_cleanup(void)
Packit 34410b
{
Packit 34410b
	closelog();
Packit 34410b
Packit 34410b
	bt_log_close();
Packit 34410b
Packit 34410b
	g_strfreev(enabled);
Packit 34410b
}