#ifndef MATADOR_LOGGER_HPP #define MATADOR_LOGGER_HPP #include "matador/logger/log_level.hpp" #include "matador/logger/log_domain.hpp" #include #include #include namespace matador::logger { /** * @brief logger to write log messages to log domains * * This class is used to write log messages to a connected * log domain (@sa log_domain). * Everywhere a logger is needed, it can be instantiated with * @code * matador::create_logger(source name) * @endcode * * The interface provides methods to log to each relevant * log level (@sa log_level) * * The message format syntax is like the printf syntax. * If the message string contains placeholder (beginning with %) * an argument is expected to be part of the argument list of the * calling method. * * All log messages are written through the internal * log_domain object to the sinks. */ class logger final { public: /** * Create a logger with a given source name connected * to the given log_domain * * @param source The name of the source * @param log_domain The log_domain containing the log sinks */ logger(std::string source, std::shared_ptr log_domain); logger(const logger& l) = delete; logger(logger&& l) noexcept; logger& operator=(const logger& l) = delete; logger& operator=(logger&& l) noexcept; /** * Writes a log message string with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void fatal(const std::string &what, ARGS const &... args) { fatal(what.c_str(), args...); } /** * Writes a log message represented by a char pointer with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void fatal(const char *what, ARGS const &... args) { log(log_level::LVL_FATAL, what, args...); } /** * Writes a log message string with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void error(const std::string &what, ARGS const &... args) { error(what.c_str(), args...); } /** * Writes a log message represented by a char pointer with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void error(const char *what, ARGS const &... args) { log(log_level::LVL_ERROR, what, args...); } /** * Writes a log message string with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void warn(const std::string &what, ARGS const &... args) { warn(what.c_str(), args...); } /** * Writes a log message represented by a char pointer with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void warn(const char *what, ARGS const &... args) { log(log_level::LVL_WARN, what, args...); } /** * Writes a log message string with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void info(const std::string &what, ARGS const &... args) { info(what.c_str(), args...); } /** * Writes a log message represented by a char pointer with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void info(const char *what, ARGS const &... args) { log(log_level::LVL_INFO, what, args...); } /** * Writes a log message string with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void debug(const std::string &what, ARGS const &... args) { debug(what.c_str(), args...); } /** * Writes a log message represented by a char pointer with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void debug(const char *what, ARGS const &... args) { log(log_level::LVL_DEBUG, what, args...); } /** * Writes a log message string with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void trace(const std::string &what, ARGS const &... args) { trace(what.c_str(), args...); } /** * Writes a log message represented by a char pointer with log level LVL_FATAL * to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param what The message to log * @param args The arguments to be replaced in the message */ template void trace(const char *what, ARGS const &... args) { log(log_level::LVL_TRACE, what, args...); } /** * Writes a log message represented by a char pointer * with the given log level to the connected log_domain. * * @tparam ARGS Type of the arguments to be replaced for the message placeholder * @param lvl The log level * @param what The message to log * @param args The arguments to be replaced in the message */ template void log(log_level lvl, const char *what, ARGS const &... args); /** * Writes a log message represented by a char pointer * with the given log level to the connected log_domain. * * @param lvl The log level * @param what The message to log */ void log(log_level lvl, const char *what) const; /** * Returns the name of the source the logger represents * * @return Represented log source name */ const std::string& source() const; /** * Returns the name of the connected log domain * * @return The name of the log domain */ std::string domain() const; private: std::string source_; std::shared_ptr logger_domain_; }; template void logger::log(log_level lvl, const char *what, ARGS const &... args) { char message_buffer[16384]; #ifdef _MSC_VER sprintf_s(message_buffer, 16384, what, args...); #else sprintf(message_buffer, what, args...); #endif logger_domain_->log(lvl, source_, message_buffer); } /* template decltype(auto) myForward(T&& t) { return t; } template<> decltype(auto) myForward(std::string& t) { return t.c_str(); } template<> decltype(auto) myForward(std::string&& t) { return t.c_str(); } template static void log(const char* pszFmt, Args&&... args) { doSomething(pszFmt, myForward(std::forward(args))...); } */ } #endif //MATADOR_LOGGER_HPP