log.c 6.34 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Warren Dukes's avatar
Warren Dukes committed
18 19 20 21
 */

#include "log.h"
#include "conf.h"
Max Kellermann's avatar
Max Kellermann committed
22 23
#include "utils.h"
#include "config.h"
24 25 26 27 28 29 30

#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
Max Kellermann's avatar
Max Kellermann committed
31 32 33 34 35 36
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <glib.h>
Warren Dukes's avatar
Warren Dukes committed
37

Max Kellermann's avatar
Max Kellermann committed
38 39 40 41
#ifdef HAVE_SYSLOG
#include <syslog.h>
#endif

42 43 44
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "log"

45
#define LOG_LEVEL_SECURE G_LOG_LEVEL_INFO
46

47 48
#define LOG_DATE_BUF_SIZE 16
#define LOG_DATE_LEN (LOG_DATE_BUF_SIZE - 1)
Max Kellermann's avatar
Max Kellermann committed
49

50
static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE;
Max Kellermann's avatar
Max Kellermann committed
51

52 53
static const char *log_charset;

Max Kellermann's avatar
Max Kellermann committed
54
static bool stdout_mode = true;
55
static int out_fd;
56
static const char *out_filename;
Avuton Olrich's avatar
Avuton Olrich committed
57

58
static void redirect_logs(int fd)
Eric Wong's avatar
Eric Wong committed
59
{
60 61
	assert(fd >= 0);
	if (dup2(fd, STDOUT_FILENO) < 0)
Max Kellermann's avatar
Max Kellermann committed
62
		g_error("problems dup2 stdout : %s\n", strerror(errno));
63
	if (dup2(fd, STDERR_FILENO) < 0)
Max Kellermann's avatar
Max Kellermann committed
64
		g_error("problems dup2 stderr : %s\n", strerror(errno));
Eric Wong's avatar
Eric Wong committed
65
}
Avuton Olrich's avatar
Avuton Olrich committed
66

Eric Wong's avatar
Eric Wong committed
67 68
static const char *log_date(void)
{
69
	static char buf[LOG_DATE_BUF_SIZE];
Eric Wong's avatar
Eric Wong committed
70
	time_t t = time(NULL);
71
	strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M : ", localtime(&t));
Eric Wong's avatar
Eric Wong committed
72
	return buf;
Warren Dukes's avatar
Warren Dukes committed
73
}
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/**
 * Determines the length of the string excluding trailing whitespace
 * characters.
 */
static int
chomp_length(const char *p)
{
	size_t length = strlen(p);

	while (length > 0 && g_ascii_isspace(p[length - 1]))
		--length;

	return (int)length;
}

90
static void
91
file_log_func(const gchar *log_domain,
92
	      GLogLevelFlags log_level,
93
	      const gchar *message, G_GNUC_UNUSED gpointer user_data)
94
{
95 96
	char *converted;

97
	if (log_level > log_threshold)
98 99
		return;

100 101 102 103 104 105 106 107
	if (log_charset != NULL) {
		converted = g_convert_with_fallback(message, -1,
						    log_charset, "utf-8",
						    NULL, NULL, NULL, NULL);
		if (converted != NULL)
			message = converted;
	} else
		converted = NULL;
108

109 110 111
	if (log_domain == NULL)
		log_domain = "";

112
	fprintf(stderr, "%s%s%s%.*s\n",
113
		stdout_mode ? "" : log_date(),
114
		log_domain, *log_domain == 0 ? "" : ": ",
115
		chomp_length(message), message);
116 117

	g_free(converted);
118 119
}

120 121 122 123 124 125
static void
log_init_stdout(void)
{
	g_log_set_default_handler(file_log_func, NULL);
}

126 127 128 129 130
static int
open_log_file(void)
{
	assert(out_filename != NULL);

131
	return open(out_filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
132 133
}

134 135 136 137 138 139
static void
log_init_file(const char *path, unsigned line)
{
	out_filename = path;
	out_fd = open_log_file();
	if (out_fd < 0)
Max Kellermann's avatar
Max Kellermann committed
140 141
		g_error("problem opening log file \"%s\" (config line %u) for "
			"writing\n", path, line);
142 143 144 145

	g_log_set_default_handler(file_log_func, NULL);
}

Max Kellermann's avatar
Max Kellermann committed
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
#ifdef HAVE_SYSLOG

static int
glib_to_syslog_level(GLogLevelFlags log_level)
{
	switch (log_level & G_LOG_LEVEL_MASK) {
	case G_LOG_LEVEL_ERROR:
	case G_LOG_LEVEL_CRITICAL:
		return LOG_ERR;

	case G_LOG_LEVEL_WARNING:
		return LOG_WARNING;

	case G_LOG_LEVEL_MESSAGE:
		return LOG_NOTICE;

	case G_LOG_LEVEL_INFO:
		return LOG_INFO;

	case G_LOG_LEVEL_DEBUG:
		return LOG_DEBUG;

	default:
		return LOG_NOTICE;
	}
}

static void
syslog_log_func(const gchar *log_domain,
		GLogLevelFlags log_level, const gchar *message,
		G_GNUC_UNUSED gpointer user_data)
{
	if (stdout_mode) {
		/* fall back to the file log function during
		   startup */
		file_log_func(log_domain, log_level,
			      message, user_data);
		return;
	}

	if (log_level > log_threshold)
		return;

	if (log_domain == NULL)
		log_domain = "";

192
	syslog(glib_to_syslog_level(log_level), "%s%s%.*s",
Max Kellermann's avatar
Max Kellermann committed
193
	       log_domain, *log_domain == 0 ? "" : ": ",
194
	       chomp_length(message), message);
Max Kellermann's avatar
Max Kellermann committed
195 196 197 198 199 200 201 202 203 204 205 206 207
}

static void
log_init_syslog(void)
{
	assert(out_filename == NULL);

	openlog(PACKAGE, 0, LOG_DAEMON);
	g_log_set_default_handler(syslog_log_func, NULL);
}

#endif

208 209 210 211 212 213 214 215 216
static inline GLogLevelFlags
parse_log_level(const char *value, unsigned line)
{
	if (0 == strcmp(value, "default"))
		return G_LOG_LEVEL_MESSAGE;
	if (0 == strcmp(value, "secure"))
		return LOG_LEVEL_SECURE;
	else if (0 == strcmp(value, "verbose"))
		return G_LOG_LEVEL_DEBUG;
217
	else {
Max Kellermann's avatar
Max Kellermann committed
218 219
		g_error("unknown log level \"%s\" at line %u\n",
			value, line);
220 221
		return G_LOG_LEVEL_MESSAGE;
	}
222 223
}

224 225 226 227 228 229 230 231 232
void
log_early_init(bool verbose)
{
	if (verbose)
		log_threshold = G_LOG_LEVEL_DEBUG;

	log_init_stdout();
}

233
void log_init(bool verbose, bool use_stdout)
Eric Wong's avatar
Eric Wong committed
234
{
235
	const struct config_param *param;
Eric Wong's avatar
Eric Wong committed
236

237 238
	g_get_charset(&log_charset);

239
	if (verbose)
240
		log_threshold = G_LOG_LEVEL_DEBUG;
241
	else if ((param = config_get_param(CONF_LOG_LEVEL)) != NULL)
242
		log_threshold = parse_log_level(param->value, param->line);
Eric Wong's avatar
Eric Wong committed
243

244 245 246
	if (use_stdout) {
		log_init_stdout();
	} else {
247
		param = config_get_param(CONF_LOG_FILE);
Max Kellermann's avatar
Max Kellermann committed
248 249 250 251 252 253
		if (param == NULL) {
#ifdef HAVE_SYSLOG
			/* no configuration: default to syslog (if
			   available) */
			log_init_syslog();
#else
Max Kellermann's avatar
Max Kellermann committed
254 255
			g_error("config parameter \"%s\" not found\n",
				CONF_LOG_FILE);
Max Kellermann's avatar
Max Kellermann committed
256 257 258 259 260 261
#endif
#ifdef HAVE_SYSLOG
		} else if (strcmp(param->value, "syslog") == 0) {
			log_init_syslog();
#endif
		} else {
262 263
			const char *path = config_get_path(CONF_LOG_FILE);
			assert(path != NULL);
Max Kellermann's avatar
Max Kellermann committed
264

265
			log_init_file(path, param->line);
Max Kellermann's avatar
Max Kellermann committed
266
		}
267
	}
Eric Wong's avatar
Eric Wong committed
268 269
}

Max Kellermann's avatar
Max Kellermann committed
270
void setup_log_output(bool use_stdout)
Eric Wong's avatar
Eric Wong committed
271 272 273
{
	fflush(NULL);
	if (!use_stdout) {
274 275 276 277 278
		if (out_filename != NULL) {
			redirect_logs(out_fd);
			close(out_fd);
		}

Max Kellermann's avatar
Max Kellermann committed
279
		stdout_mode = false;
280
		log_charset = NULL;
Eric Wong's avatar
Eric Wong committed
281 282 283 284 285
	}
}

int cycle_log_files(void)
{
286 287
	int fd;

Max Kellermann's avatar
Max Kellermann committed
288
	if (stdout_mode || out_filename == NULL)
Eric Wong's avatar
Eric Wong committed
289 290 291
		return 0;
	assert(out_filename);

Max Kellermann's avatar
Max Kellermann committed
292
	g_debug("Cycling log files...\n");
Eric Wong's avatar
Eric Wong committed
293 294
	close_log_files();

295 296
	fd = open_log_file();
	if (fd < 0) {
Max Kellermann's avatar
Max Kellermann committed
297
		g_warning("error re-opening log file: %s\n", out_filename);
Eric Wong's avatar
Eric Wong committed
298 299 300
		return -1;
	}

301
	redirect_logs(fd);
Max Kellermann's avatar
Max Kellermann committed
302
	g_debug("Done cycling log files\n");
Eric Wong's avatar
Eric Wong committed
303 304 305 306 307 308 309
	return 0;
}

void close_log_files(void)
{
	if (stdout_mode)
		return;
Max Kellermann's avatar
Max Kellermann committed
310

311
#ifdef HAVE_SYSLOG
Max Kellermann's avatar
Max Kellermann committed
312 313
	if (out_filename == NULL)
		closelog();
314
#endif
Eric Wong's avatar
Eric Wong committed
315 316
}