35#include <fmt/format.h>
48 static constexpr const char* IFW_FND_LOGGER_NAME =
"ifwfnd";
101 virtual void Throw(
const std::string& message);
191 template <
typename... Args>
192 inline std::string
TraceExtra(fmt::string_view fmt_str, Args&&... args) {
193 return ": " + fmt::vformat(fmt_str, fmt::make_format_args(args...));
216 std::string extra)
noexcept;
225 const std::chrono::steady_clock::time_point m_entry;
226 const std::string m_location;
227 const std::string m_extra;
250#define FND_LOG_IMPL_(level_, fmt_str_, ...) \
252 auto& _fnd_log = ::ifw::fnd::Log(); \
253 if (_fnd_log.GetLogLevel() <= (level_)) { \
254 _fnd_log.Emit((level_), \
257 ::fmt::runtime(std::string(fmt_str_)) \
258 __VA_OPT__(,) __VA_ARGS__)); \
287#define FND_TRACE_NAME_(line) _fnd_trace_##line
288#define FND_TRACE_NAME(line) FND_TRACE_NAME_(line)
290#define FNDTRACE(...) \
291 [[maybe_unused]] ::ifw::fnd::ScopedTracer FND_TRACE_NAME(__LINE__) { \
292 (::ifw::fnd::HasLogger() && \
293 ::ifw::fnd::Log().GetLogLevel() <= ::ifw::fnd::LogLevel::TRACE) \
294 ? (FNDLOC + ": " + std::string(__PRETTY_FUNCTION__)) \
296 (::ifw::fnd::HasLogger() && \
297 ::ifw::fnd::Log().GetLogLevel() <= ::ifw::fnd::LogLevel::TRACE) \
298 ? ::ifw::fnd::TraceExtra(__VA_ARGS__) \
302#define FNDDEBUG(fmt_str, ...) \
303 FND_LOG_IMPL_(::ifw::fnd::LogLevel::DEBUG, fmt_str __VA_OPT__(,) __VA_ARGS__)
305#define FNDINFO(fmt_str, ...) \
306 FND_LOG_IMPL_(::ifw::fnd::LogLevel::INFO, fmt_str __VA_OPT__(,) __VA_ARGS__)
308#define FNDWARNING(fmt_str, ...) \
309 FND_LOG_IMPL_(::ifw::fnd::LogLevel::WARNING, fmt_str __VA_OPT__(,) __VA_ARGS__)
311#define FNDERROR(fmt_str, ...) \
312 FND_LOG_IMPL_(::ifw::fnd::LogLevel::ERROR, fmt_str __VA_OPT__(,) __VA_ARGS__)
314#define FNDTHROW(fmt_str, ...) \
316 ::ifw::fnd::Log().Throw( \
319 ::fmt::runtime(std::string(fmt_str)) \
320 __VA_OPT__(,) __VA_ARGS__)); \
Logger(const Logger &)=delete
void Error(const std::string &m)
Definition logger.hpp:139
void Debug(const std::string &m)
Definition logger.hpp:136
void Info(const std::string &m)
Definition logger.hpp:137
void SetLogLevel(LogLevel level) noexcept
Definition logger.hpp:127
void Warning(const std::string &m)
Definition logger.hpp:138
Logger & operator=(Logger &&)=delete
LogLevel m_level
Definition logger.hpp:143
virtual ~Logger()=default
void Trace(const std::string &m)
Definition logger.hpp:135
virtual std::vector< std::string > ListSources() const
Enumerate the named log sources this backend knows about.
Definition logger.hpp:123
virtual void Throw(const std::string &message)
Log at error level and then throw.
Definition defs.cpp:110
virtual void Emit(LogLevel level, const std::string &message)=0
Deliver one already-formatted log line.
LogLevel GetLogLevel() const noexcept
Definition logger.hpp:128
Logger & operator=(const Logger &)=delete
ScopedTracer & operator=(ScopedTracer &&)=delete
ScopedTracer(std::string location, std::string extra) noexcept
Definition defs.cpp:171
ScopedTracer & operator=(const ScopedTracer &)=delete
ScopedTracer(const ScopedTracer &)=delete
ScopedTracer(ScopedTracer &&)=delete
~ScopedTracer()
Definition defs.cpp:189
This source file contains definitions of the types and constants to handle data types.
Definition defs.cpp:28
LogLevel
Log severity, lowest to highest.
Definition logger.hpp:55
@ WARNING
Definition logger.hpp:59
@ TRACE
Definition logger.hpp:56
@ INFO
Definition logger.hpp:58
@ OFF
Definition logger.hpp:61
@ ERROR
Definition logger.hpp:60
@ DEBUG
Definition logger.hpp:57
std::string TraceExtra()
Helper used by FNDTRACE.
Definition logger.hpp:188
std::unique_ptr< Logger > MakeStdoutLogger()
Factory: a logger that prints to stdout.
Definition defs.cpp:135
Logger & Log()
Access the installed logger.
Definition defs.cpp:120
bool HasLogger() noexcept
true iff a logger has been installed.
Definition defs.cpp:131
std::unique_ptr< Logger > MakeNullLogger()
Factory: a logger that discards everything.
Definition defs.cpp:139
void InstallLogger(std::unique_ptr< Logger > logger) noexcept
Install the process-wide logger.
Definition defs.cpp:115
std::string_view LogLevelName(LogLevel level) noexcept
Render a LogLevel as a short uppercase string.
Definition defs.cpp:143