ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CVLog.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "CVLog.h"
9 
10 // CV_CORE_lib
11 #include <CVPlatform.h>
12 
13 // System
14 #include <algorithm>
15 #include <cassert>
16 #include <vector>
17 
18 #if !defined(CV_WINDOWS)
19 #define _vsnprintf vsnprintf
20 #endif
21 
22 /***************
23  *** Globals ***
24  ***************/
25 
26 // buffer for formatted string generation
27 static const size_t s_bufferMaxSize = 4096;
29 
31 struct Message {
32  Message(const QString& t, int f) : text(t), flags(f) {}
33 
34  QString text;
35  int flags;
36 };
37 
38 // message backup system
39 static bool s_backupEnabled;
40 // backuped messages
41 static std::vector<Message> s_backupMessages;
42 
43 // message verbosity level
44 #ifdef QT_DEBUG
46 #else
48 #endif
49 
50 // unique console instance
51 static CVLog* s_instance = nullptr;
52 
54 
55 void CVLog::EnableMessageBackup(bool state) { s_backupEnabled = state; }
56 
58 
59 void CVLog::SetVerbosityLevel(int level) {
61  level, static_cast<int>(LOG_ERROR)); // can't ignore error messages
62 }
63 
64 void CVLog::LogMessage(const QString& message, int level) {
65  // skip messages below the current 'verbosity' level
66  if ((level & 7) < s_verbosityLevel) {
67  return;
68  }
69 
70  if (s_instance) {
71  s_instance->logMessage(message, level);
72  } else if (s_backupEnabled) {
73  try {
74  s_backupMessages.emplace_back(message, level);
75  } catch (const std::bad_alloc&) {
76  // nothing to do, the message will be lost...
77  }
78  }
79 }
80 
81 void CVLog::RegisterInstance(CVLog* logInstance) {
82  s_instance = logInstance;
83  if (s_instance) {
84  // if we have a valid instance, we can now flush the backuped messages
85  for (const Message& message : s_backupMessages) {
86  s_instance->logMessage(message.text, message.flags);
87  }
88  s_backupMessages.clear();
89  }
90 }
91 
92 // Conversion from '...' parameters to QString so as to call CVLog::logMessage
93 //(we get the "..." parameters as "printf" would do)
94 #define LOG_ARGS(flags) \
95  if (s_instance || s_backupEnabled) { \
96  va_list args; \
97  va_start(args, format); \
98  _vsnprintf(s_buffer, s_bufferMaxSize, format, args); \
99  va_end(args); \
100  LogMessage(QString(s_buffer), flags); \
101  }
102 
103 bool CVLog::PrintVerbose(const char* format, ...) {
105  return true;
106 }
107 
108 bool CVLog::PrintVerbose(const QString& message) {
109  LogMessage(message, LOG_VERBOSE);
110  return true;
111 }
112 
113 bool CVLog::Print(const char* format, ...) {
115  return true;
116 }
117 
118 bool CVLog::Print(const QString& message) {
119  LogMessage(message, LOG_STANDARD);
120  return true;
121 }
122 
123 bool CVLog::PrintHigh(const char* format, ...) {
125  return true;
126 }
127 
128 bool CVLog::PrintHigh(const QString& message) {
129  LogMessage(message, LOG_IMPORTANT);
130  return true;
131 }
132 
133 bool CVLog::Warning(const char* format, ...) {
135  return false;
136 }
137 
138 bool CVLog::Warning(const QString& message) {
139  LogMessage(message, LOG_WARNING);
140  return false;
141 }
142 
143 bool CVLog::Error(const char* format, ...) {
145  return false;
146 }
147 
148 bool CVLog::Error(const QString& message) {
149  LogMessage(message, LOG_ERROR);
150  return false;
151 }
152 
153 bool CVLog::PrintDebug(const char* format, ...) {
154 #ifdef QT_DEBUG
156 #endif
157  return true;
158 }
159 
160 bool CVLog::PrintDebug(const QString& message) {
161 #ifdef QT_DEBUG
162  LogMessage(message, LOG_STANDARD | DEBUG_FLAG);
163 #endif
164  return false;
165 }
166 
167 bool CVLog::WarningDebug(const char* format, ...) {
168 #ifdef QT_DEBUG
170 #endif
171  return false;
172 }
173 
174 bool CVLog::WarningDebug(const QString& message) {
175 #ifdef QT_DEBUG
176  LogMessage(message, LOG_WARNING | DEBUG_FLAG);
177 #endif
178  return false;
179 }
180 
181 bool CVLog::ErrorDebug(const char* format, ...) {
182 #ifdef QT_DEBUG
184 #endif
185  return false;
186 }
187 
188 bool CVLog::ErrorDebug(const QString& message) {
189 #ifdef QT_DEBUG
190  LogMessage(message, LOG_ERROR | DEBUG_FLAG);
191 #endif
192  return false;
193 }
static bool s_backupEnabled
Definition: CVLog.cpp:39
static char s_buffer[s_bufferMaxSize]
Definition: CVLog.cpp:28
static const size_t s_bufferMaxSize
Definition: CVLog.cpp:27
static CVLog * s_instance
Definition: CVLog.cpp:51
#define LOG_ARGS(flags)
Definition: CVLog.cpp:94
static int s_verbosityLevel
Definition: CVLog.cpp:47
static std::vector< Message > s_backupMessages
Definition: CVLog.cpp:41
filament::Texture::InternalFormat format
Main log interface.
Definition: CVLog.h:25
static bool PrintHigh(const char *format,...)
Prints out an important formatted message in console.
Definition: CVLog.cpp:123
static bool WarningDebug(const char *format,...)
Same as Warning, but works only in Debug mode.
Definition: CVLog.cpp:167
static bool PrintDebug(const char *format,...)
Same as Print, but works only in Debug mode.
Definition: CVLog.cpp:153
static bool ErrorDebug(const char *format,...)
Same as Error, but works only in Debug mode.
Definition: CVLog.cpp:181
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
static bool Print(const char *format,...)
Prints out a formatted message in console.
Definition: CVLog.cpp:113
virtual void logMessage(const QString &message, int level)=0
Generic message logging method.
static void SetVerbosityLevel(int level)
Sets the verbosity level.
Definition: CVLog.cpp:59
static void EnableMessageBackup(bool state)
Enables the message backup system.
Definition: CVLog.cpp:55
static bool PrintVerbose(const char *format,...)
Prints out a verbose formatted message in console.
Definition: CVLog.cpp:103
static void LogMessage(const QString &message, int level)
Static shortcut to CVLog::logMessage.
Definition: CVLog.cpp:64
@ LOG_IMPORTANT
Definition: CVLog.h:45
@ LOG_STANDARD
Definition: CVLog.h:44
@ DEBUG_FLAG
Definition: CVLog.h:49
@ LOG_VERBOSE
Definition: CVLog.h:43
@ LOG_WARNING
Definition: CVLog.h:46
@ LOG_ERROR
Definition: CVLog.h:47
static int VerbosityLevel()
Returns the current verbosity level.
Definition: CVLog.cpp:57
static CVLog * TheInstance()
Returns the static and unique instance.
Definition: CVLog.cpp:53
static void RegisterInstance(CVLog *logInstance)
Registers a unique instance.
Definition: CVLog.cpp:81
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
int min(int a, int b)
Definition: cutil_math.h:53
Message.
Definition: CVLog.cpp:31
Message(const QString &t, int f)
Definition: CVLog.cpp:32
int flags
Definition: CVLog.cpp:35
QString text
Definition: CVLog.cpp:34