2012-04-24 17:38:35 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-04-24 17:38:35 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-04-24 17:38:35 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-04-24 17:38:35 +10:00
|
|
|
*
|
|
|
|
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_LOG_HPP
|
|
|
|
#define __UTIL_LOG_HPP
|
|
|
|
|
2016-03-21 14:21:14 +11:00
|
|
|
#include "./nocopy.hpp"
|
|
|
|
#include "./preprocessor.hpp"
|
2013-03-05 21:38:51 +11:00
|
|
|
|
2012-04-24 17:38:35 +10:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
2012-04-27 17:56:23 +10:00
|
|
|
// Windows.h or one of its friends defines a macro 'ERROR'. Screw Microsoft.
|
|
|
|
#ifdef ERROR
|
|
|
|
#undef ERROR
|
|
|
|
#endif
|
|
|
|
|
2012-04-24 17:38:35 +10:00
|
|
|
namespace util {
|
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 */
|
2016-01-21 13:55:08 +11:00
|
|
|
DEBUG /** debug-level messages */
|
2012-04-24 17:38:35 +10:00
|
|
|
};
|
|
|
|
|
2016-03-21 14:21:14 +11:00
|
|
|
#define MAP_LEVEL_T(F) MAP(F, EMERGENCY, ALERT, CRITICAL, ERROR, WARN, NOTICE, INFO, DEBUG)
|
|
|
|
|
2016-01-21 13:55:08 +11:00
|
|
|
constexpr auto DEFAULT_LOG_LEVEL = INFO;
|
|
|
|
|
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
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-01-20 16:40:42 +11:00
|
|
|
void log (level_t, const std::string &msg);
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename ...Args, size_t N>
|
|
|
|
void log (level_t, const char (&fmt)[N], const Args&...);
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2012-06-08 16:45:18 +10:00
|
|
|
#define LOG_EMERGENCY(...) do { util::log(util::EMERGENCY, ##__VA_ARGS__); } while (0)
|
|
|
|
#define LOG_ALERT(...) do { util::log(util::ALERT, ##__VA_ARGS__); } while (0)
|
|
|
|
#define LOG_CRITICAL(...) do { util::log(util::CRITICAL, ##__VA_ARGS__); } while (0)
|
|
|
|
#define LOG_ERROR(...) do { util::log(util::ERROR, ##__VA_ARGS__); } while (0)
|
|
|
|
#define LOG_WARNING(...) do { util::log(util::WARNING, ##__VA_ARGS__); } while (0)
|
|
|
|
#define LOG_WARN(...) do { util::log(util::WARN, ##__VA_ARGS__); } while (0)
|
|
|
|
#define LOG_NOTICE(...) do { util::log(util::NOTICE, ##__VA_ARGS__); } while (0)
|
|
|
|
#define LOG_INFO(...) do { util::log(util::INFO, ##__VA_ARGS__); } while (0)
|
2012-05-11 12:18:41 +10:00
|
|
|
#if defined(ENABLE_DEBUGGING)
|
2012-06-08 16:45:18 +10:00
|
|
|
#define LOG_DEBUG(...) do { util::log(util::DEBUG, ##__VA_ARGS__); } while (0)
|
2012-05-11 12:18:41 +10:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
|
|
#include "log.ipp"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|