VTK  9.3.20240424
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
177#ifndef vtkLogger_h
178#define vtkLogger_h
179
180#include "vtkObjectBase.h"
181#include "vtkSetGet.h" // needed for macros
182
183#include <string> // needed for std::string
184
185#if defined(_MSC_VER)
186#include <sal.h> // Needed for _In_z_ etc annotations
187#endif
188
189// this is copied from `loguru.hpp`
190#if defined(__clang__) || defined(__GNUC__)
191// Helper macro for declaring functions as having similar signature to printf.
192// This allows the compiler to catch format errors at compile-time.
193#define VTK_PRINTF_LIKE(fmtarg, firstvararg) \
194 __attribute__((__format__(__printf__, fmtarg, firstvararg)))
195#define VTK_FORMAT_STRING_TYPE const char*
196#elif defined(_MSC_VER)
197#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
198#define VTK_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char*
199#else
200#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
201#define VTK_FORMAT_STRING_TYPE const char*
202#endif
203
204VTK_ABI_NAMESPACE_BEGIN
205class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
206{
207public:
209 void PrintSelf(ostream& os, vtkIndent indent) override;
210
212 {
213 // Used to mark an invalid verbosity. Do not log to this level.
214 VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
215
216 // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
217 VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
218
219 VERBOSITY_ERROR = -2,
220 VERBOSITY_WARNING = -1,
221
222 // Normal messages. By default written to stderr.
223 VERBOSITY_INFO = 0,
224
225 // Same as VERBOSITY_INFO in every way.
226 VERBOSITY_0 = 0,
227
228 // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
229 VERBOSITY_1 = +1,
230 VERBOSITY_2 = +2,
231 VERBOSITY_3 = +3,
232 VERBOSITY_4 = +4,
233 VERBOSITY_5 = +5,
234 VERBOSITY_6 = +6,
235 VERBOSITY_7 = +7,
236 VERBOSITY_8 = +8,
237 VERBOSITY_9 = +9,
238
239 // trace level, same as VERBOSITY_9
240 VERBOSITY_TRACE = +9,
241
242 // Don not use higher verbosity levels, as that will make grepping log files harder.
243 VERBOSITY_MAX = +9,
244 };
245
283 static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
284 static void Init();
293 static void SetStderrVerbosity(Verbosity level);
294
302
308 {
310 APPEND
311 };
312
319 static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
320
324 static void EndLogToFile(const char* path);
325
327
330 static void SetThreadName(const std::string& name);
331 static std::string GetThreadName();
333
337 static std::string GetIdentifier(vtkObjectBase* obj);
338
343 struct Message
344 {
345 // You would generally print a Message by just concatenating the buffers without spacing.
346 // Optionally, ignore preamble and indentation.
347 Verbosity verbosity; // Already part of preamble
348 const char* filename; // Already part of preamble
349 unsigned line; // Already part of preamble
350 const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
351 const char* indentation; // Just a bunch of spacing.
352 const char* prefix; // Assertion failure info goes here (or "").
353 const char* message; // User message goes here.
354 };
355
357
360 using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
361 using CloseHandlerCallbackT = void (*)(void* user_data);
362 using FlushHandlerCallbackT = void (*)(void* user_data);
364
374 static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
375 Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
376 FlushHandlerCallbackT on_flush = nullptr);
377
382 static bool RemoveCallback(const char* id);
383
387 static bool IsEnabled();
388
395
402 static Verbosity ConvertToVerbosity(int value);
403
410 static Verbosity ConvertToVerbosity(const char* text);
411
413
418 static void Log(
419 Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno, const char* txt);
420 static void StartScope(
421 Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname, unsigned int lineno);
422 static void EndScope(const char* id);
423#if !defined(__WRAP__)
424 static void LogF(Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno,
425 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
426 static void StartScopeF(Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname,
427 unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
428
429 class VTKCOMMONCORE_EXPORT LogScopeRAII
430 {
431 public:
433 LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
434 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
436#if defined(_MSC_VER) && _MSC_VER > 1800
437 // see loguru.hpp for the reason why this is needed on MSVC
439 : Internals(other.Internals)
440 {
441 other.Internals = nullptr;
442 }
443#else
445#endif
446
447 private:
448 LogScopeRAII(const LogScopeRAII&) = delete;
449 void operator=(const LogScopeRAII&) = delete;
450 class LSInternals;
451 LSInternals* Internals;
452 };
453#endif
455
469
470protected:
472 ~vtkLogger() override;
473
474private:
475 vtkLogger(const vtkLogger&) = delete;
476 void operator=(const vtkLogger&) = delete;
477 static vtkLogger::Verbosity InternalVerbosityLevel;
478};
479
481
495#define vtkVLogF(level, ...) \
496 ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
497 ? (void)0 \
498 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
499#define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
500#define vtkVLog(level, x) \
501 do \
502 { \
503 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
504 { \
505 vtkOStrStreamWrapper::EndlType const endl; \
506 vtkOStrStreamWrapper::UseEndl(endl); \
507 vtkOStrStreamWrapper vtkmsg; \
508 vtkmsg << "" x; \
509 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
510 vtkmsg.rdbuf()->freeze(0); \
511 } \
512 } while (false)
513#define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
515
517
529#define vtkVLogIfF(level, cond, ...) \
530 ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
531 ? (void)0 \
532 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
533
534#define vtkLogIfF(verbosity_name, cond, ...) \
535 vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
536
537#define vtkVLogIf(level, cond, x) \
538 do \
539 { \
540 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
541 { \
542 vtkOStrStreamWrapper::EndlType endl; \
543 vtkOStrStreamWrapper::UseEndl(endl); \
544 vtkOStrStreamWrapper vtkmsg; \
545 vtkmsg << "" x; \
546 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
547 vtkmsg.rdbuf()->freeze(0); \
548 } \
549 } while (false)
550#define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
552
553#define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
554#define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
555#define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
556
557#define vtkVLogScopeF(level, ...) \
558 auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
559 ? vtkLogger::LogScopeRAII() \
560 : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
561
562#define vtkLogScopeF(verbosity_name, ...) \
563 vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
564
565#define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
566#define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
567
569
573#define vtkLogStartScope(verbosity_name, id) \
574 vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
575#define vtkLogEndScope(id) vtkLogger::EndScope(id)
576
577#define vtkLogStartScopeF(verbosity_name, id, ...) \
578 vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
579
580#define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
581#define vtkVLogStartScopeF(level, id, ...) \
582 vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
584
590#define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
591
592VTK_ABI_NAMESPACE_END
593#endif
a simple class to control print indentation
Definition vtkIndent.h:108
LogScopeRAII(vtkLogger::Verbosity verbosity, const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(5
LogScopeRAII(LogScopeRAII &&)=default
logging framework for use in VTK and in applications based on VTK
Definition vtkLogger.h:206
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals,...
Definition vtkLogger.h:461
static bool EnableSigintHandler
Definition vtkLogger.h:466
static Verbosity ConvertToVerbosity(int value)
Convenience function to convert an integer to matching verbosity level.
static bool EnableSigtermHandler
Definition vtkLogger.h:468
static bool EnableSigsegvHandler
Definition vtkLogger.h:467
static bool EnableSigfpeHandler
Definition vtkLogger.h:464
void(*)(void *user_data) CloseHandlerCallbackT
Callback handle types.
Definition vtkLogger.h:361
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition vtkLogger.h:360
static bool EnableSigabrtHandler
Definition vtkLogger.h:462
static bool RemoveCallback(const char *id)
Remove a callback using the id specified.
static std::string GetIdentifier(vtkObjectBase *obj)
Returns a printable string for a vtkObjectBase instance.
static void EndScope(const char *id)
vtkBaseTypeMacro(vtkLogger, vtkObjectBase)
static std::string GetThreadName()
Get/Set the name to identify the current thread in the log output.
static void SetInternalVerbosityLevel(Verbosity level)
Set internal messages verbosity level.
void(*)(void *user_data) FlushHandlerCallbackT
Callback handle types.
Definition vtkLogger.h:362
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.
static bool IsEnabled()
Returns true if VTK is built with logging support enabled.
static void SetThreadName(const std::string &name)
Get/Set the name to identify the current thread in the log output.
static Verbosity ConvertToVerbosity(const char *text)
Convenience function to convert a string to matching verbosity level.
static bool EnableSigbusHandler
Definition vtkLogger.h:463
static void Init()
Initializes logging.
static void EndLogToFile(const char *path)
Stop logging to a file at the given path.
static void Init(int &argc, char *argv[], const char *verbosity_flag="-v")
Initializes logging.
static void Log(Verbosity verbosity, VTK_FILEPATH const char *fname, unsigned int lineno, const char *txt)
static void SetStderrVerbosity(Verbosity level)
Set the verbosity level for the output logged to stderr.
FileMode
Support log file modes: TRUNCATE truncates the file clearing any existing contents while APPEND appen...
Definition vtkLogger.h:308
static void LogF(Verbosity verbosity, VTK_FILEPATH const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(4
static Verbosity GetCurrentVerbosityCutoff()
Returns the maximum verbosity of all log outputs.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static bool EnableSigillHandler
Definition vtkLogger.h:465
static void StartScope(Verbosity verbosity, const char *id, VTK_FILEPATH const char *fname, unsigned int lineno)
~vtkLogger() override
static void LogToFile(const char *path, FileMode filemode, Verbosity verbosity)
Enable logging to a file at the given path.
abstract base class for most VTK objects
void operator=(const vtkObjectBase &)
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition vtkLogger.h:344
const char * filename
Definition vtkLogger.h:348
const char * preamble
Definition vtkLogger.h:350
Verbosity verbosity
Definition vtkLogger.h:347
const char * message
Definition vtkLogger.h:353
const char * indentation
Definition vtkLogger.h:351
const char * prefix
Definition vtkLogger.h:352
#define VTK_FORMAT_STRING_TYPE
Definition vtkLogger.h:201
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition vtkLogger.h:200
#define VTK_FILEPATH