Log.cxx 4.4 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "LogV.hxx"
21
#include "util/Domain.hxx"
22 23

#include <exception>
24

Max Kellermann's avatar
Max Kellermann committed
25
#include <stdio.h>
26
#include <string.h>
Max Kellermann's avatar
Max Kellermann committed
27
#include <errno.h>
28

29 30
static constexpr Domain exception_domain("exception");

31
void
Max Kellermann's avatar
Max Kellermann committed
32 33
LogFormatV(const Domain &domain, LogLevel level,
	   const char *fmt, va_list ap) noexcept
34
{
35 36 37
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
	Log(domain, level, msg);
38 39
}

40
void
Max Kellermann's avatar
Max Kellermann committed
41
LogFormat(const Domain &domain, LogLevel level, const char *fmt, ...) noexcept
42
{
43 44 45 46
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, level, fmt, ap);
	va_end(ap);
47 48
}

49
void
Max Kellermann's avatar
Max Kellermann committed
50
FormatDebug(const Domain &domain, const char *fmt, ...) noexcept
51
{
52 53 54 55
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::DEBUG, fmt, ap);
	va_end(ap);
56 57
}

58
void
Max Kellermann's avatar
Max Kellermann committed
59
FormatInfo(const Domain &domain, const char *fmt, ...) noexcept
60
{
61 62 63 64
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::INFO, fmt, ap);
	va_end(ap);
65 66
}

67
void
Max Kellermann's avatar
Max Kellermann committed
68
FormatDefault(const Domain &domain, const char *fmt, ...) noexcept
69 70 71 72 73 74 75
{
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::DEFAULT, fmt, ap);
	va_end(ap);
}

76
void
Max Kellermann's avatar
Max Kellermann committed
77
FormatWarning(const Domain &domain, const char *fmt, ...) noexcept
78
{
79 80 81 82
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::WARNING, fmt, ap);
	va_end(ap);
83 84
}

85
void
Max Kellermann's avatar
Max Kellermann committed
86
FormatError(const Domain &domain, const char *fmt, ...) noexcept
Max Kellermann's avatar
Max Kellermann committed
87
{
88 89 90 91
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::ERROR, fmt, ap);
	va_end(ap);
Max Kellermann's avatar
Max Kellermann committed
92 93
}

94
void
Max Kellermann's avatar
Max Kellermann committed
95
LogError(const std::exception &e) noexcept
96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
	Log(exception_domain, LogLevel::ERROR, e.what());

	try {
		std::rethrow_if_nested(e);
	} catch (const std::exception &nested) {
		LogError(nested, "nested");
	} catch (...) {
		Log(exception_domain, LogLevel::ERROR,
		    "Unrecognized nested exception");
	}
}

void
Max Kellermann's avatar
Max Kellermann committed
110
LogError(const std::exception &e, const char *msg) noexcept
111 112 113 114 115 116 117 118 119 120 121 122 123
{
	FormatError(exception_domain, "%s: %s", msg, e.what());

	try {
		std::rethrow_if_nested(e);
	} catch (const std::exception &nested) {
		LogError(nested);
	} catch (...) {
		Log(exception_domain, LogLevel::ERROR,
		    "Unrecognized nested exception");
	}
}

124
void
Max Kellermann's avatar
Max Kellermann committed
125
FormatError(const std::exception &e, const char *fmt, ...) noexcept
126 127 128 129 130 131 132 133 134 135
{
	char msg[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

	LogError(e, msg);
}

136
void
Max Kellermann's avatar
Max Kellermann committed
137
LogError(const std::exception_ptr &ep) noexcept
138 139 140 141 142 143 144 145 146 147 148 149
{
	try {
		std::rethrow_exception(ep);
	} catch (const std::exception &e) {
		LogError(e);
	} catch (...) {
		Log(exception_domain, LogLevel::ERROR,
		    "Unrecognized exception");
	}
}

void
Max Kellermann's avatar
Max Kellermann committed
150
LogError(const std::exception_ptr &ep, const char *msg) noexcept
151 152 153 154 155 156 157 158 159 160 161 162
{
	try {
		std::rethrow_exception(ep);
	} catch (const std::exception &e) {
		LogError(e, msg);
	} catch (...) {
		FormatError(exception_domain,
			    "%s: Unrecognized exception", msg);
	}
}

void
Max Kellermann's avatar
Max Kellermann committed
163
FormatError(const std::exception_ptr &ep, const char *fmt, ...) noexcept
164 165 166 167 168 169 170 171 172 173
{
	char msg[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

	LogError(ep, msg);
}

174
void
Max Kellermann's avatar
Max Kellermann committed
175
LogErrno(const Domain &domain, int e, const char *msg) noexcept
176
{
177
	LogFormat(domain, LogLevel::ERROR, "%s: %s", msg, strerror(e));
178 179
}

180
void
Max Kellermann's avatar
Max Kellermann committed
181
LogErrno(const Domain &domain, const char *msg) noexcept
Eric Wong's avatar
Eric Wong committed
182
{
183
	LogErrno(domain, errno, msg);
Eric Wong's avatar
Eric Wong committed
184 185
}

186
static void
Max Kellermann's avatar
Max Kellermann committed
187
FormatErrnoV(const Domain &domain, int e, const char *fmt, va_list ap) noexcept
188
{
189 190
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
191

192
	LogErrno(domain, e, msg);
193 194 195
}

void
Max Kellermann's avatar
Max Kellermann committed
196
FormatErrno(const Domain &domain, int e, const char *fmt, ...) noexcept
197
{
198 199 200 201
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
202 203
}

204
void
Max Kellermann's avatar
Max Kellermann committed
205
FormatErrno(const Domain &domain, const char *fmt, ...) noexcept
Eric Wong's avatar
Eric Wong committed
206
{
207
	const int e = errno;
Eric Wong's avatar
Eric Wong committed
208

209 210 211 212
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
Eric Wong's avatar
Eric Wong committed
213
}