HighFive 2.7.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Utility.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Blue Brain Project - EPFL (CH)
3 *
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9
10#pragma once
11
12#include <H5Epublic.h>
13#include <functional>
14#include <string>
15#include <iostream>
16
17#include "bits/H5Friends.hpp"
18
19namespace HighFive {
20
25 public:
26 inline SilenceHDF5(bool enable = true)
27 : _client_data(nullptr) {
28 H5Eget_auto2(H5E_DEFAULT, &_func, &_client_data);
29 if (enable)
30 H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
31 }
32
33 inline ~SilenceHDF5() {
34 H5Eset_auto2(H5E_DEFAULT, _func, _client_data);
35 }
36
37 private:
38 H5E_auto2_t _func;
39 void* _client_data;
40};
41
42#define HIGHFIVE_LOG_LEVEL_DEBUG 10
43#define HIGHFIVE_LOG_LEVEL_INFO 20
44#define HIGHFIVE_LOG_LEVEL_WARN 30
45#define HIGHFIVE_LOG_LEVEL_ERROR 40
46
47#ifndef HIGHFIVE_LOG_LEVEL
48#define HIGHFIVE_LOG_LEVEL HIGHFIVE_LOG_LEVEL_WARN
49#endif
50
51enum class LogSeverity {
56};
57
58inline std::string to_string(LogSeverity severity) {
59 switch (severity) {
61 return "DEBUG";
63 return "INFO";
65 return "WARN";
67 return "ERROR";
68 default:
69 return "??";
70 }
71}
72
87class Logger {
88 public:
90 std::function<void(LogSeverity, const std::string&, const std::string&, int)>;
91
92 public:
93 Logger() = delete;
94 Logger(const Logger&) = delete;
95 Logger(Logger&&) = delete;
96
97 explicit Logger(callback_type cb)
98 : _cb(std::move(cb)) {}
99
100 Logger& operator=(const Logger&) = delete;
101 Logger& operator=(Logger&&) = delete;
102
103 inline void log(LogSeverity severity,
104 const std::string& message,
105 const std::string& file,
106 int line) {
107 _cb(severity, message, file, line);
108 }
109
111 _cb = std::move(cb);
112 }
113
114 private:
115 callback_type _cb;
116};
117
119 const std::string& message,
120 const std::string& file,
121 int line) {
122 std::clog << file << ": " << line << " :: " << to_string(severity) << message << std::endl;
123}
124
133 static Logger logger(&default_logging_callback);
134 return logger;
135}
136
139 auto& logger = get_global_logger();
140 logger.set_logging_callback(std::move(cb));
141}
142
143namespace detail {
145inline void log(LogSeverity severity,
146 const std::string& message,
147 const std::string& file,
148 int line) {
149 auto& logger = get_global_logger();
150 logger.log(severity, message, file, line);
151}
152} // namespace detail
153
154#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_DEBUG
155#define HIGHFIVE_LOG_DEBUG(message) \
156 ::HighFive::detail::log(::HighFive::LogSeverity::Debug, (message), __FILE__, __LINE__);
157
158// Useful, for the common pattern: if ...; then log something.
159#define HIGHFIVE_LOG_DEBUG_IF(cond, message) \
160 if ((cond)) { \
161 HIGHFIVE_LOG_DEBUG((message)); \
162 }
163
164#else
165#define HIGHFIVE_LOG_DEBUG(message) ;
166#define HIGHFIVE_LOG_DEBUG_IF(cond, message) ;
167#endif
168
169#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_INFO
170#define HIGHFIVE_LOG_INFO(message) \
171 ::HighFive::detail::log(::HighFive::LogSeverity::Info, (message), __FILE__, __LINE__);
172
173// Useful, for the common pattern: if ...; then log something.
174#define HIGHFIVE_LOG_INFO_IF(cond, message) \
175 if ((cond)) { \
176 HIGHFIVE_LOG_INFO((message)); \
177 }
178
179#else
180#define HIGHFIVE_LOG_INFO(message) ;
181#define HIGHFIVE_LOG_INFO_IF(cond, message) ;
182#endif
183
184
185#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_WARN
186#define HIGHFIVE_LOG_WARN(message) \
187 ::HighFive::detail::log(::HighFive::LogSeverity::Warn, (message), __FILE__, __LINE__);
188
189// Useful, for the common pattern: if ...; then log something.
190#define HIGHFIVE_LOG_WARN_IF(cond, message) \
191 if ((cond)) { \
192 HIGHFIVE_LOG_WARN((message)); \
193 }
194
195#else
196#define HIGHFIVE_LOG_WARN(message) ;
197#define HIGHFIVE_LOG_WARN_IF(cond, message) ;
198#endif
199
200#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_ERROR
201#define HIGHFIVE_LOG_ERROR(message) \
202 ::HighFive::detail::log(::HighFive::LogSeverity::Error, (message), __FILE__, __LINE__);
203
204// Useful, for the common pattern: if ...; then log something.
205#define HIGHFIVE_LOG_ERROR_IF(cond, message) \
206 if ((cond)) { \
207 HIGHFIVE_LOG_ERROR((message)); \
208 }
209
210#else
211#define HIGHFIVE_LOG_ERROR(message) ;
212#define HIGHFIVE_LOG_ERROR_IF(cond, message) ;
213#endif
214
215} // namespace HighFive
#define HIGHFIVE_LOG_LEVEL_WARN
Definition H5Utility.hpp:44
#define HIGHFIVE_LOG_LEVEL_ERROR
Definition H5Utility.hpp:45
#define HIGHFIVE_LOG_LEVEL_DEBUG
Definition H5Utility.hpp:42
#define HIGHFIVE_LOG_LEVEL_INFO
Definition H5Utility.hpp:43
A logger with supporting basic functionality.
Definition H5Utility.hpp:87
Logger(const Logger &)=delete
Logger & operator=(const Logger &)=delete
std::function< void(LogSeverity, const std::string &, const std::string &, int)> callback_type
Definition H5Utility.hpp:90
void set_logging_callback(callback_type cb)
Definition H5Utility.hpp:110
Logger(Logger &&)=delete
Logger(callback_type cb)
Definition H5Utility.hpp:97
Logger & operator=(Logger &&)=delete
void log(LogSeverity severity, const std::string &message, const std::string &file, int line)
Definition H5Utility.hpp:103
Utility class to disable HDF5 stack printing inside a scope.
Definition H5Utility.hpp:24
~SilenceHDF5()
Definition H5Utility.hpp:33
SilenceHDF5(bool enable=true)
Definition H5Utility.hpp:26
Definition H5_definitions.hpp:15
void register_logging_callback(Logger::callback_type cb)
Sets the callback that's used by the logger.
Definition H5Utility.hpp:138
LogSeverity
Definition H5Utility.hpp:51
void default_logging_callback(LogSeverity severity, const std::string &message, const std::string &file, int line)
Definition H5Utility.hpp:118
std::string to_string(LogSeverity severity)
Definition H5Utility.hpp:58
Logger & get_global_logger()
Obtain a reference to the logger used by HighFive.
Definition H5Utility.hpp:132