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
24#define AutoPasLog(lvl, fmt, ...) SPDLOG_LOGGER_##lvl(autopas::Logger::get(), fmt, ##__VA_ARGS__)
25
26namespace autopas {
31class Logger {
32 static inline const std::string loggerName = "AutoPasLog";
33
34 public:
38 using LogLevel = spdlog::level::level_enum;
39
47 static std::shared_ptr<spdlog::logger> get() {
48 auto logger = spdlog::get(loggerName);
49 if (logger) {
50 return logger;
51 }
52
53 // Logger not found? Create a default one safely.
54 std::lock_guard<std::mutex> lock(_registrationMutex);
55
56 // Double-check after locking in case another thread created it just now
57 logger = spdlog::get(loggerName);
58 if (not logger) {
59 logger = create_internal(std::cout); // Default to cout
60 }
61 return logger;
62 }
63
68 static void create(std::ostream &logOutputStream = std::cout) {
69 std::lock_guard<std::mutex> lock(_registrationMutex);
70 unregister_internal(); // Drop old one first
71 create_internal(logOutputStream);
72 }
73
78 static void create(const std::string &filename) {
79 std::lock_guard<std::mutex> lock(_registrationMutex);
80 unregister_internal(); // Drop old one first
81
82 auto logger = spdlog::basic_logger_mt(loggerName, filename);
83 logger->flush_on(spdlog::level::warn);
84 // spdlog automatically registers the logger upon creation via factory methods
85 }
86
91 static void unregister() {
92 std::lock_guard<std::mutex> lock(_registrationMutex);
93 unregister_internal();
94 }
95
96 private:
100 static inline std::mutex _registrationMutex;
101
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>();
112 } else { // no color for streams other than cout / cerr
113 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
114 }
115#else
116 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
117#endif
118 auto logger = std::make_shared<spdlog::logger>(loggerName, ostreamSink);
119 // The logger is normally only flushed on successful program termination.
120 // This line ensures flushing when log messages of level warning or more severe are created.
121 logger->flush_on(spdlog::level::warn);
122 spdlog::register_logger(logger);
123 return logger;
124 }
125
129 static void unregister_internal() { spdlog::drop(loggerName); }
130}; // class Logger
131} // namespace autopas
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