Log.hxx 3.52 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 21
#ifndef MPD_LOG_HXX
#define MPD_LOG_HXX
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "LogLevel.hxx"
24

25
#include <fmt/core.h>
26
#if FMT_VERSION < 70000 || FMT_VERSION >= 80000
27 28 29
#include <fmt/format.h>
#endif

30
#include <exception>
31
#include <string_view>
32
#include <utility>
33

34 35 36
class Domain;

void
37
Log(LogLevel level, const Domain &domain, std::string_view msg) noexcept;
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
void
LogVFmt(LogLevel level, const Domain &domain,
	fmt::string_view format_str, fmt::format_args args) noexcept;

template<typename S, typename... Args>
void
LogFmt(LogLevel level, const Domain &domain,
       const S &format_str, Args&&... args) noexcept
{
#if FMT_VERSION >= 70000
	return LogVFmt(level, domain, fmt::to_string_view(format_str),
		       fmt::make_args_checked<Args...>(format_str,
						       args...));
#else
	/* expensive fallback for older libfmt versions */
	const auto result = fmt::format(format_str, args...);
	return Log(level, domain, result);
#endif
}

template<typename S, typename... Args>
void
FmtDebug(const Domain &domain,
	 const S &format_str, Args&&... args) noexcept
{
	LogFmt(LogLevel::DEBUG, domain, format_str, args...);
}

template<typename S, typename... Args>
void
FmtInfo(const Domain &domain,
	const S &format_str, Args&&... args) noexcept
{
	LogFmt(LogLevel::INFO, domain, format_str, args...);
}

template<typename S, typename... Args>
void
FmtNotice(const Domain &domain,
	  const S &format_str, Args&&... args) noexcept
{
	LogFmt(LogLevel::NOTICE, domain, format_str, args...);
}

template<typename S, typename... Args>
void
FmtWarning(const Domain &domain,
	   const S &format_str, Args&&... args) noexcept
{
	LogFmt(LogLevel::WARNING, domain, format_str, args...);
}

template<typename S, typename... Args>
void
FmtError(const Domain &domain,
	 const S &format_str, Args&&... args) noexcept
{
	LogFmt(LogLevel::ERROR, domain, format_str, args...);
}

99 100 101 102 103 104
void
Log(LogLevel level, const std::exception_ptr &ep) noexcept;

void
Log(LogLevel level, const std::exception_ptr &ep, const char *msg) noexcept;

105
static inline void
Max Kellermann's avatar
Max Kellermann committed
106
LogDebug(const Domain &domain, const char *msg) noexcept
107
{
108
	Log(LogLevel::DEBUG, domain, msg);
109 110 111
}

static inline void
Max Kellermann's avatar
Max Kellermann committed
112
LogInfo(const Domain &domain, const char *msg) noexcept
113
{
114
	Log(LogLevel::INFO, domain, msg);
115 116
}

117
static inline void
118
LogNotice(const Domain &domain, const char *msg) noexcept
119
{
120
	Log(LogLevel::NOTICE, domain, msg);
121 122
}

123
static inline void
Max Kellermann's avatar
Max Kellermann committed
124
LogWarning(const Domain &domain, const char *msg) noexcept
125
{
126
	Log(LogLevel::WARNING, domain, msg);
127 128 129
}

static inline void
Max Kellermann's avatar
Max Kellermann committed
130
LogError(const Domain &domain, const char *msg) noexcept
131
{
132
	Log(LogLevel::ERROR, domain, msg);
133 134
}

135 136 137 138 139
inline void
LogError(const std::exception_ptr &ep) noexcept
{
	Log(LogLevel::ERROR, ep);
}
140

141 142 143 144 145
inline void
LogError(const std::exception_ptr &ep, const char *msg) noexcept
{
	Log(LogLevel::ERROR, ep, msg);
}
146

Eric Wong's avatar
Eric Wong committed
147
#endif /* LOG_H */