VTK: Common/Core/vtkLogger.h Source File

| VTK 9.3.1

vtkLogger.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
150 #ifndef vtkLogger_h
151 #define vtkLogger_h
152
153 #include "vtkObjectBase.h"
154 #include "vtkSetGet.h" // needed for macros
155
156 #include <string> // needed for std::string
157
158 #if defined(_MSC_VER)
159 #include <sal.h> // Needed for _In_z_ etc annotations
160 #endif
161
162 // this is copied from `loguru.hpp`
163 #if defined(__clang__) || defined(__GNUC__)
164 // Helper macro for declaring functions as having similar signature to printf.
165 // This allows the compiler to catch format errors at compile-time.
166 #define VTK_PRINTF_LIKE(fmtarg, firstvararg) \
167 __attribute__((__format__(__printf__, fmtarg, firstvararg)))
168 #define VTK_FORMAT_STRING_TYPE const char*
169 #elif defined(_MSC_VER)
170 #define VTK_PRINTF_LIKE(fmtarg, firstvararg)
171 #define VTK_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char*
172 #else
173 #define VTK_PRINTF_LIKE(fmtarg, firstvararg)
174 #define VTK_FORMAT_STRING_TYPE const char*
175 #endif
176
177 VTK_ABI_NAMESPACE_BEGIN
178 class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
179 {
180 public:
181 vtkBaseTypeMacro(vtkLogger, vtkObjectBase);
182 void PrintSelf(ostream& os, vtkIndent indent) override;
183
184 enum Verbosity
185 {
186 // Used to mark an invalid verbosity. Do not log to this level.
187 VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
188
189 // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
190 VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
191
192 VERBOSITY_ERROR = -2,
193 VERBOSITY_WARNING = -1,
194
195 // Normal messages. By default written to stderr.
196 VERBOSITY_INFO = 0,
197
198 // Same as VERBOSITY_INFO in every way.
199 VERBOSITY_0 = 0,
200
201 // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
202 VERBOSITY_1 = +1,
203 VERBOSITY_2 = +2,
204 VERBOSITY_3 = +3,
205 VERBOSITY_4 = +4,
206 VERBOSITY_5 = +5,
207 VERBOSITY_6 = +6,
208 VERBOSITY_7 = +7,
209 VERBOSITY_8 = +8,
210 VERBOSITY_9 = +9,
211
212 // trace level, same as VERBOSITY_9
213 VERBOSITY_TRACE = +9,
214
215 // Don not use higher verbosity levels, as that will make grepping log files harder.
216 VERBOSITY_MAX = +9,
217 };
218
256 static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
257 static void Init();
266 static void SetStderrVerbosity(Verbosity level);
267
274 static void SetInternalVerbosityLevel(Verbosity level);
275
280 enum FileMode
281 {
282 TRUNCATE,
283 APPEND
284 };
285
292 static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
293
297 static void EndLogToFile(const char* path);
298
300
303 static void SetThreadName(const std::string& name);
304 static std::string GetThreadName();
306
310 static std::string GetIdentifier(vtkObjectBase* obj);
311
316 struct Message
317 {
318 // You would generally print a Message by just concatenating the buffers without spacing.
319 // Optionally, ignore preamble and indentation.
320 Verbosity verbosity; // Already part of preamble
321 const char* filename; // Already part of preamble
322 unsigned line; // Already part of preamble
323 const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
324 const char* indentation; // Just a bunch of spacing.
325 const char* prefix; // Assertion failure info goes here (or "").
326 const char* message; // User message goes here.
327 };
328
330
333 using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
334 using CloseHandlerCallbackT = void (*)(void* user_data);
335 using FlushHandlerCallbackT = void (*)(void* user_data);
337
347 static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
348 Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
349 FlushHandlerCallbackT on_flush = nullptr);
350
355 static bool RemoveCallback(const char* id);
356
360 static bool IsEnabled();
361
367 static Verbosity GetCurrentVerbosityCutoff();
368
375 static Verbosity ConvertToVerbosity(int value);
376
383 static Verbosity ConvertToVerbosity(const char* text);
384
386
391 static void Log(
392 Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno, const char* txt);
393 static void StartScope(
394 Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname, unsigned int lineno);
395 static void EndScope(const char* id);
396 #if !defined(__WRAP__)
397 static void LogF(Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno,
398 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
399 static void StartScopeF(Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname,
400 unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
401
402 class VTKCOMMONCORE_EXPORT LogScopeRAII
403 {
404 public:
405 LogScopeRAII();
406 LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
407 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
408 ~LogScopeRAII();
409 #if defined(_MSC_VER) && _MSC_VER > 1800
410 // see loguru.hpp for the reason why this is needed on MSVC
411 LogScopeRAII(LogScopeRAII&& other)
412 : Internals(other.Internals)
413 {
414 other.Internals = nullptr;
415 }
416 #else
417 LogScopeRAII(LogScopeRAII&&) = default;
418 #endif
419
420 private:
421 LogScopeRAII(const LogScopeRAII&) = delete;
422 void operator=(const LogScopeRAII&) = delete;
423 class LSInternals;
424 LSInternals* Internals;
425 };
426 #endif
428
434 static bool EnableUnsafeSignalHandler;
435
436 protected:
437 vtkLogger();
438 ~vtkLogger() override;
439
440 private:
441 vtkLogger(const vtkLogger&) = delete;
442 void operator=(const vtkLogger&) = delete;
443 static vtkLogger::Verbosity InternalVerbosityLevel;
444 };
445
447
461 #define vtkVLogF(level, ...) \
462 ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
463 ? (void)0 \
464 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
465 #define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
466 #define vtkVLog(level, x) \
467 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
468 { \
469 vtkOStrStreamWrapper::EndlType const endl; \
470 vtkOStrStreamWrapper::UseEndl(endl); \
471 vtkOStrStreamWrapper vtkmsg; \
472 vtkmsg << "" x; \
473 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
474 vtkmsg.rdbuf()->freeze(0); \
475 }
476 #define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
478
480
492 #define vtkVLogIfF(level, cond, ...) \
493 ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
494 ? (void)0 \
495 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
496
497 #define vtkLogIfF(verbosity_name, cond, ...) \
498 vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
499
500 #define vtkVLogIf(level, cond, x) \
501 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
502 { \
503 vtkOStrStreamWrapper::EndlType endl; \
504 vtkOStrStreamWrapper::UseEndl(endl); \
505 vtkOStrStreamWrapper vtkmsg; \
506 vtkmsg << "" x; \
507 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
508 vtkmsg.rdbuf()->freeze(0); \
509 }
510 #define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
512
513 #define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
514 #define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
515 #define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
516
517 #define vtkVLogScopeF(level, ...) \
518 auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
519 ? vtkLogger::LogScopeRAII() \
520 : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
521
522 #define vtkLogScopeF(verbosity_name, ...) \
523 vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
524
525 #define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
526 #define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
527
529
533 #define vtkLogStartScope(verbosity_name, id) \
534 vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
535 #define vtkLogEndScope(id) vtkLogger::EndScope(id)
536
537 #define vtkLogStartScopeF(verbosity_name, id, ...) \
538 vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
539
540 #define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
541 #define vtkVLogStartScopeF(level, id, ...) \
542 vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
544
550 #define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
551
552 VTK_ABI_NAMESPACE_END
553 #endif
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:38
vtkLogger::LogScopeRAII
Definition: vtkLogger.h:403
vtkLogger::LogScopeRAII::LogScopeRAII
LogScopeRAII()
vtkLogger::LogScopeRAII::LogScopeRAII
LogScopeRAII(vtkLogger::Verbosity verbosity, const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(5
vtkLogger::LogScopeRAII::LogScopeRAII
LogScopeRAII(LogScopeRAII &&)=default
vtkLogger
logging framework for use in VTK and in applications based on VTK
Definition: vtkLogger.h:179
vtkLogger::EnableUnsafeSignalHandler
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals,...
Definition: vtkLogger.h:434
vtkLogger::ConvertToVerbosity
static Verbosity ConvertToVerbosity(int value)
Convenience function to convert an integer to matching verbosity level.
vtkLogger::CloseHandlerCallbackT
void(*)(void *user_data) CloseHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:334
vtkLogger::LogHandlerCallbackT
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:333
vtkLogger::RemoveCallback
static bool RemoveCallback(const char *id)
Remove a callback using the id specified.
vtkLogger::GetIdentifier
static std::string GetIdentifier(vtkObjectBase *obj)
Returns a printable string for a vtkObjectBase instance.
vtkLogger::EndScope
static void EndScope(const char *id)
vtkLogger::vtkBaseTypeMacro
vtkBaseTypeMacro(vtkLogger, vtkObjectBase)
vtkLogger::GetThreadName
static std::string GetThreadName()
Get/Set the name to identify the current thread in the log output.
vtkLogger::SetInternalVerbosityLevel
static void SetInternalVerbosityLevel(Verbosity level)
Set internal messages verbosity level.
vtkLogger::FlushHandlerCallbackT
void(*)(void *user_data) FlushHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:335
vtkLogger::AddCallback
static void AddCallback(const char *id, LogHandlerCallbackT callback, void *user_data, Verbosity verbosity, CloseHandlerCallbackT on_close=nullptr, FlushHandlerCallbackT on_flush=nullptr)
Add a callback to call on each log message with a verbosity less or equal to the given one.
vtkLogger::Verbosity
Verbosity
Definition: vtkLogger.h:185
vtkLogger::vtkLogger
vtkLogger()
vtkLogger::IsEnabled
static bool IsEnabled()
Returns true if VTK is built with logging support enabled.
vtkLogger::SetThreadName
static void SetThreadName(const std::string &name)
Get/Set the name to identify the current thread in the log output.
vtkLogger::ConvertToVerbosity
static Verbosity ConvertToVerbosity(const char *text)
Convenience function to convert a string to matching verbosity level.
vtkLogger::Init
static void Init()
Initializes logging.
vtkLogger::EndLogToFile
static void EndLogToFile(const char *path)
Stop logging to a file at the given path.
vtkLogger::Init
static void Init(int &argc, char *argv[], const char *verbosity_flag="-v")
Initializes logging.
vtkLogger::Log
static void Log(Verbosity verbosity, VTK_FILEPATH const char *fname, unsigned int lineno, const char *txt)
vtkLogger::SetStderrVerbosity
static void SetStderrVerbosity(Verbosity level)
Set the verbosity level for the output logged to stderr.
vtkLogger::FileMode
FileMode
Support log file modes: TRUNCATE truncates the file clearing any existing contents while APPEND appen...
Definition: vtkLogger.h:281
vtkLogger::TRUNCATE
@ TRUNCATE
Definition: vtkLogger.h:282
vtkLogger::LogF
static void LogF(Verbosity verbosity, VTK_FILEPATH const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(4
vtkLogger::GetCurrentVerbosityCutoff
static Verbosity GetCurrentVerbosityCutoff()
Returns the maximum verbosity of all log outputs.
vtkLogger::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkLogger::StartScope
static void StartScope(Verbosity verbosity, const char *id, VTK_FILEPATH const char *fname, unsigned int lineno)
vtkLogger::~vtkLogger
~vtkLogger() override
vtkLogger::LogToFile
static void LogToFile(const char *path, FileMode filemode, Verbosity verbosity)
Enable logging to a file at the given path.
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:72
vtkObjectBase::operator=
void operator=(const vtkObjectBase &)
Definition: vtkObjectBase.h:332
vtkX3D::level
@ level
Definition: vtkX3D.h:395
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkX3D::name
@ name
Definition: vtkX3D.h:219
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtkLogger::Message
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition: vtkLogger.h:317
vtkLogger::Message::filename
const char * filename
Definition: vtkLogger.h:321
vtkLogger::Message::preamble
const char * preamble
Definition: vtkLogger.h:323
vtkLogger::Message::verbosity
Verbosity verbosity
Definition: vtkLogger.h:320
vtkLogger::Message::message
const char * message
Definition: vtkLogger.h:326
vtkLogger::Message::indentation
const char * indentation
Definition: vtkLogger.h:324
vtkLogger::Message::line
unsigned line
Definition: vtkLogger.h:322
vtkLogger::Message::prefix
const char * prefix
Definition: vtkLogger.h:325
VTK_FORMAT_STRING_TYPE
#define VTK_FORMAT_STRING_TYPE
Definition: vtkLogger.h:174
VTK_PRINTF_LIKE
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition: vtkLogger.h:173
vtkObjectBase.h
VTK_FILEPATH
#define VTK_FILEPATH
Definition: vtkWrappingHints.h:34