netgen/libsrc/core/logging.cpp

138 lines
3.8 KiB
C++
Raw Normal View History

2018-12-28 17:43:15 +05:00
#include "logging.hpp"
#ifdef NETGEN_USE_SPDLOG
2019-01-16 18:33:48 +05:00
#include <spdlog/spdlog.h>
2018-12-28 17:43:15 +05:00
#include <spdlog/sinks/ansicolor_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
2019-01-16 18:33:48 +05:00
#else // NETGEN_USE_SPDLOG
#include <iostream>
2018-12-28 17:43:15 +05:00
#endif // NETGEN_USE_SPDLOG
2019-01-16 18:33:48 +05:00
2018-12-28 17:43:15 +05:00
namespace ngcore
{
2019-01-16 18:33:48 +05:00
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
2019-01-16 19:10:53 +05:00
if(level>level::debug)
std::clog << s << '\n';
2019-01-16 18:33:48 +05:00
#endif // NETGEN_USE_SPDLOG
}
2018-12-28 17:43:15 +05:00
#ifdef NETGEN_USE_SPDLOG
namespace detail
{
std::vector<std::shared_ptr<spdlog::sinks::sink>>& GetDefaultSinks()
{
static std::vector<std::shared_ptr<spdlog::sinks::sink>> sinks =
{ std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>() };
return sinks;
}
std::shared_ptr<spdlog::logger> CreateDefaultLogger(const std::string& name)
{
auto& default_sinks = GetDefaultSinks();
auto logger = std::make_shared<spdlog::logger>(name, default_sinks.begin(), default_sinks.end());
spdlog::details::registry::instance().register_and_init(logger);
return logger;
}
} // namespace detail
2019-01-16 18:33:48 +05:00
std::shared_ptr<Logger> GetLogger(const std::string& name)
2018-12-28 17:43:15 +05:00
{
auto logger = spdlog::get(name);
if(!logger)
logger = detail::CreateDefaultLogger(name);
2019-01-16 18:33:48 +05:00
return std::make_shared<Logger>(logger);
2018-12-28 17:43:15 +05:00
}
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<spdlog::sinks::basic_file_sink_mt>(filename);
sink->set_level(level);
if(!logger.empty())
2019-01-16 18:33:48 +05:00
GetLogger(logger)->logger->sinks().push_back(sink);
2018-12-28 17:43:15 +05:00
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<spdlog::sinks::ansicolor_stdout_sink_mt>();
sink->set_level(level);
if(!logger.empty())
2019-01-16 18:33:48 +05:00
GetLogger(logger)->logger->sinks().push_back(sink);
2018-12-28 17:43:15 +05:00
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())
2019-01-16 18:33:48 +05:00
GetLogger(logger)->logger->sinks().clear();
2018-12-28 17:43:15 +05:00
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())
2019-01-16 18:33:48 +05:00
GetLogger(logger)->logger->flush_on(level);
2018-12-28 17:43:15 +05:00
else
spdlog::flush_on(level);
}
#else // NETGEN_USE_SPDLOG
2019-01-16 18:33:48 +05:00
} //namespace ngcore
namespace spdlog
{
class logger
{
public:
logger() = default;
};
} // namespace spdlog
namespace ngcore
{
2018-12-28 17:43:15 +05:00
// Dummy functions if no spdlog is available
2019-01-16 18:33:48 +05:00
std::shared_ptr<Logger> GetLogger(const std::string& /*unused*/)
2018-12-28 17:43:15 +05:00
{
2019-01-16 18:33:48 +05:00
return std::make_shared<Logger>(std::make_shared<spdlog::logger>());
2018-12-28 17:43:15 +05:00
}
2019-01-16 18:33:48 +05:00
void SetLoggingLevel(level::level_enum /*unused*/, const std::string& /*unused*/) {}
void AddFileSink(const std::string& /*unused*/, level::level_enum /*unused*/,
const std::string& /*unused*/)
2018-12-28 17:43:15 +05:00
{}
2019-01-16 18:33:48 +05:00
void AddConsoleSink(level::level_enum /*unused*/, const std::string& /*unused*/) {}
2018-12-28 17:43:15 +05:00
void ClearLoggingSinks(const std::string& /*unused*/) {}
2019-01-16 18:33:48 +05:00
void FlushOnLoggingLevel(level::level_enum /*unused*/, const std::string& /*unused*/) {}
} //namespace ngcore
2018-12-28 17:43:15 +05:00
#endif // NETGEN_USE_SPDLOG