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 #include <filesystem>
37 
41 template <>
42 struct fmt::formatter<std::filesystem::path> : formatter<std::string_view> {
43  template <typename FormatContext>
44  auto format(const std::filesystem::path& path, FormatContext& ctxt) const
45  {
46  return formatter<std::string_view>::format(path.string(), ctxt);
47  }
48 };
49 
50 namespace mio
51 {
52 
53 enum class LogLevel
54 {
55  trace,
56  debug,
57  info,
58  warn,
59  err,
60  critical,
61  off
62 };
63 
64 namespace details
65 {
66 inline spdlog::level::level_enum get_spdlog_level(LogLevel level)
67 {
68  spdlog::level::level_enum l;
69  switch (level) {
70  case LogLevel::trace:
71  l = spdlog::level::trace;
72  break;
73  case LogLevel::debug:
74  l = spdlog::level::debug;
75  break;
76  case LogLevel::info:
77  l = spdlog::level::info;
78  break;
79  case LogLevel::warn:
80  l = spdlog::level::warn;
81  break;
82  case LogLevel::err:
83  l = spdlog::level::err;
84  break;
85  case LogLevel::critical:
86  l = spdlog::level::critical;
87  break;
88  case LogLevel::off:
89  l = spdlog::level::off;
90  break;
91  default:
92  l = spdlog::level::info;
93  assert(false && "Unknown LogLevel.");
94  }
95  return l;
96 }
97 } // namespace details
98 
102 inline void set_log_level(LogLevel level)
103 {
104  spdlog::set_level(details::get_spdlog_level(level));
105 }
106 
107 template <typename... Args>
108 inline void log_info(spdlog::string_view_t fmt, const Args&... args)
109 {
110  spdlog::default_logger_raw()->info(fmt::runtime(fmt), args...);
111 }
112 
113 template <typename... Args>
114 inline void log_error(spdlog::string_view_t fmt, const Args&... args)
115 {
116  spdlog::default_logger_raw()->error(fmt::runtime(fmt), args...);
117 }
118 
119 template <typename... Args>
120 inline void log_critical(spdlog::string_view_t fmt, const Args&... args)
121 {
122  spdlog::default_logger_raw()->critical(fmt::runtime(fmt), args...);
123 }
124 
125 template <typename... Args>
126 inline void log_warning(spdlog::string_view_t fmt, const Args&... args)
127 {
128  spdlog::default_logger_raw()->warn(fmt::runtime(fmt), args...);
129 }
130 
131 template <typename... Args>
132 inline void log_debug(spdlog::string_view_t fmt, const Args&... args)
133 {
134 #ifndef NDEBUG
135  spdlog::default_logger_raw()->debug(fmt::runtime(fmt), args...);
136 #else
137  unused(fmt, args...);
138 #endif
139 }
140 
141 template <typename... Args>
142 inline void log(LogLevel level, spdlog::string_view_t fmt, const Args&... args)
143 {
144  spdlog::default_logger_raw()->log(details::get_spdlog_level(level), fmt::runtime(fmt), args...);
145 }
146 
147 } // namespace mio
148 
149 #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:66
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:126
LogLevel
Definition: logging.h:54
void log(LogLevel level, spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:142
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:114
void log_debug(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:132
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:120
void set_log_level(LogLevel level)
Sets the verbosity of the logger.
Definition: logging.h:102
void log_info(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:108
Definition: io.h:95
MSVC_WARNING_DISABLE_PUSH(4127) GCC_CLANG_DIAGNOSTIC(ignored "-Wexpansion-to-defined") namespace mio
Definition: random_number_generator.h:31
auto format(const std::filesystem::path &path, FormatContext &ctxt) const
Definition: logging.h:44