Log.cxx 3.98 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 23 24
#include "ConfigData.hxx"
#include "ConfigGlobal.hxx"
#include "ConfigOption.hxx"
25 26
#include "system/fd_util.h"
#include "system/FatalError.hxx"
27 28
#include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
29 30
#include "util/Error.hxx"
#include "util/Domain.hxx"
31
#include "system/FatalError.hxx"
32

33 34
#include <glib.h>

35 36 37 38 39
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
Max Kellermann's avatar
Max Kellermann committed
40 41 42 43 44
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
45

46 47 48 49 50 51
static GLogLevelFlags
ToGLib(LogLevel level)
{
	switch (level) {
	case LogLevel::DEBUG:
		return G_LOG_LEVEL_DEBUG;
Max Kellermann's avatar
Max Kellermann committed
52

53
	case LogLevel::INFO:
54 55 56
		return G_LOG_LEVEL_INFO;

	case LogLevel::DEFAULT:
57
		return G_LOG_LEVEL_MESSAGE;
58

59 60 61 62
	case LogLevel::WARNING:
	case LogLevel::ERROR:
		return G_LOG_LEVEL_WARNING;
	}
Avuton Olrich's avatar
Avuton Olrich committed
63

64 65
	assert(false);
	gcc_unreachable();
Eric Wong's avatar
Eric Wong committed
66
}
Avuton Olrich's avatar
Avuton Olrich committed
67

68 69
void
Log(const Domain &domain, LogLevel level, const char *msg)
Eric Wong's avatar
Eric Wong committed
70
{
71
	g_log(domain.GetName(), ToGLib(level), "%s", msg);
Warren Dukes's avatar
Warren Dukes committed
72
}
73

74 75
void
LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap)
76
{
77
	g_logv(domain.GetName(), ToGLib(level), fmt, ap);
78 79
}

80 81
void
LogFormat(const Domain &domain, LogLevel level, const char *fmt, ...)
82
{
83 84 85 86
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, level, fmt, ap);
	va_end(ap);
87 88
}

89 90
void
FormatDebug(const Domain &domain, const char *fmt, ...)
91
{
92 93 94 95
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::DEBUG, fmt, ap);
	va_end(ap);
96 97
}

98 99
void
FormatInfo(const Domain &domain, const char *fmt, ...)
100
{
101 102 103 104
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::INFO, fmt, ap);
	va_end(ap);
105 106
}

107 108 109 110 111 112 113 114 115
void
FormatDefault(const Domain &domain, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::DEFAULT, fmt, ap);
	va_end(ap);
}

116 117
void
FormatWarning(const Domain &domain, const char *fmt, ...)
118
{
119 120 121 122
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::WARNING, fmt, ap);
	va_end(ap);
123 124
}

125 126
void
FormatError(const Domain &domain, const char *fmt, ...)
Max Kellermann's avatar
Max Kellermann committed
127
{
128 129 130 131
	va_list ap;
	va_start(ap, fmt);
	LogFormatV(domain, LogLevel::ERROR, fmt, ap);
	va_end(ap);
Max Kellermann's avatar
Max Kellermann committed
132 133
}

134 135
void
LogError(const Error &error)
Max Kellermann's avatar
Max Kellermann committed
136
{
137
	Log(error.GetDomain(), LogLevel::ERROR, error.GetMessage());
Max Kellermann's avatar
Max Kellermann committed
138 139
}

140 141
void
LogError(const Error &error, const char *msg)
Max Kellermann's avatar
Max Kellermann committed
142
{
143 144
	LogFormat(error.GetDomain(), LogLevel::ERROR, "%s: %s",
		  msg, error.GetMessage());
Max Kellermann's avatar
Max Kellermann committed
145 146
}

147 148
void
FormatError(const Error &error, const char *fmt, ...)
149
{
150 151 152 153 154 155 156
	char msg[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);

	LogError(error, msg);
157 158
}

159
void
160
LogErrno(const Domain &domain, int e, const char *msg)
161
{
162
	LogFormat(domain, LogLevel::ERROR, "%s: %s", msg, g_strerror(e));
163 164
}

165 166
void
LogErrno(const Domain &domain, const char *msg)
Eric Wong's avatar
Eric Wong committed
167
{
168
	LogErrno(domain, errno, msg);
Eric Wong's avatar
Eric Wong committed
169 170
}

171
static void
172
FormatErrnoV(const Domain &domain, int e, const char *fmt, va_list ap)
173
{
174 175
	char msg[1024];
	vsnprintf(msg, sizeof(msg), fmt, ap);
176

177
	LogErrno(domain, e, msg);
178 179 180
}

void
181
FormatErrno(const Domain &domain, int e, const char *fmt, ...)
182
{
183 184 185 186
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
187 188
}

189 190
void
FormatErrno(const Domain &domain, const char *fmt, ...)
Eric Wong's avatar
Eric Wong committed
191
{
192
	const int e = errno;
Eric Wong's avatar
Eric Wong committed
193

194 195 196 197
	va_list ap;
	va_start(ap, fmt);
	FormatErrnoV(domain, e, fmt, ap);
	va_end(ap);
Eric Wong's avatar
Eric Wong committed
198
}