VTK  9.1.0
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=========================================================================*/
169#ifndef vtkLogger_h
170#define vtkLogger_h
171
172#include "vtkObjectBase.h"
173#include "vtkSetGet.h" // needed for macros
174
175#include <string> // needed for std::string
176
177#if defined(_MSC_VER)
178#include <sal.h> // Needed for _In_z_ etc annotations
179#endif
180
181// this is copied from `loguru.hpp`
182#if defined(__clang__) || defined(__GNUC__)
183// Helper macro for declaring functions as having similar signature to printf.
184// This allows the compiler to catch format errors at compile-time.
185#define VTK_PRINTF_LIKE(fmtarg, firstvararg) \
186 __attribute__((__format__(__printf__, fmtarg, firstvararg)))
187#define VTK_FORMAT_STRING_TYPE const char*
188#elif defined(_MSC_VER)
189#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
190#define VTK_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char*
191#else
192#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
193#define VTK_FORMAT_STRING_TYPE const char*
194#endif
195
196class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
197{
198public:
200 void PrintSelf(ostream& os, vtkIndent indent) override;
201
203 {
204 // Used to mark an invalid verbosity. Do not log to this level.
205 VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
206
207 // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
208 VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
209
210 VERBOSITY_ERROR = -2,
211 VERBOSITY_WARNING = -1,
212
213 // Normal messages. By default written to stderr.
214 VERBOSITY_INFO = 0,
215
216 // Same as VERBOSITY_INFO in every way.
217 VERBOSITY_0 = 0,
218
219 // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
220 VERBOSITY_1 = +1,
221 VERBOSITY_2 = +2,
222 VERBOSITY_3 = +3,
223 VERBOSITY_4 = +4,
224 VERBOSITY_5 = +5,
225 VERBOSITY_6 = +6,
226 VERBOSITY_7 = +7,
227 VERBOSITY_8 = +8,
228 VERBOSITY_9 = +9,
229
230 // trace level, same as VERBOSITY_9
231 VERBOSITY_TRACE = +9,
232
233 // Don not use higher verbosity levels, as that will make grepping log files harder.
234 VERBOSITY_MAX = +9,
235 };
236
274 static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
275 static void Init();
285
293
299 {
301 APPEND
302 };
303
310 static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
311
315 static void EndLogToFile(const char* path);
316
318
321 static void SetThreadName(const std::string& name);
324
329
334 struct Message
335 {
336 // You would generally print a Message by just concatenating the buffers without spacing.
337 // Optionally, ignore preamble and indentation.
338 Verbosity verbosity; // Already part of preamble
339 const char* filename; // Already part of preamble
340 unsigned line; // Already part of preamble
341 const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
342 const char* indentation; // Just a bunch of spacing.
343 const char* prefix; // Assertion failure info goes here (or "").
344 const char* message; // User message goes here.
345 };
346
348
351 using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
352 using CloseHandlerCallbackT = void (*)(void* user_data);
353 using FlushHandlerCallbackT = void (*)(void* user_data);
355
365 static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
366 Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
367 FlushHandlerCallbackT on_flush = nullptr);
368
373 static bool RemoveCallback(const char* id);
374
378 static bool IsEnabled();
379
386
394
401 static Verbosity ConvertToVerbosity(const char* text);
402
404
409 static void Log(
410 Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno, const char* txt);
411 static void StartScope(
412 Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname, unsigned int lineno);
413 static void EndScope(const char* id);
414#if !defined(__WRAP__)
415 static void LogF(Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno,
416 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
417 static void StartScopeF(Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname,
418 unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
419
420 class VTKCOMMONCORE_EXPORT LogScopeRAII
421 {
422 public:
424 LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
425 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
427#if defined(_MSC_VER) && _MSC_VER > 1800
428 // see loguru.hpp for the reason why this is needed on MSVC
430 : Internals(other.Internals)
431 {
432 other.Internals = nullptr;
433 }
434#else
436#endif
437
438 private:
439 LogScopeRAII(const LogScopeRAII&) = delete;
440 void operator=(const LogScopeRAII&) = delete;
441 class LSInternals;
442 LSInternals* Internals;
443 };
444#endif
446
453
454protected:
456 ~vtkLogger() override;
457
458private:
459 vtkLogger(const vtkLogger&) = delete;
460 void operator=(const vtkLogger&) = delete;
461 static vtkLogger::Verbosity InternalVerbosityLevel;
462 static std::string ThreadName;
463};
464
466
480#define vtkVLogF(level, ...) \
481 ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
482 ? (void)0 \
483 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
484#define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
485#define vtkVLog(level, x) \
486 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
487 { \
488 vtkOStrStreamWrapper::EndlType endl; \
489 vtkOStrStreamWrapper::UseEndl(endl); \
490 vtkOStrStreamWrapper vtkmsg; \
491 vtkmsg << "" x; \
492 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
493 vtkmsg.rdbuf()->freeze(0); \
494 }
495#define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
497
499
511#define vtkVLogIfF(level, cond, ...) \
512 ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
513 ? (void)0 \
514 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
515
516#define vtkLogIfF(verbosity_name, cond, ...) \
517 vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
518
519#define vtkVLogIf(level, cond, x) \
520 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
521 { \
522 vtkOStrStreamWrapper::EndlType endl; \
523 vtkOStrStreamWrapper::UseEndl(endl); \
524 vtkOStrStreamWrapper vtkmsg; \
525 vtkmsg << "" x; \
526 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
527 vtkmsg.rdbuf()->freeze(0); \
528 }
529#define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
531
532#define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
533#define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
534#define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
535
536#define vtkVLogScopeF(level, ...) \
537 auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
538 ? vtkLogger::LogScopeRAII() \
539 : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
540
541#define vtkLogScopeF(verbosity_name, ...) \
542 vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
543
544#define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
545#define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
546
548
552#define vtkLogStartScope(verbosity_name, id) \
553 vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
554#define vtkLogEndScope(id) vtkLogger::EndScope(id)
555
556#define vtkLogStartScopeF(verbosity_name, id, ...) \
557 vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
558
559#define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
560#define vtkVLogStartScopeF(level, id, ...) \
561 vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
563
569#define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
570
571#endif
a simple class to control print indentation
Definition: vtkIndent.h:113
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:197
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals,...
Definition: vtkLogger.h:452
static Verbosity ConvertToVerbosity(int value)
Convenience function to convert an integer to matching verbosity level.
void(*)(void *user_data) CloseHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:352
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:351
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:353
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 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:299
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.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:70
void operator=(const vtkObjectBase &)
@ level
Definition: vtkX3D.h:401
@ value
Definition: vtkX3D.h:226
@ name
Definition: vtkX3D.h:225
@ string
Definition: vtkX3D.h:496
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition: vtkLogger.h:335
const char * filename
Definition: vtkLogger.h:339
const char * preamble
Definition: vtkLogger.h:341
Verbosity verbosity
Definition: vtkLogger.h:338
const char * message
Definition: vtkLogger.h:344
const char * indentation
Definition: vtkLogger.h:342
const char * prefix
Definition: vtkLogger.h:343
#define VTK_FORMAT_STRING_TYPE
Definition: vtkLogger.h:193
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition: vtkLogger.h:192
#define VTK_FILEPATH