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(spdlog::get("AutoPasLog"), fmt, ##__VA_ARGS__)
25
26namespace autopas {
31class Logger {
32 private:
33 static inline auto loggerName() { return "AutoPasLog"; };
34
35 public:
39 using LogLevel = spdlog::level::level_enum;
44 static void create(std::string &filename) {
45 // drop an already registered Logger if it exists
46 if (spdlog::get(loggerName())) {
47 unregister();
48 }
49 spdlog::basic_logger_mt(loggerName(), filename);
50 }
51
57 static void create(std::ostream &oss = std::cout) {
58 // drop an already registered Logger if it exists
59 if (spdlog::get(loggerName())) {
60 unregister();
61 }
62 std::shared_ptr<spdlog::sinks::sink> ostreamSink;
63#ifdef AUTOPAS_COLORED_CONSOLE_LOGGING
64 if (oss.rdbuf() == std::cout.rdbuf()) {
65 ostreamSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
66 } else if (oss.rdbuf() == std::cerr.rdbuf()) {
67 ostreamSink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
68 } else { // no color for streams other than cout / cerr
69 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
70 }
71#else
72 ostreamSink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
73#endif
74 auto logger = std::make_shared<spdlog::logger>(loggerName(), ostreamSink);
75 spdlog::register_logger(logger);
76 }
77
84 static void unregister() { spdlog::drop(loggerName()); }
85
90 static auto get() { return spdlog::get(loggerName()); }
91}; // class Logger
92} // namespace autopas
Logger class to provide interface to basic functions of the logger.
Definition: Logger.h:31
spdlog::level::level_enum LogLevel
Typalias for log levels.
Definition: Logger.h:39
static void create(std::ostream &oss=std::cout)
create a logger with an arbitrary ostream.
Definition: Logger.h:57
static void create(std::string &filename)
create a logger writing to the file system
Definition: Logger.h:44
static auto get()
Get a pointer to the actual logger object.
Definition: Logger.h:90
static void unregister()
removes the logger.
Definition: Logger.h:84
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32