2012-04-24 17:38:35 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_LOG_HPP
|
|
|
|
#define __UTIL_LOG_HPP
|
|
|
|
|
2013-03-05 21:38:51 +11:00
|
|
|
#include "nocopy.hpp"
|
|
|
|
|
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 {
|
|
|
|
// 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 */
|
2012-04-26 18:20:46 +10:00
|
|
|
WARNING,
|
|
|
|
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-26 18:20:46 +10:00
|
|
|
NUM_LEVELS,
|
|
|
|
DEFAULT = INFO
|
2012-04-24 17:38:35 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream&, level_t);
|
|
|
|
|
|
|
|
void log (level_t, const std::string &format);
|
|
|
|
|
|
|
|
template <typename ...tail>
|
|
|
|
void log (level_t, const std::string &format, tail ..._tail);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class scoped_logger : public nocopy {
|
|
|
|
public:
|
|
|
|
scoped_logger (const level_t, const std::string&);
|
|
|
|
~scoped_logger ();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const level_t m_level;
|
|
|
|
const std::string &m_message;
|
|
|
|
};
|
2012-04-24 17:38:35 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "log.ipp"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|