logging.h Source File

CPP API: logging.h Source File
logging.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele, Martin Siggel
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_UTILS_LOGGING_H
21 #define MIO_UTILS_LOGGING_H
22 
23 #ifdef NDEBUG
24 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
25 #else
26 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
27 #endif
28 
30 
31 // C4996: Some stdext functions used in spdlog 1.11 are marked as deprecated in version 19.38.33135.0 of MSVC. Maybe a future version of spdlog will fix this.
33 #include <spdlog/spdlog.h>
35 
36 namespace mio
37 {
38 
39 enum class LogLevel
40 {
41  trace,
42  debug,
43  info,
44  warn,
45  err,
46  critical,
47  off
48 };
49 
50 namespace details
51 {
52 inline spdlog::level::level_enum get_spdlog_level(LogLevel level)
53 {
54  spdlog::level::level_enum l;
55  switch (level) {
56  case LogLevel::trace:
57  l = spdlog::level::trace;
58  break;
59  case LogLevel::debug:
60  l = spdlog::level::debug;
61  break;
62  case LogLevel::info:
63  l = spdlog::level::info;
64  break;
65  case LogLevel::warn:
66  l = spdlog::level::warn;
67  break;
68  case LogLevel::err:
69  l = spdlog::level::err;
70  break;
71  case LogLevel::critical:
72  l = spdlog::level::critical;
73  break;
74  case LogLevel::off:
75  l = spdlog::level::off;
76  break;
77  default:
78  l = spdlog::level::info;
79  assert(false && "Unknown LogLevel.");
80  }
81  return l;
82 }
83 } // namespace details
84 
88 inline void set_log_level(LogLevel level)
89 {
90  spdlog::set_level(details::get_spdlog_level(level));
91 }
92 
93 template <typename... Args>
94 inline void log_info(spdlog::string_view_t fmt, const Args&... args)
95 {
96  spdlog::default_logger_raw()->info(fmt::runtime(fmt), args...);
97 }
98 
99 template <typename... Args>
100 inline void log_error(spdlog::string_view_t fmt, const Args&... args)
101 {
102  spdlog::default_logger_raw()->error(fmt::runtime(fmt), args...);
103 }
104 
105 template <typename... Args>
106 inline void log_critical(spdlog::string_view_t fmt, const Args&... args)
107 {
108  spdlog::default_logger_raw()->critical(fmt::runtime(fmt), args...);
109 }
110 
111 template <typename... Args>
112 inline void log_warning(spdlog::string_view_t fmt, const Args&... args)
113 {
114  spdlog::default_logger_raw()->warn(fmt::runtime(fmt), args...);
115 }
116 
117 template <typename... Args>
118 inline void log_debug(spdlog::string_view_t fmt, const Args&... args)
119 {
120 #ifndef NDEBUG
121  spdlog::default_logger_raw()->debug(fmt::runtime(fmt), args...);
122 #else
123  unused(fmt, args...);
124 #endif
125 }
126 
127 template <typename... Args>
128 inline void log(LogLevel level, spdlog::string_view_t fmt, const Args&... args)
129 {
130  spdlog::default_logger_raw()->log(details::get_spdlog_level(level), fmt::runtime(fmt), args...);
131 }
132 
133 } // namespace mio
134 
135 #endif // MIO_UTILS_LOGGING_H
#define MSVC_WARNING_POP()
Definition: compiler_diagnostics.h:44
spdlog::level::level_enum get_spdlog_level(LogLevel level)
Definition: logging.h:52
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
void log_warning(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:112
LogLevel
Definition: logging.h:40
void log(LogLevel level, spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:128
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:100
void log_debug(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:118
void unused(T &&...)
Does nothing, can be used to mark variables as not used.
Definition: compiler_diagnostics.h:30
void log_critical(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:106
void set_log_level(LogLevel level)
Sets the verbosity of the logger.
Definition: logging.h:88
void log_info(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:94
MSVC_WARNING_DISABLE_PUSH(4127) GCC_CLANG_DIAGNOSTIC(ignored "-Wexpansion-to-defined") namespace mio
Definition: random_number_generator.h:31