9#include <spdlog/fmt/bundled/ranges.h>
10#include <spdlog/sinks/basic_file_sink.h>
11#include <spdlog/sinks/ostream_sink.h>
12#include <spdlog/sinks/stdout_color_sinks.h>
13#include <spdlog/spdlog.h>
25#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
26#define AutoPasLog_TRACE(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::trace, __VA_ARGS__)
28#define AutoPasLog_TRACE(...) (void)0
32#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
33#define AutoPasLog_DEBUG(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::debug, __VA_ARGS__)
35#define AutoPasLog_DEBUG(...) (void)0
39#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
40#define AutoPasLog_INFO(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::info, __VA_ARGS__)
42#define AutoPasLog_INFO(...) (void)0
46#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
47#define AutoPasLog_WARN(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::warn, __VA_ARGS__)
49#define AutoPasLog_WARN(...) (void)0
53#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
54#define AutoPasLog_ERROR(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::err, __VA_ARGS__)
56#define AutoPasLog_ERROR(...) (void)0
60#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
61#define AutoPasLog_CRITICAL(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::critical, __VA_ARGS__)
63#define AutoPasLog_CRITICAL(...) (void)0
74#define AutoPasLog(lvl, fmt, ...) AutoPasLog_##lvl(fmt, ##__VA_ARGS__)
82 static inline const std::string loggerName =
"AutoPasLog";
97 static std::shared_ptr<spdlog::logger>
get() {
98 auto logger = spdlog::get(loggerName);
104 std::lock_guard<std::mutex> lock(_registrationMutex);
107 logger = spdlog::get(loggerName);
109 logger = create_internal(std::cout);
118 static void create(std::ostream &logOutputStream = std::cout) {
119 std::lock_guard<std::mutex> lock(_registrationMutex);
120 unregister_internal();
121 create_internal(logOutputStream);
128 static void create(
const std::string &filename) {
129 std::lock_guard<std::mutex> lock(_registrationMutex);
130 unregister_internal();
132 auto logger = spdlog::basic_logger_mt(loggerName, filename);
133 logger->flush_on(spdlog::level::warn);
142 std::lock_guard<std::mutex> lock(_registrationMutex);
143 unregister_internal();
150 static inline std::mutex _registrationMutex;
155 static std::shared_ptr<spdlog::logger> create_internal(std::ostream &oss) {
156 std::shared_ptr<spdlog::sinks::sink> ostreamSink;
157#ifdef AUTOPAS_COLORED_CONSOLE_LOGGING
158 if (oss.rdbuf() == std::cout.rdbuf()) {
159 ostreamSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
160 }
else if (oss.rdbuf() == std::cerr.rdbuf()) {
161 ostreamSink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
163 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
166 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
168 auto logger = std::make_shared<spdlog::logger>(loggerName, ostreamSink);
171 logger->flush_on(spdlog::level::warn);
172 spdlog::register_logger(logger);
179 static void unregister_internal() { spdlog::drop(loggerName); }
Logger class to provide interface to basic functions of the logger.
Definition: Logger.h:81
static void create(const std::string &filename)
Explicitly initialize/reset the logger to write to a file.
Definition: Logger.h:128
spdlog::level::level_enum LogLevel
Typealias for log levels.
Definition: Logger.h:88
static std::shared_ptr< spdlog::logger > get()
Get the pointer to the spdlog logger.
Definition: Logger.h:97
static void create(std::ostream &logOutputStream=std::cout)
Explicitly initialize/reset the logger to write to an output stream.
Definition: Logger.h:118
static void unregister()
Removes the logger from the registry.
Definition: Logger.h:141
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34