HighFive 2.10.0
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 <functional>
13#include <string>
14#include <iostream>
15
16#include "bits/h5e_wrapper.hpp"
17#include "bits/H5Friends.hpp"
18
19namespace HighFive {
20
25 public:
26 inline SilenceHDF5(bool enable = true)
27 : _client_data(nullptr) {
28 detail::nothrow::h5e_get_auto2(H5E_DEFAULT, &_func, &_client_data);
29
30 if (enable) {
31 detail::nothrow::h5e_set_auto2(H5E_DEFAULT, nullptr, nullptr);
32 }
33 }
34
35 inline ~SilenceHDF5() {
36 detail::nothrow::h5e_set_auto2(H5E_DEFAULT, _func, _client_data);
37 }
38
39 private:
40 H5E_auto2_t _func;
41 void* _client_data;
42};
43
44#define HIGHFIVE_LOG_LEVEL_DEBUG 10
45#define HIGHFIVE_LOG_LEVEL_INFO 20
46#define HIGHFIVE_LOG_LEVEL_WARN 30
47#define HIGHFIVE_LOG_LEVEL_ERROR 40
48
49#ifndef HIGHFIVE_LOG_LEVEL
50#define HIGHFIVE_LOG_LEVEL HIGHFIVE_LOG_LEVEL_WARN
51#endif
52
59
60inline std::string to_string(LogSeverity severity) {
61 switch (severity) {
63 return "DEBUG";
65 return "INFO";
67 return "WARN";
69 return "ERROR";
70 default:
71 return "??";
72 }
73}
74
89class Logger {
90 public:
92 std::function<void(LogSeverity, const std::string&, const std::string&, int)>;
93
94 public:
95 Logger() = delete;
96 Logger(const Logger&) = delete;
97 Logger(Logger&&) = delete;
98
99 explicit Logger(callback_type cb)
100 : _cb(std::move(cb)) {}
101
102 Logger& operator=(const Logger&) = delete;
103 Logger& operator=(Logger&&) = delete;
104
105 inline void log(LogSeverity severity,
106 const std::string& message,
107 const std::string& file,
108 int line) {
109 _cb(severity, message, file, line);
110 }
111
113 _cb = std::move(cb);
114 }
115
116 private:
117 callback_type _cb;
118};
119
121 const std::string& message,
122 const std::string& file,
123 int line) {
124 std::clog << file << ": " << line << " [" << to_string(severity) << "] " << message
125 << std::endl;
126}
127
136 static Logger logger(&default_logging_callback);
137 return logger;
138}
139
142 auto& logger = get_global_logger();
143 logger.set_logging_callback(std::move(cb));
144}
145
146namespace detail {
148inline void log(LogSeverity severity,
149 const std::string& message,
150 const std::string& file,
151 int line) {
152 auto& logger = get_global_logger();
153 logger.log(severity, message, file, line);
154}
155} // namespace detail
156
157#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_DEBUG
158#define HIGHFIVE_LOG_DEBUG(message) \
159 ::HighFive::detail::log(::HighFive::LogSeverity::Debug, (message), __FILE__, __LINE__);
160
161// Useful, for the common pattern: if ...; then log something.
162#define HIGHFIVE_LOG_DEBUG_IF(cond, message) \
163 if ((cond)) { \
164 HIGHFIVE_LOG_DEBUG((message)); \
165 }
166
167#else
168#define HIGHFIVE_LOG_DEBUG(message) ;
169#define HIGHFIVE_LOG_DEBUG_IF(cond, message) ;
170#endif
171
172#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_INFO
173#define HIGHFIVE_LOG_INFO(message) \
174 ::HighFive::detail::log(::HighFive::LogSeverity::Info, (message), __FILE__, __LINE__);
175
176// Useful, for the common pattern: if ...; then log something.
177#define HIGHFIVE_LOG_INFO_IF(cond, message) \
178 if ((cond)) { \
179 HIGHFIVE_LOG_INFO((message)); \
180 }
181
182#else
183#define HIGHFIVE_LOG_INFO(message) ;
184#define HIGHFIVE_LOG_INFO_IF(cond, message) ;
185#endif
186
187
188#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_WARN
189#define HIGHFIVE_LOG_WARN(message) \
190 ::HighFive::detail::log(::HighFive::LogSeverity::Warn, (message), __FILE__, __LINE__);
191
192// Useful, for the common pattern: if ...; then log something.
193#define HIGHFIVE_LOG_WARN_IF(cond, message) \
194 if ((cond)) { \
195 HIGHFIVE_LOG_WARN((message)); \
196 }
197
198#else
199#define HIGHFIVE_LOG_WARN(message) ;
200#define HIGHFIVE_LOG_WARN_IF(cond, message) ;
201#endif
202
203#if HIGHFIVE_LOG_LEVEL <= HIGHFIVE_LOG_LEVEL_ERROR
204#define HIGHFIVE_LOG_ERROR(message) \
205 ::HighFive::detail::log(::HighFive::LogSeverity::Error, (message), __FILE__, __LINE__);
206
207// Useful, for the common pattern: if ...; then log something.
208#define HIGHFIVE_LOG_ERROR_IF(cond, message) \
209 if ((cond)) { \
210 HIGHFIVE_LOG_ERROR((message)); \
211 }
212
213#else
214#define HIGHFIVE_LOG_ERROR(message) ;
215#define HIGHFIVE_LOG_ERROR_IF(cond, message) ;
216#endif
217
218} // namespace HighFive
#define HIGHFIVE_LOG_LEVEL_WARN
Definition H5Utility.hpp:46
#define HIGHFIVE_LOG_LEVEL_ERROR
Definition H5Utility.hpp:47
#define HIGHFIVE_LOG_LEVEL_DEBUG
Definition H5Utility.hpp:44
#define HIGHFIVE_LOG_LEVEL_INFO
Definition H5Utility.hpp:45
A logger with supporting basic functionality.
Definition H5Utility.hpp:89
Logger(const Logger &)=delete
std::function< void(LogSeverity, const std::string &, const std::string &, int)> callback_type
Definition H5Utility.hpp:91
Logger & operator=(const Logger &)=delete
void set_logging_callback(callback_type cb)
Definition H5Utility.hpp:112
Logger(Logger &&)=delete
Logger(callback_type cb)
Definition H5Utility.hpp:99
Logger & operator=(Logger &&)=delete
void log(LogSeverity severity, const std::string &message, const std::string &file, int line)
Definition H5Utility.hpp:105
Utility class to disable HDF5 stack printing inside a scope.
Definition H5Utility.hpp:24
~SilenceHDF5()
Definition H5Utility.hpp:35
SilenceHDF5(bool enable=true)
Definition H5Utility.hpp:26
Definition H5_definitions.hpp:22
void register_logging_callback(Logger::callback_type cb)
Sets the callback that's used by the logger.
Definition H5Utility.hpp:141
LogSeverity
Definition H5Utility.hpp:53
void default_logging_callback(LogSeverity severity, const std::string &message, const std::string &file, int line)
Definition H5Utility.hpp:120
std::string to_string(LogSeverity severity)
Definition H5Utility.hpp:60
Logger & get_global_logger()
Obtain a reference to the logger used by HighFive.
Definition H5Utility.hpp:135