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