libcruft-util/log/log.hpp

82 lines
2.7 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2012-2019 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "level.hpp"
#include "packet.hpp"
#include "sink/base.hpp"
#include <memory>
#include <string>
namespace cruft::log {
///////////////////////////////////////////////////////////////////////////
sink::base& default_sink (void);
sink::base& default_sink (std::unique_ptr<sink::base>);
//-------------------------------------------------------------------------
void write (level_t, std::string const &msg);
void write (level_t, char const *msg);
void write (level_t, std::string_view msg);
//-------------------------------------------------------------------------
template <typename FormatT, typename ...ArgsT>
void
write (level_t l, FormatT fmt, ArgsT &&...args)
{
default_sink ().write (
packet (
l,
std::forward<FormatT> (fmt),
std::forward<ArgsT> (args)...
)
);
}
}
//-------------------------------------------------------------------------
// Various convenience macros for logging specific strings with a well
// known severity.
//
// The format string _must_ be a compile time literal so that compile time
// checking of strings and arguments is possible.
//
// LOG_DEBUG is treated similarly to assert; if NDEBUG is defined then we
// compile out the statement so as to gain a little runtime efficiency.
#define LOG(LEVEL, FMT, ...) \
do { \
::cruft::log::write ( \
(LEVEL), \
FMT_STRING(FMT) \
__VA_OPT__(,) \
__VA_ARGS__ \
); \
} while (0)
#define LOG_EMERGENCY(FMT, ...) LOG(::cruft::log::EMERGENCY, FMT __VA_OPT__(,) __VA_ARGS__)
#define LOG_ALERT(FMT, ...) LOG(::cruft::log::ALERT, FMT __VA_OPT__(,) __VA_ARGS__)
#define LOG_CRITICAL(FMT, ...) LOG(::cruft::log::CRITICAL, FMT __VA_OPT__(,) __VA_ARGS__)
#define LOG_ERROR(FMT, ...) LOG(::cruft::log::ERROR, FMT __VA_OPT__(,) __VA_ARGS__)
#define LOG_WARNING(FMT, ...) LOG(::cruft::log::WARNING, FMT __VA_OPT__(,) __VA_ARGS__)
#define LOG_NOTICE(FMT, ...) LOG(::cruft::log::NOTICE, FMT __VA_OPT__(,) __VA_ARGS__)
#define LOG_INFO(FMT, ...) LOG(::cruft::log::INFO, FMT __VA_OPT__(,) __VA_ARGS__)
#if !defined(NDEBUG)
#define LOG_DEBUG(FMT, ...) LOG(::cruft::log::DEBUG, FMT __VA_OPT__(,) __VA_ARGS__)
#else
#define LOG_DEBUG(...) do { ; } while (0)
#endif
#define LOG_WARN(...) LOG_WARNING(__VA_ARGS__)