#include "logging.hpp" #ifdef NETGEN_USE_SPDLOG #include #include #include #else // NETGEN_USE_SPDLOG #include #endif // NETGEN_USE_SPDLOG namespace ngcore { void Logger::log(level::level_enum level, std::string && s) { #ifdef NETGEN_USE_SPDLOG logger->log(spdlog::level::level_enum(level), s); #else // NETGEN_USE_SPDLOG if(level>level::debug) std::clog << s << '\n'; #endif // NETGEN_USE_SPDLOG } #ifdef NETGEN_USE_SPDLOG namespace detail { std::vector>& GetDefaultSinks() { static std::vector> sinks = { std::make_shared() }; return sinks; } std::shared_ptr CreateDefaultLogger(const std::string& name) { auto& default_sinks = GetDefaultSinks(); auto logger = std::make_shared(name, default_sinks.begin(), default_sinks.end()); spdlog::details::registry::instance().register_and_init(logger); return logger; } } // namespace detail std::shared_ptr GetLogger(const std::string& name) { auto logger = spdlog::get(name); if(!logger) logger = detail::CreateDefaultLogger(name); return std::make_shared(logger); } void SetLoggingLevel(spdlog::level::level_enum level, const std::string& name) { if(!name.empty()) spdlog::get(name)->set_level(level); else spdlog::set_level(level); } void AddFileSink(const std::string& filename, spdlog::level::level_enum level, const std::string& logger) { auto sink = std::make_shared(filename); sink->set_level(level); if(!logger.empty()) GetLogger(logger)->logger->sinks().push_back(sink); else { detail::GetDefaultSinks().push_back(sink); spdlog::details::registry::instance().apply_all([sink](auto logger) { logger->sinks().push_back(sink); }); } } void AddConsoleSink(spdlog::level::level_enum level, const std::string& logger) { auto sink = std::make_shared(); sink->set_level(level); if(!logger.empty()) GetLogger(logger)->logger->sinks().push_back(sink); else { detail::GetDefaultSinks().push_back(sink); spdlog::details::registry::instance().apply_all([sink](auto logger) { logger->sinks().push_back(sink); }); } } void ClearLoggingSinks(const std::string& logger) { if(!logger.empty()) GetLogger(logger)->logger->sinks().clear(); else { detail::GetDefaultSinks().clear(); spdlog::details::registry::instance().apply_all([](auto logger) { logger->sinks().clear(); }); } } void FlushOnLoggingLevel(spdlog::level::level_enum level, const std::string& logger) { if(!logger.empty()) GetLogger(logger)->logger->flush_on(level); else spdlog::flush_on(level); } #else // NETGEN_USE_SPDLOG } //namespace ngcore namespace spdlog { class logger { public: logger() = default; }; } // namespace spdlog namespace ngcore { // Dummy functions if no spdlog is available std::shared_ptr GetLogger(const std::string& /*unused*/) { return std::make_shared(std::make_shared()); } void SetLoggingLevel(level::level_enum /*unused*/, const std::string& /*unused*/) {} void AddFileSink(const std::string& /*unused*/, level::level_enum /*unused*/, const std::string& /*unused*/) {} void AddConsoleSink(level::level_enum /*unused*/, const std::string& /*unused*/) {} void ClearLoggingSinks(const std::string& /*unused*/) {} void FlushOnLoggingLevel(level::level_enum /*unused*/, const std::string& /*unused*/) {} } //namespace ngcore #endif // NETGEN_USE_SPDLOG