CORSIKA  @c8_version@
The framework to simulate particle cascades for astroparticle physics
Logging.hpp
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
3  *
4  * This software is distributed under the terms of the GNU General Public
5  * Licence version 3 (GPL Version 3). See file LICENSE for a full version of
6  * the license.
7  */
8 
18 #pragma once
19 
20 // Configure some behaviour of sdlog.
21 // This must be done before spdlog is included.
22 
23 // use the coarse system clock. This is *much* faster
24 // but introduces a timestamp error of O(10 ms) which is fine for us.
25 #define SPDLOG_CLOCK_COARSE
26 
27 // do not create a default logger (we provide our own "corsika" logger)
28 #define SPDLOG_DISABLE_DEFAULT_LOGGER
29 
30 // use __PRETTY_FUNCTION__ instead of __FUNCTION__ where
31 // printing function names in trace statements. This is much
32 // nicer than __FUNCTION__ under GCC/clang.
33 #define SPDLOG_FUNCTION __PRETTY_FUNCTION__
34 
35 // if this is a Debug build, include debug messages in objects
36 #ifdef _C8_DEBUG_
37 // trace is the highest level of logging (ALL messages will be printed)
38 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
39 #else // otherwise, remove everything but "error" and worse messages
40 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
41 #endif
42 
43 #include <spdlog/fmt/ostr.h> // will output whenerver a streaming operator is found
44 #include <spdlog/sinks/stdout_color_sinks.h>
45 #include <spdlog/spdlog.h>
46 
47 namespace corsika {
48 
49  /*
50  * The default pattern for CORSIKA8 loggers.
51  */
52  const std::string minimal_pattern{"[%n:%^%-8l%$] %v"};
53  const std::string default_pattern{"[%n:%^%-8l%$(%s:%#)] %v"};
54  const std::string source_pattern{"[%n:%^%-8l%$(%s:%!:%#)] %v"};
55 
71  std::shared_ptr<spdlog::logger> create_logger(std::string const& name,
72  bool const defaultlog = false);
73 
87  std::shared_ptr<spdlog::logger> get_logger(std::string const& name,
88  bool const defaultlog = false);
89 
93  static inline std::shared_ptr<spdlog::logger> corsika_logger =
94  get_logger("corsika", true);
95 
96  // many of these free functions are special to the logging
97  // infrastructure so we hide them in the corsika::logging namespace.
98  namespace logging {
99 
100  // bring spdlog into the corsika::logging namespace
101  using namespace spdlog;
102 
109  auto set_default_level(level::level_enum const minlevel) -> void;
110 
117  template <typename TLogger>
118  auto add_source_info(TLogger& logger) -> void;
119 
126  template <typename TLogger>
127  auto reset_pattern(TLogger& logger) -> void;
128 
129  } // namespace logging
130 
131 // define our macro-style loggers
132 // these use the default "corsika" logger
133 #define CORSIKA_LOG_TRACE SPDLOG_TRACE
134 #define CORSIKA_LOG_DEBUG SPDLOG_DEBUG
135 #define CORSIKA_LOG_INFO SPDLOG_INFO
136 #define CORSIKA_LOG_WARN SPDLOG_WARN
137 #define CORSIKA_LOG_ERROR SPDLOG_ERROR
138 #define CORSIKA_LOG_CRITICAL SPDLOG_CRITICAL
139 
140 // and the specific logger versions
141 // these take a logger instance as their first argument
142 #define CORSIKA_LOGGER_TRACE SPDLOG_LOGGER_TRACE
143 #define CORSIKA_LOGGER_DEBUG SPDLOG_LOGGER_DEBUG
144 #define CORSIKA_LOGGER_INFO SPDLOG_LOGGER_INFO
145 #define CORSIKA_LOGGER_WARN SPDLOG_LOGGER_WARN
146 #define CORSIKA_LOGGER_ERROR SPDLOG_LOGGER_ERROR
147 #define CORSIKA_LOGGER_CRITICAL SPDLOG_LOGGER_CRITICAL
148 
149 } // namespace corsika
150 
151 #include <corsika/detail/framework/core/Logging.inl>
auto add_source_info(TLogger &logger) -> void
Add the source (filename, line no) info to the logger.
std::shared_ptr< spdlog::logger > create_logger(std::string const &name, bool const defaultlog=false)
Create a new C8-style logger.
std::shared_ptr< spdlog::logger > get_logger(std::string const &name, bool const defaultlog=false)
Get a smart pointer to an existing logger.
auto set_default_level(level::level_enum const minlevel) -> void
Set the default log level for all newly created loggers.
`, since they are used everywhere as integral part of the framework.
auto reset_pattern(TLogger &logger) -> void
Reset the logging pattern to the default.