libcruft-util/log/log.hpp

82 lines
2.7 KiB
C++
Raw Normal View History

2019-10-10 15:10:41 +11:00
/*
* 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"
2019-10-10 15:10:41 +11:00
2019-10-10 16:02:10 +11:00
#include <memory>
2019-10-10 15:10:41 +11:00
#include <string>
namespace cruft::log {
///////////////////////////////////////////////////////////////////////////
sink::base& default_sink (void);
2019-10-10 16:02:10 +11:00
sink::base& default_sink (std::unique_ptr<sink::base>);
2019-10-10 15:10:41 +11:00
2019-10-10 16:02:10 +11:00
//-------------------------------------------------------------------------
2019-10-10 15:10:41 +11:00
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)...
2019-10-10 15:10:41 +11:00
)
);
}
2021-04-14 10:23:33 +10:00
}
2019-10-10 15:10:41 +11:00
2021-04-14 10:23:33 +10:00
//-------------------------------------------------------------------------
// 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__)
2019-10-10 15:10:41 +11:00
#if !defined(NDEBUG)
2021-04-14 10:23:33 +10:00
#define LOG_DEBUG(FMT, ...) LOG(::cruft::log::DEBUG, FMT __VA_OPT__(,) __VA_ARGS__)
2019-10-10 15:10:41 +11:00
#else
2021-04-14 10:23:33 +10:00
#define LOG_DEBUG(...) do { ; } while (0)
2019-10-10 15:10:41 +11:00
#endif
2021-04-14 10:23:33 +10:00
#define LOG_WARN(...) LOG_WARNING(__VA_ARGS__)