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

Rosen Penev's avatar
Rosen Penev committed
24 25
#include <cerrno>

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

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

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

40
void
41
LogFormat(LogLevel level, const Domain &domain, const char *fmt, ...) noexcept
42
{
43
	std::va_list ap;
44
	va_start(ap, fmt);
45
	LogFormatV(level, domain, fmt, ap);
46
	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
	std::va_list ap;
53
	va_start(ap, fmt);
54
	LogFormatV(LogLevel::DEBUG, domain, fmt, ap);
55
	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
	std::va_list ap;
62
	va_start(ap, fmt);
63
	LogFormatV(LogLevel::INFO, domain, fmt, ap);
64
	va_end(ap);
65 66
}

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

76
void
Max Kellermann's avatar
Max Kellermann committed
77
FormatWarning(const Domain &domain, const char *fmt, ...) noexcept
78
{
79
	std::va_list ap;
80
	va_start(ap, fmt);
81
	LogFormatV(LogLevel::WARNING, domain, fmt, ap);
82
	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
	std::va_list ap;
89
	va_start(ap, fmt);
90
	LogFormatV(LogLevel::ERROR, domain, fmt, ap);
91
	va_end(ap);
Max Kellermann's avatar
Max Kellermann committed
92 93
}

94
void
95
Log(LogLevel level, const std::exception &e) noexcept
96
{
97
	Log(level, exception_domain, GetFullMessage(e).c_str());
98 99 100
}

void
101
Log(LogLevel level, const std::exception &e, const char *msg) noexcept
102
{
103
	LogFormat(level, exception_domain, "%s: %s", msg, GetFullMessage(e).c_str());
104 105
}

106
void
107
LogFormat(LogLevel level, const std::exception &e, const char *fmt, ...) noexcept
108 109
{
	char msg[1024];
110
	std::va_list ap;
111 112 113 114
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

115
	Log(level, e, msg);
116 117
}

118
void
119
Log(LogLevel level, const std::exception_ptr &ep) noexcept
120
{
121
	Log(level, exception_domain, GetFullMessage(ep).c_str());
122 123 124
}

void
125
Log(LogLevel level, const std::exception_ptr &ep, const char *msg) noexcept
126
{
127 128
	LogFormat(level, exception_domain, "%s: %s", msg,
		  GetFullMessage(ep).c_str());
129 130 131
}

void
132
LogFormat(LogLevel level, const std::exception_ptr &ep, const char *fmt, ...) noexcept
133 134
{
	char msg[1024];
135
	std::va_list ap;
136 137 138 139
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

140
	Log(level, ep, msg);
141 142
}

143
void
Max Kellermann's avatar
Max Kellermann committed
144
LogErrno(const Domain &domain, int e, const char *msg) noexcept
145
{
146
	LogFormat(LogLevel::ERROR, domain, "%s: %s", msg, strerror(e));
147 148
}

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

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

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

void
Max Kellermann's avatar
Max Kellermann committed
165
FormatErrno(const Domain &domain, int e, const char *fmt, ...) noexcept
166
{
167
	std::va_list ap;
168 169 170
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
171 172
}

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

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