Open3D (C++ API)  0.16.0
Logging.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2021 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26
27#pragma once
28
29#include <functional>
30#include <memory>
31#include <string>
32
33// NVCC does not support deprecated attribute on Windows prior to v11.
34#if defined(__CUDACC__) && defined(_MSC_VER) && __CUDACC_VER_MAJOR__ < 11
35#ifndef FMT_DEPRECATED
36#define FMT_DEPRECATED
37#endif
38#endif
39
40#include <fmt/core.h>
41#include <fmt/printf.h>
42#include <fmt/ranges.h>
43
44#define DEFAULT_IO_BUFFER_SIZE 1024
45
46#include "open3d/Macro.h"
47
48// Mimic "macro in namespace" by concatenating `utility::` and a macro.
49// Ref: https://stackoverflow.com/a/11791202
50//
51// We avoid using (format, ...) since in this case __VA_ARGS__ can be
52// empty, and the behavior of pruning trailing comma with ##__VA_ARGS__ is not
53// officially standard.
54// Ref: https://stackoverflow.com/a/28074198
55//
56// __PRETTY_FUNCTION__ has to be converted, otherwise a bug regarding [noreturn]
57// will be triggered.
58// Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94742
59
60// LogError throws now a runtime_error with the given error message. This
61// should be used if there is no point in continuing the given algorithm at
62// some point and the error is not returned in another way (e.g., via a
63// bool/int as return value).
64//
65// Usage : utility::LogError(format_string, arg0, arg1, ...);
66// Example: utility::LogError("name: {}, age: {}", "dog", 5);
67#define LogError(...) \
68 Logger::LogError_(__FILE__, __LINE__, \
69 static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
70
71// LogWarning is used if an error occurs, but the error is also signaled
72// via a return value (i.e., there is no need to throw an exception). This
73// warning should further be used, if the algorithms encounters a state
74// that does not break its continuation, but the output is likely not to be
75// what the user expected.
76//
77// Usage : utility::LogWarning(format_string, arg0, arg1, ...);
78// Example: utility::LogWarning("name: {}, age: {}", "dog", 5);
79#define LogWarning(...) \
80 Logger::LogWarning_(__FILE__, __LINE__, \
81 static_cast<const char *>(OPEN3D_FUNCTION), \
82 __VA_ARGS__)
83
84// LogInfo is used to inform the user with expected output, e.g, pressed a
85// key in the visualizer prints helping information.
86//
87// Usage : utility::LogInfo(format_string, arg0, arg1, ...);
88// Example: utility::LogInfo("name: {}, age: {}", "dog", 5);
89#define LogInfo(...) \
90 Logger::LogInfo_(__FILE__, __LINE__, \
91 static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
92
93// LogDebug is used to print debug/additional information on the state of
94// the algorithm.
95//
96// Usage : utility::LogDebug(format_string, arg0, arg1, ...);
97// Example: utility::LogDebug("name: {}, age: {}", "dog", 5);
98#define LogDebug(...) \
99 Logger::LogDebug_(__FILE__, __LINE__, \
100 static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
101
102namespace open3d {
103namespace utility {
104
105enum class VerbosityLevel {
110 Error = 0,
116 Warning = 1,
119 Info = 2,
122 Debug = 3,
123};
124
126class Logger {
127public:
128 Logger(Logger const &) = delete;
129 void operator=(Logger const &) = delete;
130
132 static Logger &GetInstance();
133
140 void SetPrintFunction(std::function<void(const std::string &)> print_fcn);
141
143 void ResetPrintFunction();
144
146 const std::function<void(const std::string &)> GetPrintFunction();
147
152 void SetVerbosityLevel(VerbosityLevel verbosity_level);
153
156
157 template <typename... Args>
158 static void LogError_ [[noreturn]] (const char *file,
159 int line,
160 const char *function,
161 const char *format,
162 Args &&... args) {
163 if (sizeof...(Args) > 0) {
164 Logger::GetInstance().VError(
165 file, line, function,
166 FormatArgs(format, fmt::make_format_args(args...)));
167 } else {
168 Logger::GetInstance().VError(file, line, function,
169 std::string(format));
170 }
171 }
172 template <typename... Args>
173 static void LogWarning_(const char *file,
174 int line,
175 const char *function,
176 const char *format,
177 Args &&... args) {
180 if (sizeof...(Args) > 0) {
181 Logger::GetInstance().VWarning(
182 file, line, function,
183 FormatArgs(format, fmt::make_format_args(args...)));
184 } else {
185 Logger::GetInstance().VWarning(file, line, function,
186 std::string(format));
187 }
188 }
189 }
190 template <typename... Args>
191 static void LogInfo_(const char *file,
192 int line,
193 const char *function,
194 const char *format,
195 Args &&... args) {
197 if (sizeof...(Args) > 0) {
198 Logger::GetInstance().VInfo(
199 file, line, function,
200 FormatArgs(format, fmt::make_format_args(args...)));
201 } else {
202 Logger::GetInstance().VInfo(file, line, function,
203 std::string(format));
204 }
205 }
206 }
207 template <typename... Args>
208 static void LogDebug_(const char *file,
209 int line,
210 const char *function,
211 const char *format,
212 Args &&... args) {
215 if (sizeof...(Args) > 0) {
216 Logger::GetInstance().VDebug(
217 file, line, function,
218 FormatArgs(format, fmt::make_format_args(args...)));
219 } else {
220 Logger::GetInstance().VDebug(file, line, function,
221 std::string(format));
222 }
223 }
224 }
225
226private:
227 Logger();
228 static std::string FormatArgs(const char *format, fmt::format_args args) {
229 std::string err_msg = fmt::vformat(format, args);
230 return err_msg;
231 }
232 void VError [[noreturn]] (const char *file,
233 int line,
234 const char *function,
235 const std::string &message) const;
236 void VWarning(const char *file,
237 int line,
238 const char *function,
239 const std::string &message) const;
240 void VInfo(const char *file,
241 int line,
242 const char *function,
243 const std::string &message) const;
244 void VDebug(const char *file,
245 int line,
246 const char *function,
247 const std::string &message) const;
248
249private:
250 struct Impl;
251 std::unique_ptr<Impl> impl_;
252};
253
259
262
264public:
265 VerbosityContextManager(VerbosityLevel level) : level_(level) {}
266
267 void Enter() {
268 level_backup_ = Logger::GetInstance().GetVerbosityLevel();
270 }
271
272 void Exit() { Logger::GetInstance().SetVerbosityLevel(level_backup_); }
273
274private:
275 VerbosityLevel level_;
276 VerbosityLevel level_backup_;
277};
278
279} // namespace utility
280} // namespace open3d
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:214
Logger class should be used as a global singleton object (GetInstance()).
Definition: Logging.h:126
Logger(Logger const &)=delete
void operator=(Logger const &)=delete
const std::function< void(const std::string &)> GetPrintFunction()
Get the print function used by the Logger.
Definition: Logging.cpp:135
VerbosityLevel GetVerbosityLevel() const
Get global verbosity level of Open3D.
Definition: Logging.cpp:147
void ResetPrintFunction()
Reset the print function to the default one (print to console).
Definition: Logging.cpp:139
void SetPrintFunction(std::function< void(const std::string &)> print_fcn)
Definition: Logging.cpp:130
static void LogError_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:158
static void LogWarning_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:173
static void LogDebug_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:208
static void LogInfo_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:191
void SetVerbosityLevel(VerbosityLevel verbosity_level)
Definition: Logging.cpp:143
static Logger & GetInstance()
Get Logger global singleton instance.
Definition: Logging.cpp:87
void Exit()
Definition: Logging.h:272
VerbosityContextManager(VerbosityLevel level)
Definition: Logging.h:265
void Enter()
Definition: Logging.h:267
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:269
void SetVerbosityLevel(VerbosityLevel level)
Definition: Logging.cpp:151
VerbosityLevel
Definition: Logging.h:105
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of Open3D.
Definition: Logging.cpp:155
Definition: PinholeCameraIntrinsic.cpp:35