Log.cxx 3.62 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * 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
#include "config.h"
21
#include "LogV.hxx"
22 23
#include "util/Error.hxx"
#include "util/Domain.hxx"
24

25 26
#include <glib.h>

27
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
28 29
#include <stdio.h>
#include <errno.h>
30

31 32 33 34 35 36
static GLogLevelFlags
ToGLib(LogLevel level)
{
	switch (level) {
	case LogLevel::DEBUG:
		return G_LOG_LEVEL_DEBUG;
Max Kellermann's avatar
Max Kellermann committed
37

38
	case LogLevel::INFO:
39 40 41
		return G_LOG_LEVEL_INFO;

	case LogLevel::DEFAULT:
42
		return G_LOG_LEVEL_MESSAGE;
43

44 45 46 47
	case LogLevel::WARNING:
	case LogLevel::ERROR:
		return G_LOG_LEVEL_WARNING;
	}
Avuton Olrich's avatar
Avuton Olrich committed
48

49 50
	assert(false);
	gcc_unreachable();
Eric Wong's avatar
Eric Wong committed
51
}
Avuton Olrich's avatar
Avuton Olrich committed
52

53 54
void
Log(const Domain &domain, LogLevel level, const char *msg)
Eric Wong's avatar
Eric Wong committed
55
{
56
	g_log(domain.GetName(), ToGLib(level), "%s", msg);
Warren Dukes's avatar
Warren Dukes committed
57
}
58

59 60
void
LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap)
61
{
62
	g_logv(domain.GetName(), ToGLib(level), fmt, ap);
63 64
}

65 66
void
LogFormat(const Domain &domain, LogLevel level, const char *fmt, ...)
67
{
68 69 70 71
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, level, fmt, ap);
	va_end(ap);
72 73
}

74 75
void
FormatDebug(const Domain &domain, const char *fmt, ...)
76
{
77 78 79 80
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::DEBUG, fmt, ap);
	va_end(ap);
81 82
}

83 84
void
FormatInfo(const Domain &domain, const char *fmt, ...)
85
{
86 87 88 89
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::INFO, fmt, ap);
	va_end(ap);
90 91
}

92 93 94 95 96 97 98 99 100
void
FormatDefault(const Domain &domain, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::DEFAULT, fmt, ap);
	va_end(ap);
}

101 102
void
FormatWarning(const Domain &domain, const char *fmt, ...)
103
{
104 105 106 107
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::WARNING, fmt, ap);
	va_end(ap);
108 109
}

110 111
void
FormatError(const Domain &domain, const char *fmt, ...)
Max Kellermann's avatar
Max Kellermann committed
112
{
113 114 115 116
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::ERROR, fmt, ap);
	va_end(ap);
Max Kellermann's avatar
Max Kellermann committed
117 118
}

119 120
void
LogError(const Error &error)
Max Kellermann's avatar
Max Kellermann committed
121
{
122
	Log(error.GetDomain(), LogLevel::ERROR, error.GetMessage());
Max Kellermann's avatar
Max Kellermann committed
123 124
}

125 126
void
LogError(const Error &error, const char *msg)
Max Kellermann's avatar
Max Kellermann committed
127
{
128 129
	LogFormat(error.GetDomain(), LogLevel::ERROR, "%s: %s",
		  msg, error.GetMessage());
Max Kellermann's avatar
Max Kellermann committed
130 131
}

132 133
void
FormatError(const Error &error, const char *fmt, ...)
134
{
135 136 137 138 139 140 141
	char msg[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

	LogError(error, msg);
142 143
}

144
void
145
LogErrno(const Domain &domain, int e, const char *msg)
146
{
147
	LogFormat(domain, LogLevel::ERROR, "%s: %s", msg, g_strerror(e));
148 149
}

150 151
void
LogErrno(const Domain &domain, const char *msg)
Eric Wong's avatar
Eric Wong committed
152
{
153
	LogErrno(domain, errno, msg);
Eric Wong's avatar
Eric Wong committed
154 155
}

156
static void
157
FormatErrnoV(const Domain &domain, int e, const char *fmt, va_list ap)
158
{
159 160
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
161

162
	LogErrno(domain, e, msg);
163 164 165
}

void
166
FormatErrno(const Domain &domain, int e, const char *fmt, ...)
167
{
168 169 170 171
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
172 173
}

174 175
void
FormatErrno(const Domain &domain, const char *fmt, ...)
Eric Wong's avatar
Eric Wong committed
176
{
177
	const int e = errno;
Eric Wong's avatar
Eric Wong committed
178

179 180 181 182
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
Eric Wong's avatar
Eric Wong committed
183
}