Log.cxx 4.26 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
#include "util/Domain.hxx"
23 24

#include <exception>
25

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

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

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

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

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

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

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

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

85 86
void
FormatError(const Domain &domain, const char *fmt, ...)
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 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
void
LogError(const std::exception &e)
{
	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
LogError(const std::exception &e, const char *msg)
{
	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 125 126 127 128 129 130 131 132 133 134 135
void
FormatError(const std::exception &e, const char *fmt, ...)
{
	char msg[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

	LogError(e, msg);
}

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
void
LogError(const std::exception_ptr &ep)
{
	try {
		std::rethrow_exception(ep);
	} catch (const std::exception &e) {
		LogError(e);
	} catch (...) {
		Log(exception_domain, LogLevel::ERROR,
		    "Unrecognized exception");
	}
}

void
LogError(const std::exception_ptr &ep, const char *msg)
{
	try {
		std::rethrow_exception(ep);
	} catch (const std::exception &e) {
		LogError(e, msg);
	} catch (...) {
		FormatError(exception_domain,
			    "%s: Unrecognized exception", msg);
	}
}

void
FormatError(const std::exception_ptr &ep, const char *fmt, ...)
{
	char msg[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

	LogError(ep, msg);
}

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

180 181
void
LogErrno(const Domain &domain, const char *msg)
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
187
FormatErrnoV(const Domain &domain, int e, const char *fmt, va_list ap)
188
{
189 190
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
191

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

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

204 205
void
FormatErrno(const Domain &domain, const char *fmt, ...)
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
}