#ifndef NETGEN_CORE_LOGGING_HPP #define NETGEN_CORE_LOGGING_HPP #undef NETGEN_USE_SPDLOG #include #include #include #include #include "exception.hpp" #include "ngcore_api.hpp" #include "utils.hpp" #ifdef NETGEN_USE_SPDLOG #include #include // to be able to parse anything to logger that implements operator << ostream #ifdef NETGEN_LOG_DEBUG #define SPDLOG_DEBUG_ON #define NETGEN_DEBUG_LOG(logger, ...) SPDLOG_DEBUG(logger, __VA_ARGS__) #endif // NETGEN_LOG_DEBUG #endif // NETGEN_USE_SPDLOG #ifndef NETGEN_DEBUG_LOG #define NETGEN_DEBUG_LOG(logger, ...) #endif // NETGEN_DEBUG_LOG namespace spdlog { class logger; } // namespace spdlog namespace ngcore { namespace level { enum level_enum { trace = 0, debug = 1, info = 2, warn = 3, err = 4, critical = 5, off = 6 }; } // namespace level class Logger { public: std::shared_ptr logger; Logger(std::shared_ptr l) : logger(std::move(l)) {} void NGCORE_API log( level::level_enum level, std::string && s); #ifdef NETGEN_USE_SPDLOG template void log( level::level_enum level, const char* str, Args ... args) { log(level, fmt::format(str, args...)); } #else // NETGEN_USE_SPDLOG template std::string replace(std::string s, const T & t) { auto p0 = s.find_first_of('{'); auto p1 = s.find_first_of('}', p0); if(p0==std::string::npos || p1==std::string::npos) throw Exception("invalid format string"); s.replace(p0, p1-p0+1, ToString(t)); return s; } std::string log_helper(std::string s) { return s; } template std::string log_helper(std::string s, const T &t) { return replace(s,t); } template std::string log_helper( std::string s, const T &t, Args ... args) { return log_helper(replace(s,t), args...); } template void log( level::level_enum level, const char* str, Args ... args) { log(level, log_helper(std::string(str), args...)); } #endif // NETGEN_USE_SPDLOG template void trace( const char* str, Args ... args) { log(level::level_enum::trace, str, args...); } template void debug( const char* str, Args ... args) { log(level::level_enum::debug, str, args...); } template void info( const char* str, Args ... args) { log(level::level_enum::info, str, args...); } template void warn( const char* str, Args ... args) { log(level::level_enum::warn, str, args...); } template void error( const char* str, Args ... args) { log(level::level_enum::err, str, args...); } template void critical( const char* str, Args ... args) { log(level::level_enum::critical, str, args...); } }; NGCORE_API std::shared_ptr GetLogger(const std::string& name); NGCORE_API void SetLoggingLevel(level::level_enum level, const std::string& name); NGCORE_API void AddFileSink(const std::string& filename, level::level_enum level, const std::string& logger); NGCORE_API void AddConsoleSink(level::level_enum level, const std::string& logger); NGCORE_API void ClearLoggingSinks(const std::string& logger); NGCORE_API void FlushOnLoggingLevel(level::level_enum level, const std::string& logger); } // namespace ngcore #endif // NETGEN_CORE_LOGGING_HPP