mirror of
https://github.com/NGSolve/netgen.git
synced 2024-11-11 16:49:16 +05:00
Include spdlog only in logger.cpp
This commit is contained in:
parent
7c6e2ae35e
commit
4ef76fa710
@ -106,7 +106,7 @@ namespace ngcore
|
||||
protected:
|
||||
bool shallow_to_python = false;
|
||||
std::map<std::string, VersionInfo> version_map = GetLibraryVersions();
|
||||
std::shared_ptr<spdlog::logger> logger = GetLogger("Archive");
|
||||
std::shared_ptr<Logger> logger = GetLogger("Archive");
|
||||
public:
|
||||
Archive() = delete;
|
||||
Archive(const Archive&) = delete;
|
||||
|
@ -1,15 +1,28 @@
|
||||
|
||||
#include "logging.hpp"
|
||||
|
||||
#ifdef NETGEN_USE_SPDLOG
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <spdlog/sinks/ansicolor_sink.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
|
||||
#else // NETGEN_USE_SPDLOG
|
||||
#include <iostream>
|
||||
#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
|
||||
std::clog << s << '\n';
|
||||
#endif // NETGEN_USE_SPDLOG
|
||||
}
|
||||
|
||||
#ifdef NETGEN_USE_SPDLOG
|
||||
namespace detail
|
||||
{
|
||||
@ -28,12 +41,12 @@ namespace ngcore
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
std::shared_ptr<spdlog::logger> GetLogger(const std::string& name)
|
||||
std::shared_ptr<Logger> GetLogger(const std::string& name)
|
||||
{
|
||||
auto logger = spdlog::get(name);
|
||||
if(!logger)
|
||||
logger = detail::CreateDefaultLogger(name);
|
||||
return logger;
|
||||
return std::make_shared<Logger>(logger);
|
||||
}
|
||||
|
||||
void SetLoggingLevel(spdlog::level::level_enum level, const std::string& name)
|
||||
@ -49,7 +62,7 @@ namespace ngcore
|
||||
auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename);
|
||||
sink->set_level(level);
|
||||
if(!logger.empty())
|
||||
GetLogger(logger)->sinks().push_back(sink);
|
||||
GetLogger(logger)->logger->sinks().push_back(sink);
|
||||
else
|
||||
{
|
||||
detail::GetDefaultSinks().push_back(sink);
|
||||
@ -62,7 +75,7 @@ namespace ngcore
|
||||
auto sink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>();
|
||||
sink->set_level(level);
|
||||
if(!logger.empty())
|
||||
GetLogger(logger)->sinks().push_back(sink);
|
||||
GetLogger(logger)->logger->sinks().push_back(sink);
|
||||
else
|
||||
{
|
||||
detail::GetDefaultSinks().push_back(sink);
|
||||
@ -73,7 +86,7 @@ namespace ngcore
|
||||
void ClearLoggingSinks(const std::string& logger)
|
||||
{
|
||||
if(!logger.empty())
|
||||
GetLogger(logger)->sinks().clear();
|
||||
GetLogger(logger)->logger->sinks().clear();
|
||||
else
|
||||
{
|
||||
detail::GetDefaultSinks().clear();
|
||||
@ -84,26 +97,40 @@ namespace ngcore
|
||||
void FlushOnLoggingLevel(spdlog::level::level_enum level, const std::string& logger)
|
||||
{
|
||||
if(!logger.empty())
|
||||
GetLogger(logger)->flush_on(level);
|
||||
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<spdlog::logger> GetLogger(const std::string& /*unused*/)
|
||||
std::shared_ptr<Logger> GetLogger(const std::string& /*unused*/)
|
||||
{
|
||||
return std::make_shared<spdlog::logger>();
|
||||
return std::make_shared<Logger>(std::make_shared<spdlog::logger>());
|
||||
}
|
||||
void SetLoggingLevel(spdlog::level::level_enum /*unused*/, const std::string& /*unused*/) {}
|
||||
void AddFileSink(const std::string& /*unused*/, spdlog::level::level_enum /*unused*/,
|
||||
const std::string& /*unused*/)
|
||||
|
||||
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(spdlog::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(spdlog::level::level_enum /*unused*/, const std::string& /*unused*/) {}
|
||||
void FlushOnLoggingLevel(level::level_enum /*unused*/, const std::string& /*unused*/) {}
|
||||
} //namespace ngcore
|
||||
|
||||
#endif // NETGEN_USE_SPDLOG
|
||||
} //namespace ngcore
|
||||
|
@ -1,109 +1,124 @@
|
||||
#ifndef NETGEN_CORE_LOGGING_HPP
|
||||
#define NETGEN_CORE_LOGGING_HPP
|
||||
|
||||
#undef NETGEN_USE_SPDLOG
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "exception.hpp"
|
||||
#include "ngcore_api.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
#ifdef NETGEN_USE_SPDLOG
|
||||
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
#include <spdlog/fmt/ostr.h> // 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
|
||||
|
||||
#include <spdlog/fmt/ostr.h> // to be able to parse anything to logger that implements operator << ostream
|
||||
#include <spdlog/logger.h>
|
||||
#include <spdlog/sinks/sink.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#define NETGEN_DEBUG_LOG(...) SPDLOG_DEBUG(__VA_ARGS__)
|
||||
|
||||
#else // NETGEN_USE_SPDLOG
|
||||
|
||||
#include <iostream>
|
||||
#ifndef NETGEN_DEBUG_LOG
|
||||
#define NETGEN_DEBUG_LOG(logger, ...)
|
||||
#endif // NETGEN_DEBUG_LOG
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
// Dummys if Netgen is compiled with USE_SPDLOG=OFF.
|
||||
namespace level
|
||||
{
|
||||
enum level_enum
|
||||
{
|
||||
trace = 0,
|
||||
debug = 1,
|
||||
info = 2,
|
||||
warn = 3,
|
||||
err = 4,
|
||||
critical = 5,
|
||||
off = 6
|
||||
};
|
||||
} // namespace level
|
||||
|
||||
class logger
|
||||
{
|
||||
public:
|
||||
void log_helper() {}
|
||||
|
||||
template<typename T>
|
||||
void log_helper( T t) { std::clog << t; }
|
||||
|
||||
template<typename T, typename ... Args>
|
||||
void log_helper( T t, Args ... args)
|
||||
{
|
||||
std::clog << t;
|
||||
log_helper(args...);
|
||||
std::clog << ", ";
|
||||
}
|
||||
|
||||
template<typename ... Args>
|
||||
void log( level::level_enum level, const char* fmt, Args ... args)
|
||||
{
|
||||
std::clog << level << ": " << fmt << "\t Arguments: ";
|
||||
log_helper(args...);
|
||||
std::clog << "\n";
|
||||
}
|
||||
|
||||
template<typename ... Args>
|
||||
void trace( const char* fmt, Args ... args) { log(level::level_enum::trace, fmt, args...); }
|
||||
template<typename ... Args>
|
||||
void debug( const char* fmt, Args ... args) { log(level::level_enum::debug, fmt, args...); }
|
||||
template<typename ... Args>
|
||||
void info( const char* fmt, Args ... args) { log(level::level_enum::info, fmt, args...); }
|
||||
template<typename ... Args>
|
||||
void warn( const char* fmt, Args ... args) { log(level::level_enum::warn, fmt, args...); }
|
||||
template<typename ... Args>
|
||||
void error( const char* fmt, Args ... args) { log(level::level_enum::err, fmt, args...); }
|
||||
template<typename ... Args>
|
||||
void critical( const char* fmt, Args ... args) { log(level::level_enum::critical, fmt, args...); }
|
||||
};
|
||||
|
||||
namespace sinks
|
||||
{
|
||||
class sink {};
|
||||
} // namespace sinks
|
||||
|
||||
} //namespace spdlog
|
||||
|
||||
#define NETGEN_DEBUG_LOG(logger, ...)
|
||||
|
||||
#endif // NETGEN_USE_SPDLOG
|
||||
class logger;
|
||||
} // namespace spdlog
|
||||
|
||||
namespace ngcore
|
||||
{
|
||||
namespace detail
|
||||
namespace level
|
||||
{
|
||||
std::vector<std::shared_ptr<spdlog::sinks::sink>>& GetDefaultSinks();
|
||||
inline std::shared_ptr<spdlog::logger> CreateDefaultLogger(const std::string& name);
|
||||
} //namespace detail
|
||||
enum level_enum
|
||||
{
|
||||
trace = 0,
|
||||
debug = 1,
|
||||
info = 2,
|
||||
warn = 3,
|
||||
err = 4,
|
||||
critical = 5,
|
||||
off = 6
|
||||
};
|
||||
}
|
||||
|
||||
NGCORE_API std::shared_ptr<spdlog::logger> GetLogger(const std::string& name);
|
||||
NGCORE_API void SetLoggingLevel(spdlog::level::level_enum level, const std::string& name);
|
||||
NGCORE_API void AddFileSink(const std::string& filename, spdlog::level::level_enum level, const std::string& logger);
|
||||
NGCORE_API void AddConsoleSink(spdlog::level::level_enum level, const std::string& logger);
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
std::shared_ptr<spdlog::logger> logger;
|
||||
|
||||
Logger(std::shared_ptr<spdlog::logger> l) : logger(l) {}
|
||||
|
||||
void NGCORE_API log( level::level_enum level, std::string && s);
|
||||
|
||||
#ifdef NETGEN_USE_SPDLOG
|
||||
template<typename ... Args>
|
||||
void log( level::level_enum level, const char* str, Args ... args)
|
||||
{
|
||||
log(level, fmt::format(str, args...));
|
||||
}
|
||||
#else // NETGEN_USE_SPDLOG
|
||||
template<typename T>
|
||||
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<typename T>
|
||||
std::string log_helper(std::string s, const T &t)
|
||||
{
|
||||
return replace(s,t);
|
||||
}
|
||||
|
||||
template<typename T, typename ... Args>
|
||||
std::string log_helper( std::string s, const T &t, Args ... args)
|
||||
{
|
||||
return log_helper(replace(s,t), args...);
|
||||
}
|
||||
|
||||
template<typename ... Args>
|
||||
void log( level::level_enum level, const char* str, Args ... args)
|
||||
{
|
||||
log(level, log_helper(std::string(str), args...));
|
||||
}
|
||||
#endif // NETGEN_USE_SPDLOG
|
||||
|
||||
template<typename ... Args>
|
||||
void trace( const char* str, Args ... args) { log(level::level_enum::trace, str, args...); }
|
||||
template<typename ... Args>
|
||||
void debug( const char* str, Args ... args) { log(level::level_enum::debug, str, args...); }
|
||||
template<typename ... Args>
|
||||
void info( const char* str, Args ... args) { log(level::level_enum::info, str, args...); }
|
||||
template<typename ... Args>
|
||||
void warn( const char* str, Args ... args) { log(level::level_enum::warn, str, args...); }
|
||||
template<typename ... Args>
|
||||
void error( const char* str, Args ... args) { log(level::level_enum::err, str, args...); }
|
||||
template<typename ... Args>
|
||||
void critical( const char* str, Args ... args) { log(level::level_enum::critical, str, args...); }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
NGCORE_API std::shared_ptr<Logger> 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(spdlog::level::level_enum level, const std::string& logger);
|
||||
NGCORE_API void FlushOnLoggingLevel(level::level_enum level, const std::string& logger);
|
||||
} // namespace ngcore
|
||||
|
||||
#endif // NETGEN_CORE_LOGGING_HPP
|
||||
|
@ -92,7 +92,7 @@ namespace ngcore
|
||||
|
||||
FILE * ctrace_stream;
|
||||
TTimePoint start_time;
|
||||
std::shared_ptr<spdlog::logger> logger = GetLogger("PajeTrace");
|
||||
std::shared_ptr<Logger> logger = GetLogger("PajeTrace");
|
||||
|
||||
|
||||
double ConvertTime(TTimePoint t) {
|
||||
|
@ -18,7 +18,7 @@ namespace ngcore
|
||||
using TClock = std::chrono::system_clock;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<spdlog::logger> logger = GetLogger("PajeTrace");
|
||||
std::shared_ptr<Logger> logger = GetLogger("PajeTrace");
|
||||
private:
|
||||
NGCORE_API static size_t max_tracefile_size;
|
||||
NGCORE_API static bool trace_thread_counter;
|
||||
|
@ -13,7 +13,7 @@ namespace ngcore
|
||||
size_t NgProfiler::dummy_thread_flops[NgProfiler::SIZE];
|
||||
size_t * NgProfiler::thread_flops = NgProfiler::dummy_thread_flops; // NOLINT
|
||||
|
||||
std::shared_ptr<spdlog::logger> NgProfiler::logger = GetLogger("Profiler"); // NOLINT
|
||||
std::shared_ptr<Logger> NgProfiler::logger = GetLogger("Profiler"); // NOLINT
|
||||
|
||||
NgProfiler :: NgProfiler()
|
||||
{
|
||||
|
@ -34,7 +34,7 @@ namespace ngcore
|
||||
|
||||
NGCORE_API static TTimePoint * thread_times;
|
||||
NGCORE_API static TTimePoint * thread_flops;
|
||||
NGCORE_API static std::shared_ptr<spdlog::logger> logger;
|
||||
NGCORE_API static std::shared_ptr<Logger> logger;
|
||||
NGCORE_API static size_t dummy_thread_times[NgProfiler::SIZE];
|
||||
NGCORE_API static size_t dummy_thread_flops[NgProfiler::SIZE];
|
||||
private:
|
||||
|
@ -8,14 +8,14 @@ using namespace ngcore;
|
||||
|
||||
PYBIND11_MODULE(pyngcore, m) // NOLINT
|
||||
{
|
||||
py::enum_<spdlog::level::level_enum>(m, "LOG_LEVEL", "Logging level")
|
||||
.value("Trace", spdlog::level::trace)
|
||||
.value("Debug", spdlog::level::debug)
|
||||
.value("Info", spdlog::level::info)
|
||||
.value("Warn", spdlog::level::warn)
|
||||
.value("Error", spdlog::level::err)
|
||||
.value("Critical", spdlog::level::critical)
|
||||
.value("Off", spdlog::level::off);
|
||||
py::enum_<level::level_enum>(m, "LOG_LEVEL", "Logging level")
|
||||
.value("Trace", level::trace)
|
||||
.value("Debug", level::debug)
|
||||
.value("Info", level::info)
|
||||
.value("Warn", level::warn)
|
||||
.value("Error", level::err)
|
||||
.value("Critical", level::critical)
|
||||
.value("Off", level::off);
|
||||
|
||||
m.def("SetLoggingLevel", &SetLoggingLevel, py::arg("level"), py::arg("logger")="",
|
||||
"Set logging level, if name is given only to the specific logger, else set the global logging level");
|
||||
|
Loading…
Reference in New Issue
Block a user