libcruft-util/log.hpp

139 lines
4.8 KiB
C++
Raw Normal View History

2012-04-24 17:38:35 +10:00
/*
2018-08-04 15:14:06 +10: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/.
2012-04-24 17:38:35 +10:00
*
* Copyright 2012-2018 Danny Robson <danny@nerdcruft.net>
2012-04-24 17:38:35 +10:00
*/
#ifndef CRUFT_UTIL_LOG_HPP
#define CRUFT_UTIL_LOG_HPP
2012-04-24 17:38:35 +10:00
#include "nocopy.hpp"
#include "preprocessor.hpp"
#include "format.hpp"
2013-03-05 21:38:51 +11:00
2018-03-22 16:10:06 +11:00
#include <iosfwd>
2012-04-24 17:38:35 +10:00
#include <string>
// Windows.h or one of its friends defines a macro 'ERROR'. Screw Microsoft.
#ifdef ERROR
#undef ERROR
#endif
namespace cruft {
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////
2012-04-24 17:38:35 +10:00
// rfc5424 log levels. It is assumed they are contiguous to simplify array
// indexing in logging code.
enum level_t {
EMERGENCY, /** system is unusable */
ALERT, /** action must be taken immediately */
CRITICAL, /** critical conditions */
ERROR, /** error conditions */
2015-04-13 18:06:08 +10:00
WARNING,
2012-04-26 18:20:46 +10:00
WARN = WARNING, /** warning conditions */
2012-04-24 17:38:35 +10:00
NOTICE, /** normal but significant condition */
INFORMATIONAL,
INFO = INFORMATIONAL, /** informational messages */
DEBUG /** debug-level messages */
2012-04-24 17:38:35 +10:00
};
#define MAP_LEVEL_T(F) MAP0(F, EMERGENCY, ALERT, CRITICAL, ERROR, WARN, NOTICE, INFO, DEBUG)
2016-03-21 14:21:14 +11:00
constexpr auto DEFAULT_LOG_LEVEL = NOTICE;
2016-03-21 14:21:14 +11:00
//-------------------------------------------------------------------------
const std::string& to_string (level_t);
2015-07-23 17:37:02 +10:00
//-------------------------------------------------------------------------
2012-04-24 17:38:35 +10:00
std::ostream&
operator<< (std::ostream&, level_t);
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////
// Query or set the global logging level filter. If the severity passed to
// a logging call is less than this then the output is discarded.
//
// The initial logging level can be controlled by setting the environment
// variable LOG_LEVEL to a string corresponding to the names of the values
// in level_t.
//
// Otherwise we fall back to the hard coded default from above.
//
// As a special case, a blank value for the environment variable LOG_LEVEL
// corresponds to the maximum log level. It indicates a preference for
// minimum output, and may be changed to disable all output in future
// updates.
level_t log_level (void);
level_t log_level (level_t);
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////
void log (level_t, const std::string &msg);
2012-04-24 17:38:35 +10:00
template <typename ...Args, size_t N>
void
log (level_t l, const char (&fmt)[N], Args &&...args)
{
log (
l,
to_string (
format::printf (fmt) (
std::forward<Args> (args)...
)
)
);
}
2012-04-24 17:38:35 +10:00
2015-07-23 17:37:02 +10:00
//-------------------------------------------------------------------------
// Various convenience macros for logging specific strings with a well
// known severity.
//
// 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
// speed.
#define LOG_EMERGENCY(...) do { cruft::log(cruft::EMERGENCY, ##__VA_ARGS__); } while (0)
#define LOG_ALERT(...) do { cruft::log(cruft::ALERT, ##__VA_ARGS__); } while (0)
#define LOG_CRITICAL(...) do { cruft::log(cruft::CRITICAL, ##__VA_ARGS__); } while (0)
#define LOG_ERROR(...) do { cruft::log(cruft::ERROR, ##__VA_ARGS__); } while (0)
#define LOG_WARNING(...) do { cruft::log(cruft::WARNING, ##__VA_ARGS__); } while (0)
#define LOG_WARN(...) do { cruft::log(cruft::WARN, ##__VA_ARGS__); } while (0)
#define LOG_NOTICE(...) do { cruft::log(cruft::NOTICE, ##__VA_ARGS__); } while (0)
#define LOG_INFO(...) do { cruft::log(cruft::INFO, ##__VA_ARGS__); } while (0)
#if !defined(NDEBUG)
#define LOG_DEBUG(...) do { cruft::log(cruft::DEBUG, ##__VA_ARGS__); } while (0)
#else
#define LOG_DEBUG(...) do { ; } while (0)
#endif
2013-03-05 21:38:51 +11:00
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////
2013-03-05 21:38:51 +11:00
class scoped_logger : public nocopy {
2015-07-23 17:37:02 +10:00
public:
2016-03-17 18:08:02 +11:00
scoped_logger (level_t, std::string);
2015-07-23 17:37:02 +10:00
~scoped_logger ();
2013-03-05 21:38:51 +11:00
2015-07-23 17:37:02 +10:00
protected:
2016-03-17 18:08:02 +11:00
const level_t m_level;
const std::string m_message;
2013-03-05 21:38:51 +11:00
};
2015-07-23 17:37:02 +10:00
2015-07-23 17:37:43 +10:00
///////////////////////////////////////////////////////////////////////////
class scoped_timer : public nocopy {
public:
2016-03-17 18:08:02 +11:00
scoped_timer (level_t, std::string);
2015-07-23 17:37:43 +10:00
~scoped_timer ();
private:
const level_t m_level;
const std::string m_message;
uint64_t m_start;
};
2012-04-24 17:38:35 +10:00
}
#endif