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