UsenetSearch/include/usenetsearch/Logger.h

91 lines
2.1 KiB
C++

/*
Copyright© 2021 John Sennesael
UsenetSearch 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.
UsenetSearch 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 UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <type_traits>
#include <memory>
#include <stdexcept>
namespace usenetsearch {
/**
* Simple logging class singleton.
*/
class Logger
{
/// Holds a pointer to the singleton instance.
static std::unique_ptr<Logger> m_instance;
public:
/**
* Constructor.
*/
Logger();
/**
* Destructor.
*/
virtual ~Logger() = default;
/**
* Gets an instance to the logger.
*/
static Logger& get();
/**
* Logs a debug message.
*
* @param[in] msg Message to be logged.
*/
void debug(const std::string& msg) const;
/**
* Logs an info message.
*
* @param[in] msg Message to be logged.
*/
void info(const std::string& msg) const;
/**
* Logs an error message.
*
* @param[in] msg Message to be logged.
*/
void error(const std::string& msg) const;
/**
* Logs an error message and throws.
*
* @tparam EXCEPTION Type of exception to throw.
* @param[in] msg Error message to be logged.
*/
template <typename EXCEPTION>
inline void fatal(const std::string& msg) const
{
static_assert(
std::is_base_of<std::exception, EXCEPTION>::value,
"EXCEPTION must be an std::exception or derrived child.");
error(msg);
throw EXCEPTION(msg);
};
};
} // namespace usenetsearch