VTK  9.2.6
vtkLogger.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkLogger.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
149
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
177class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
178{
179public:
181 void PrintSelf(ostream& os, vtkIndent indent) override;
182
184 {
185 // Used to mark an invalid verbosity. Do not log to this level.
186 VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
187
188 // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
189 VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
190
193
194 // Normal messages. By default written to stderr.
196
197 // Same as VERBOSITY_INFO in every way.
199
200 // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
210
211 // trace level, same as VERBOSITY_9
213
214 // Don not use higher verbosity levels, as that will make grepping log files harder.
216 };
217
255 static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
256 static void Init();
258
265 static void SetStderrVerbosity(Verbosity level);
266
274
280 {
283 };
284
291 static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
292
296 static void EndLogToFile(const char* path);
297
299
302 static void SetThreadName(const std::string& name);
303 static std::string GetThreadName();
305
309 static std::string GetIdentifier(vtkObjectBase* obj);
310
315 struct Message
316 {
317 // You would generally print a Message by just concatenating the buffers without spacing.
318 // Optionally, ignore preamble and indentation.
319 Verbosity verbosity; // Already part of preamble
320 const char* filename; // Already part of preamble
321 unsigned line; // Already part of preamble
322 const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
323 const char* indentation; // Just a bunch of spacing.
324 const char* prefix; // Assertion failure info goes here (or "").
325 const char* message; // User message goes here.
326 };
327
329
332 using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
333 using CloseHandlerCallbackT = void (*)(void* user_data);
334 using FlushHandlerCallbackT = void (*)(void* user_data);
336
346 static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
347 Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
348 FlushHandlerCallbackT on_flush = nullptr);
349
354 static bool RemoveCallback(const char* id);
355
359 static bool IsEnabled();
360
367
374 static Verbosity ConvertToVerbosity(int value);
375
382 static Verbosity ConvertToVerbosity(const char* text);
383
385
390 static void Log(
391 Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno, const char* txt);
392 static void StartScope(
393 Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname, unsigned int lineno);
394 static void EndScope(const char* id);
395#if !defined(__WRAP__)
396 static void LogF(Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno,
397 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
398 static void StartScopeF(Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname,
399 unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
400
401 class VTKCOMMONCORE_EXPORT LogScopeRAII
402 {
403 public:
405 LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
406 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
408#if defined(_MSC_VER) && _MSC_VER > 1800
409 // see loguru.hpp for the reason why this is needed on MSVC
411 : Internals(other.Internals)
412 {
413 other.Internals = nullptr;
414 }
415#else
417#endif
418
419 private:
420 LogScopeRAII(const LogScopeRAII&) = delete;
421 void operator=(const LogScopeRAII&) = delete;
422 class LSInternals;
423 LSInternals* Internals;
424 };
425#endif
427
434
435protected:
437 ~vtkLogger() override;
438
439private:
440 vtkLogger(const vtkLogger&) = delete;
441 void operator=(const vtkLogger&) = delete;
442 static vtkLogger::Verbosity InternalVerbosityLevel;
443};
444
446
460#define vtkVLogF(level, ...) \
461 ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
462 ? (void)0 \
463 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
464#define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
465#define vtkVLog(level, x) \
466 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
467 { \
468 vtkOStrStreamWrapper::EndlType endl; \
469 vtkOStrStreamWrapper::UseEndl(endl); \
470 vtkOStrStreamWrapper vtkmsg; \
471 vtkmsg << "" x; \
472 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
473 vtkmsg.rdbuf()->freeze(0); \
474 }
475#define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
477
479
491#define vtkVLogIfF(level, cond, ...) \
492 ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
493 ? (void)0 \
494 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
495
496#define vtkLogIfF(verbosity_name, cond, ...) \
497 vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
498
499#define vtkVLogIf(level, cond, x) \
500 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
501 { \
502 vtkOStrStreamWrapper::EndlType endl; \
503 vtkOStrStreamWrapper::UseEndl(endl); \
504 vtkOStrStreamWrapper vtkmsg; \
505 vtkmsg << "" x; \
506 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
507 vtkmsg.rdbuf()->freeze(0); \
508 }
509#define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
511
512#define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
513#define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
514#define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
515
516#define vtkVLogScopeF(level, ...) \
517 auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
518 ? vtkLogger::LogScopeRAII() \
519 : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
520
521#define vtkLogScopeF(verbosity_name, ...) \
522 vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
523
524#define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
525#define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
526
528
532#define vtkLogStartScope(verbosity_name, id) \
533 vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
534#define vtkLogEndScope(id) vtkLogger::EndScope(id)
535
536#define vtkLogStartScopeF(verbosity_name, id, ...) \
537 vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
538
539#define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
540#define vtkVLogStartScopeF(level, id, ...) \
541 vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
542
543
549#define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
550
551#endif
a simple class to control print indentation
Definition vtkIndent.h:34
LogScopeRAII(vtkLogger::Verbosity verbosity, const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(5
LogScopeRAII(LogScopeRAII &&)=default
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals,...
Definition vtkLogger.h:433
static Verbosity ConvertToVerbosity(int value)
Convenience function to convert an integer to matching verbosity level.
static void static void StartScopeF(Verbosity verbosity, const char *id, VTK_FILEPATH const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(5
void(*)(void *user_data) CloseHandlerCallbackT
Callback handle types.
Definition vtkLogger.h:333
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition vtkLogger.h:332
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:334
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.
@ VERBOSITY_OFF
Definition vtkLogger.h:189
@ VERBOSITY_WARNING
Definition vtkLogger.h:192
@ VERBOSITY_INVALID
Definition vtkLogger.h:186
@ VERBOSITY_MAX
Definition vtkLogger.h:215
@ VERBOSITY_TRACE
Definition vtkLogger.h:212
@ VERBOSITY_INFO
Definition vtkLogger.h:195
@ VERBOSITY_ERROR
Definition vtkLogger.h:191
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 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:280
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 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.
void operator=(const vtkObjectBase &)
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition vtkLogger.h:316
const char * filename
Definition vtkLogger.h:320
const char * preamble
Definition vtkLogger.h:322
Verbosity verbosity
Definition vtkLogger.h:319
const char * message
Definition vtkLogger.h:325
const char * indentation
Definition vtkLogger.h:323
const char * prefix
Definition vtkLogger.h:324
#define VTK_FORMAT_STRING_TYPE
Definition vtkLogger.h:174
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition vtkLogger.h:173
#define VTK_FILEPATH