Log.cxx 3.17 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 <assert.h>
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
void
LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap)
31
{
32 33 34
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
	Log(domain, level, msg);
35 36
}

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

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

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

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

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

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

91 92
void
LogError(const Error &error)
Max Kellermann's avatar
Max Kellermann committed
93
{
94
	Log(error.GetDomain(), LogLevel::ERROR, error.GetMessage());
Max Kellermann's avatar
Max Kellermann committed
95 96
}

97 98
void
LogError(const Error &error, const char *msg)
Max Kellermann's avatar
Max Kellermann committed
99
{
100 101
	LogFormat(error.GetDomain(), LogLevel::ERROR, "%s: %s",
		  msg, error.GetMessage());
Max Kellermann's avatar
Max Kellermann committed
102 103
}

104 105
void
FormatError(const Error &error, const char *fmt, ...)
106
{
107 108 109 110 111 112 113
	char msg[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

	LogError(error, msg);
114 115
}

116
void
117
LogErrno(const Domain &domain, int e, const char *msg)
118
{
119
	LogFormat(domain, LogLevel::ERROR, "%s: %s", msg, strerror(e));
120 121
}

122 123
void
LogErrno(const Domain &domain, const char *msg)
Eric Wong's avatar
Eric Wong committed
124
{
125
	LogErrno(domain, errno, msg);
Eric Wong's avatar
Eric Wong committed
126 127
}

128
static void
129
FormatErrnoV(const Domain &domain, int e, const char *fmt, va_list ap)
130
{
131 132
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
133

134
	LogErrno(domain, e, msg);
135 136 137
}

void
138
FormatErrno(const Domain &domain, int e, const char *fmt, ...)
139
{
140 141 142 143
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
144 145
}

146 147
void
FormatErrno(const Domain &domain, const char *fmt, ...)
Eric Wong's avatar
Eric Wong committed
148
{
149
	const int e = errno;
Eric Wong's avatar
Eric Wong committed
150

151 152 153 154
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
Eric Wong's avatar
Eric Wong committed
155
}