Open3D (C++ API)  0.17.0
Logging.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// Copyright (c) 2018-2023 www.open3d.org
5// SPDX-License-Identifier: MIT
6// ----------------------------------------------------------------------------
7
8#pragma once
9
10#include <functional>
11#include <memory>
12#include <string>
13
14// NVCC does not support deprecated attribute on Windows prior to v11.
15#if defined(__CUDACC__) && defined(_MSC_VER) && __CUDACC_VER_MAJOR__ < 11
16#ifndef FMT_DEPRECATED
17#define FMT_DEPRECATED
18#endif
19#endif
20
21#include <fmt/core.h>
22#include <fmt/printf.h>
23#include <fmt/ranges.h>
24
25#define DEFAULT_IO_BUFFER_SIZE 1024
26
27#include "open3d/Macro.h"
28
29// Mimic "macro in namespace" by concatenating `utility::` and a macro.
30// Ref: https://stackoverflow.com/a/11791202
31//
32// We avoid using (format, ...) since in this case __VA_ARGS__ can be
33// empty, and the behavior of pruning trailing comma with ##__VA_ARGS__ is not
34// officially standard.
35// Ref: https://stackoverflow.com/a/28074198
36//
37// __PRETTY_FUNCTION__ has to be converted, otherwise a bug regarding [noreturn]
38// will be triggered.
39// Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94742
40
41// LogError throws now a runtime_error with the given error message. This
42// should be used if there is no point in continuing the given algorithm at
43// some point and the error is not returned in another way (e.g., via a
44// bool/int as return value).
45//
46// Usage : utility::LogError(format_string, arg0, arg1, ...);
47// Example: utility::LogError("name: {}, age: {}", "dog", 5);
48#define LogError(...) \
49 Logger::LogError_(__FILE__, __LINE__, \
50 static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
51
52// LogWarning is used if an error occurs, but the error is also signaled
53// via a return value (i.e., there is no need to throw an exception). This
54// warning should further be used, if the algorithms encounters a state
55// that does not break its continuation, but the output is likely not to be
56// what the user expected.
57//
58// Usage : utility::LogWarning(format_string, arg0, arg1, ...);
59// Example: utility::LogWarning("name: {}, age: {}", "dog", 5);
60#define LogWarning(...) \
61 Logger::LogWarning_(__FILE__, __LINE__, \
62 static_cast<const char *>(OPEN3D_FUNCTION), \
63 __VA_ARGS__)
64
65// LogInfo is used to inform the user with expected output, e.g, pressed a
66// key in the visualizer prints helping information.
67//
68// Usage : utility::LogInfo(format_string, arg0, arg1, ...);
69// Example: utility::LogInfo("name: {}, age: {}", "dog", 5);
70#define LogInfo(...) \
71 Logger::LogInfo_(__FILE__, __LINE__, \
72 static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
73
74// LogDebug is used to print debug/additional information on the state of
75// the algorithm.
76//
77// Usage : utility::LogDebug(format_string, arg0, arg1, ...);
78// Example: utility::LogDebug("name: {}, age: {}", "dog", 5);
79#define LogDebug(...) \
80 Logger::LogDebug_(__FILE__, __LINE__, \
81 static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
82
83namespace open3d {
84namespace utility {
85
86enum class VerbosityLevel {
91 Error = 0,
97 Warning = 1,
100 Info = 2,
103 Debug = 3,
104};
105
107class Logger {
108public:
109 Logger(Logger const &) = delete;
110 void operator=(Logger const &) = delete;
111
113 static Logger &GetInstance();
114
121 void SetPrintFunction(std::function<void(const std::string &)> print_fcn);
122
124 void ResetPrintFunction();
125
127 const std::function<void(const std::string &)> GetPrintFunction();
128
133 void SetVerbosityLevel(VerbosityLevel verbosity_level);
134
137
138 template <typename... Args>
139 static void LogError_ [[noreturn]] (const char *file,
140 int line,
141 const char *function,
142 const char *format,
143 Args &&... args) {
144 if (sizeof...(Args) > 0) {
145 Logger::GetInstance().VError(
146 file, line, function,
147 FormatArgs(format, fmt::make_format_args(args...)));
148 } else {
149 Logger::GetInstance().VError(file, line, function,
150 std::string(format));
151 }
152 }
153 template <typename... Args>
154 static void LogWarning_(const char *file,
155 int line,
156 const char *function,
157 const char *format,
158 Args &&... args) {
161 if (sizeof...(Args) > 0) {
162 Logger::GetInstance().VWarning(
163 file, line, function,
164 FormatArgs(format, fmt::make_format_args(args...)));
165 } else {
166 Logger::GetInstance().VWarning(file, line, function,
167 std::string(format));
168 }
169 }
170 }
171 template <typename... Args>
172 static void LogInfo_(const char *file,
173 int line,
174 const char *function,
175 const char *format,
176 Args &&... args) {
178 if (sizeof...(Args) > 0) {
179 Logger::GetInstance().VInfo(
180 file, line, function,
181 FormatArgs(format, fmt::make_format_args(args...)));
182 } else {
183 Logger::GetInstance().VInfo(file, line, function,
184 std::string(format));
185 }
186 }
187 }
188 template <typename... Args>
189 static void LogDebug_(const char *file,
190 int line,
191 const char *function,
192 const char *format,
193 Args &&... args) {
196 if (sizeof...(Args) > 0) {
197 Logger::GetInstance().VDebug(
198 file, line, function,
199 FormatArgs(format, fmt::make_format_args(args...)));
200 } else {
201 Logger::GetInstance().VDebug(file, line, function,
202 std::string(format));
203 }
204 }
205 }
206
207private:
208 Logger();
209 static std::string FormatArgs(const char *format, fmt::format_args args) {
210 std::string err_msg = fmt::vformat(format, args);
211 return err_msg;
212 }
213 void VError [[noreturn]] (const char *file,
214 int line,
215 const char *function,
216 const std::string &message) const;
217 void VWarning(const char *file,
218 int line,
219 const char *function,
220 const std::string &message) const;
221 void VInfo(const char *file,
222 int line,
223 const char *function,
224 const std::string &message) const;
225 void VDebug(const char *file,
226 int line,
227 const char *function,
228 const std::string &message) const;
229
230private:
231 struct Impl;
232 std::unique_ptr<Impl> impl_;
233};
234
240
243
245public:
246 VerbosityContextManager(VerbosityLevel level) : level_(level) {}
247
248 void Enter() {
249 level_backup_ = Logger::GetInstance().GetVerbosityLevel();
251 }
252
253 void Exit() { Logger::GetInstance().SetVerbosityLevel(level_backup_); }
254
255private:
256 VerbosityLevel level_;
257 VerbosityLevel level_backup_;
258};
259
260} // namespace utility
261} // namespace open3d
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:195
Logger class should be used as a global singleton object (GetInstance()).
Definition: Logging.h:107
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:116
VerbosityLevel GetVerbosityLevel() const
Get global verbosity level of Open3D.
Definition: Logging.cpp:128
void ResetPrintFunction()
Reset the print function to the default one (print to console).
Definition: Logging.cpp:120
void SetPrintFunction(std::function< void(const std::string &)> print_fcn)
Definition: Logging.cpp:111
static void LogError_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:139
static void LogWarning_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:154
static void LogDebug_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:189
static void LogInfo_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:172
void SetVerbosityLevel(VerbosityLevel verbosity_level)
Definition: Logging.cpp:124
static Logger & GetInstance()
Get Logger global singleton instance.
Definition: Logging.cpp:68
void Exit()
Definition: Logging.h:253
VerbosityContextManager(VerbosityLevel level)
Definition: Logging.h:246
void Enter()
Definition: Logging.h:248
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:250
void SetVerbosityLevel(VerbosityLevel level)
Definition: Logging.cpp:132
VerbosityLevel
Definition: Logging.h:86
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of Open3D.
Definition: Logging.cpp:136
Definition: PinholeCameraIntrinsic.cpp:16