AutoPas  3.0.0
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1
7#pragma once
8
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>
14
15#include <iostream>
16
25#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
26#define AutoPasLog_TRACE(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::trace, __VA_ARGS__)
27#else
28#define AutoPasLog_TRACE(...) (void)0
29#endif
30
32#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
33#define AutoPasLog_DEBUG(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::debug, __VA_ARGS__)
34#else
35#define AutoPasLog_DEBUG(...) (void)0
36#endif
37
39#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
40#define AutoPasLog_INFO(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::info, __VA_ARGS__)
41#else
42#define AutoPasLog_INFO(...) (void)0
43#endif
44
46#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
47#define AutoPasLog_WARN(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::warn, __VA_ARGS__)
48#else
49#define AutoPasLog_WARN(...) (void)0
50#endif
51
53#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
54#define AutoPasLog_ERROR(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::err, __VA_ARGS__)
55#else
56#define AutoPasLog_ERROR(...) (void)0
57#endif
58
60#if AUTOPAS_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
61#define AutoPasLog_CRITICAL(...) SPDLOG_LOGGER_CALL(autopas::Logger::get(), spdlog::level::critical, __VA_ARGS__)
62#else
63#define AutoPasLog_CRITICAL(...) (void)0
64#endif
74#define AutoPasLog(lvl, fmt, ...) AutoPasLog_##lvl(fmt, ##__VA_ARGS__)
75
76namespace autopas {
81class Logger {
82 static inline const std::string loggerName = "AutoPasLog";
83
84 public:
88 using LogLevel = spdlog::level::level_enum;
89
97 static std::shared_ptr<spdlog::logger> get() {
98 auto logger = spdlog::get(loggerName);
99 if (logger) {
100 return logger;
101 }
102
103 // Logger not found? Create a default one safely.
104 std::lock_guard<std::mutex> lock(_registrationMutex);
105
106 // Double-check after locking in case another thread created it just now
107 logger = spdlog::get(loggerName);
108 if (not logger) {
109 logger = create_internal(std::cout); // Default to cout
110 }
111 return logger;
112 }
113
118 static void create(std::ostream &logOutputStream = std::cout) {
119 std::lock_guard<std::mutex> lock(_registrationMutex);
120 unregister_internal(); // Drop old one first
121 create_internal(logOutputStream);
122 }
123
128 static void create(const std::string &filename) {
129 std::lock_guard<std::mutex> lock(_registrationMutex);
130 unregister_internal(); // Drop old one first
131
132 auto logger = spdlog::basic_logger_mt(loggerName, filename);
133 logger->flush_on(spdlog::level::warn);
134 // spdlog automatically registers the logger upon creation via factory methods
135 }
136
141 static void unregister() {
142 std::lock_guard<std::mutex> lock(_registrationMutex);
143 unregister_internal();
144 }
145
146 private:
150 static inline std::mutex _registrationMutex;
151
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>();
162 } else { // no color for streams other than cout / cerr
163 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
164 }
165#else
166 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
167#endif
168 auto logger = std::make_shared<spdlog::logger>(loggerName, ostreamSink);
169 // The logger is normally only flushed on successful program termination.
170 // This line ensures flushing when log messages of level warning or more severe are created.
171 logger->flush_on(spdlog::level::warn);
172 spdlog::register_logger(logger);
173 return logger;
174 }
175
179 static void unregister_internal() { spdlog::drop(loggerName); }
180}; // class Logger
181} // namespace autopas
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