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>
24#define AutoPasLog(lvl, fmt, ...) SPDLOG_LOGGER_##lvl(autopas::Logger::get(), fmt, ##__VA_ARGS__)
32 static inline const std::string loggerName =
"AutoPasLog";
47 static std::shared_ptr<spdlog::logger>
get() {
48 auto logger = spdlog::get(loggerName);
54 std::lock_guard<std::mutex> lock(_registrationMutex);
57 logger = spdlog::get(loggerName);
59 logger = create_internal(std::cout);
68 static void create(std::ostream &logOutputStream = std::cout) {
69 std::lock_guard<std::mutex> lock(_registrationMutex);
70 unregister_internal();
71 create_internal(logOutputStream);
78 static void create(
const std::string &filename) {
79 std::lock_guard<std::mutex> lock(_registrationMutex);
80 unregister_internal();
82 auto logger = spdlog::basic_logger_mt(loggerName, filename);
83 logger->flush_on(spdlog::level::warn);
92 std::lock_guard<std::mutex> lock(_registrationMutex);
93 unregister_internal();
100 static inline std::mutex _registrationMutex;
105 static std::shared_ptr<spdlog::logger> create_internal(std::ostream &oss) {
106 std::shared_ptr<spdlog::sinks::sink> ostreamSink;
107#ifdef AUTOPAS_COLORED_CONSOLE_LOGGING
108 if (oss.rdbuf() == std::cout.rdbuf()) {
109 ostreamSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
110 }
else if (oss.rdbuf() == std::cerr.rdbuf()) {
111 ostreamSink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
113 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
116 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
118 auto logger = std::make_shared<spdlog::logger>(loggerName, ostreamSink);
121 logger->flush_on(spdlog::level::warn);
122 spdlog::register_logger(logger);
129 static void unregister_internal() { spdlog::drop(loggerName); }
Logger class to provide interface to basic functions of the logger.
Definition: Logger.h:31
static void create(const std::string &filename)
Explicitly initialize/reset the logger to write to a file.
Definition: Logger.h:78
spdlog::level::level_enum LogLevel
Typealias for log levels.
Definition: Logger.h:38
static std::shared_ptr< spdlog::logger > get()
Get the pointer to the spdlog logger.
Definition: Logger.h:47
static void create(std::ostream &logOutputStream=std::cout)
Explicitly initialize/reset the logger to write to an output stream.
Definition: Logger.h:68
static void unregister()
Removes the logger from the registry.
Definition: Logger.h:91
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:33