// logger.h // // Copyright (C) 2021, Celestia Development Team // // Logging functions. // // This program 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 2 // of the License, or (at your option) any later version. #pragma once #include #include #include #include template <> struct fmt::formatter : formatter { template auto format(const fs::path &path, FormatContext &ctx) { return formatter::format(path.string(), ctx); } }; namespace celestia::util { enum class Level { Error, Warning, Info, Verbose, Debug, }; class Logger { public: using Stream = std::basic_ostream; Logger(); Logger(Level level, Stream &log, Stream &err) : m_level(level), m_log(log), m_err(err) {} void setLevel(Level level) { m_level = level; } template inline void debug(const char *format, const Args&... args) const; template inline void info(const char *format, const Args&... args) const; template inline void verbose(const char *format, const Args&... args) const; template inline void warn(const char *format, const Args&... args) const; template inline void error(const char *format, const Args&... args) const; template void log(Level, const char *format, const Args&... args) const; static Logger* g_logger; private: void vlog(Level level, fmt::string_view format, fmt::format_args args) const; Stream &m_log; Stream &m_err; Level m_level { Level::Info }; }; template void Logger::log(Level level, const char *format, const Args&... args) const { if (level <= m_level) vlog(level, fmt::string_view(format), fmt::make_format_args(args...)); } template void Logger::debug(const char *format, const Args&... args) const { log(Level::Debug, format, args...); } template inline void Logger::info(const char *format, const Args&... args) const { log(Level::Info, format, args...); } template inline void Logger::verbose(const char *format, const Args&... args) const { log(Level::Verbose, format, args...); } template inline void Logger::warn(const char *format, const Args&... args) const { log(Level::Warning, format, args...); } template inline void Logger::error(const char *format, const Args&... args) const { log(Level::Error, format, args...); } Logger* GetLogger(); Logger* CreateLogger(Level level = Level::Info); Logger* CreateLogger(Level level, Logger::Stream &log, Logger::Stream &err); void DestroyLogger(); } // end namespace celestia::util