Log.cxx 3.77 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/Error.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
void
LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap)
32
{
33 34 35
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
	Log(domain, level, msg);
36 37
}

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

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

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

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

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

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

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
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");
	}
}

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

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

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

	LogError(error, msg);
145 146
}

147
void
148
LogErrno(const Domain &domain, int e, const char *msg)
149
{
150
	LogFormat(domain, LogLevel::ERROR, "%s: %s", msg, strerror(e));
151 152
}

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

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

165
	LogErrno(domain, e, msg);
166 167 168
}

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

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

182 183 184 185
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
Eric Wong's avatar
Eric Wong committed
186
}