89 lines
2.8 KiB
C++
89 lines
2.8 KiB
C++
#ifndef QUERY_LOGGER_HPP
|
|
#define QUERY_LOGGER_HPP
|
|
|
|
#include <mutex>
|
|
#include <string>
|
|
|
|
namespace matador::utils {
|
|
|
|
enum class log_level
|
|
{
|
|
LVL_FATAL, /**< If a serious error occurred use FATAL level */
|
|
LVL_ERROR, /**< On error use ERROR level */
|
|
LVL_WARN, /**< Warnings should use WARN level */
|
|
LVL_INFO, /**< Information should go with INFO level */
|
|
LVL_DEBUG, /**< Debug output should use DEBUG level */
|
|
LVL_TRACE, /**< Trace information should use TRACE level */
|
|
LVL_ALL /**< This level represents all log levels and should be used for logging */
|
|
};
|
|
|
|
/**
|
|
* Simple file logger
|
|
*/
|
|
class logger
|
|
{
|
|
public:
|
|
logger(const std::string &path, std::string source);
|
|
logger(FILE *file, std::string source);
|
|
logger(const logger &x);
|
|
logger& operator=(const logger &x);
|
|
logger(logger &&x) noexcept;
|
|
logger& operator=(logger &&x) noexcept;
|
|
|
|
template<typename ... Args>
|
|
void info(const std::string &what, Args const &... args) const { info(what.c_str(), args...); }
|
|
template<typename ... Args>
|
|
void info(const char *what, Args const &... args) const { log(log_level::LVL_INFO, what, args...); }
|
|
|
|
template<typename ... Args>
|
|
void debug(const std::string &what, Args const &... args) const { debug(what.c_str(), args...); }
|
|
template<typename ... Args>
|
|
void debug(const char *what, Args const &... args) const { log(log_level::LVL_DEBUG, what, args...); }
|
|
|
|
template<typename ... Args>
|
|
void warn(const std::string &what, Args const &... args) const { warn(what.c_str(), args...); }
|
|
template<typename ... Args>
|
|
void warn(const char *what, Args const &... args) const { log(log_level::LVL_WARN, what, args...); }
|
|
|
|
template<typename ... Args>
|
|
void error(const std::string &what, Args const &... args) const { error(what.c_str(), args...); }
|
|
template<typename ... Args>
|
|
void error(const char *what, Args const &... args) const { log(log_level::LVL_ERROR, what, args...); }
|
|
|
|
template<typename ... Args>
|
|
void fatal(const std::string &what, Args const &... args) const { fatal(what.c_str(), args...); }
|
|
template<typename ... Args>
|
|
void fatal(const char *what, Args const &... args) const { log(log_level::LVL_FATAL, what, args...); }
|
|
|
|
template<typename ... Args>
|
|
void log(log_level lvl, const char *what, Args const &... args) const;
|
|
void log(log_level lvl, const char *message) const;
|
|
|
|
private:
|
|
void write(const char *message, size_t size) const;
|
|
void close();
|
|
|
|
private:
|
|
std::string path_;
|
|
FILE *stream = nullptr;
|
|
mutable std::mutex mutex_;
|
|
std::string source_;
|
|
};
|
|
|
|
template<typename... ARGS>
|
|
void logger::log(log_level lvl, const char *what, ARGS const &... args) const
|
|
{
|
|
char message_buffer[16384];
|
|
|
|
#ifdef _MSC_VER
|
|
sprintf_s(message_buffer, 16384, what, args...);
|
|
#else
|
|
sprintf(message_buffer, what, args...);
|
|
#endif
|
|
|
|
log(lvl, message_buffer);
|
|
}
|
|
|
|
}
|
|
#endif //QUERY_LOGGER_HPP
|