OpenLexocad  27.0
Log.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Core/core_defines2.h>
4 
5 #include <Geom/Ax2.h>
6 #include <Geom/Dir.h>
7 #include <Geom/Pnt.h>
8 #include <Geom/Vec.h>
9 #include <Geom/Vec2d.h>
10 
11 #include <QTextStream>
12 #include <sstream>
13 #include <string>
14 
15 #define LOGGING_ENABLED
16 
17 namespace Core
18 {
19  enum LOGLEVEL
20  {
21  D_OFF = 0,
22  D_FATAL = 1,
23  D_ERROR = 2,
24  D_WARN = 3,
25  D_INFO = 4,
26  D_DEBUG = 5,
27  D_ALL = 6
28  };
29 
32 
34  {
35  public:
36  static LastError& Instance();
37  LastError& setError(std::string& where, std::string& msg);
38  LastError& setError(const char* where, const char* msg, ...);
39  LastError& append(std::string& msg);
40  std::string getError();
41  std::string getErrorMsg();
42  LastError();
43  ~LastError();
44 
45  private:
46  std::string m_where;
47  std::string m_msg;
48  int m_msgID = -1;
49  };
50 
51  #define LASTERROR(msg, ...) Core::LastError::Instance().setError(__FUNCTION__, msg, __VA_ARGS__)
52  #define CLEARLASTERROR() Core::LastError::Instance().setError("", "")
53 
54  class LogWriter
55  {
56  public:
57  virtual void write(const QString& s) = 0;
58  virtual void write(QString fontName, int fontsize, Geom::Vec2d position, Geom::Vec color, const QString& msg) = 0;
59  };
60 
62  {
63  public:
64  ScopedLogger(LOGLEVEL level, const std::string& s);
65  ~ScopedLogger();
66 
67  static int getIndent();
68 
69  class ScopedLoggerP;
70  ScopedLoggerP* _piml;
71  };
72 
73 
75  {
76  public:
77  virtual LogBaseClass& space() = 0;
78  virtual LogBaseClass& nospace() = 0;
79  virtual LogBaseClass& maybeSpace() = 0;
80  virtual LogBaseClass& operator<<(const Geom::Pnt2d& p) = 0;
81  virtual LogBaseClass& operator<<(const Geom::Pnt& p) = 0;
82  virtual LogBaseClass& operator<<(const Geom::Vec& p) = 0;
83  virtual LogBaseClass& operator<<(const Geom::Dir& p) = 0;
84  virtual LogBaseClass& operator<<(const Geom::Ax2& p) = 0;
85  virtual LogBaseClass& operator<<(QChar t) = 0;
86  virtual LogBaseClass& operator<<(bool t) = 0;
87  virtual LogBaseClass& operator<<(char t) = 0;
88  virtual LogBaseClass& operator<<(signed short t) = 0;
89  virtual LogBaseClass& operator<<(unsigned short t) = 0;
90  virtual LogBaseClass& operator<<(signed int t) = 0;
91  virtual LogBaseClass& operator<<(unsigned int t) = 0;
92  virtual LogBaseClass& operator<<(signed long t) = 0;
93  virtual LogBaseClass& operator<<(unsigned long t) = 0;
94  virtual LogBaseClass& operator<<(qint64 t) = 0;
95  virtual LogBaseClass& operator<<(quint64 t) = 0;
96  virtual LogBaseClass& operator<<(float t) = 0;
97  virtual LogBaseClass& operator<<(double t) = 0;
98  virtual LogBaseClass& operator<<(const char* t) = 0;
99  virtual LogBaseClass& operator<<(const QString& t) = 0;
100  virtual LogBaseClass& operator<<(const QStringRef& t) = 0;
101  virtual LogBaseClass& operator<<(const QLatin1String& t) = 0;
102  virtual LogBaseClass& operator<<(const QByteArray& t) = 0;
103  virtual LogBaseClass& operator<<(const void* t) = 0;
104  virtual LogBaseClass& operator<<(QTextStreamFunction f) = 0;
105  virtual LogBaseClass& operator<<(QTextStreamManipulator m) = 0;
106 
107 
108  };
109 
111  {
112  public:
113  inline NoDebugClass& operator<<(QTextStreamFunction) { return *this; }
114  inline NoDebugClass& operator<<(QTextStreamManipulator) { return *this; }
115  inline NoDebugClass& space() { return *this; }
116  inline NoDebugClass& nospace() { return *this; }
117  inline NoDebugClass& maybeSpace() { return *this; }
118  inline NoDebugClass& quote() { return *this; }
119  inline NoDebugClass& noquote() { return *this; }
120  inline NoDebugClass& maybeQuote(const char = '"') { return *this; }
121 
122  template<typename T>
123  inline NoDebugClass& operator<<(const T&) { return *this; }
124  };
125 
126 
127  class LogClass : public LogBaseClass
128  {
129  struct Stream
130  {
131  Stream(QIODevice* device) : ts(device), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
132  Stream(QString* string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false) {}
133  Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true) {}
134  QTextStream ts;
135  QString buffer;
136  int ref;
137  QtMsgType type;
138  bool space;
139  bool message_output;
140  } * stream;
141 
142  LOGLEVEL mylevel;
143  #if QT_VERSION >= 0x050000
144  QMessageLogContext context;
145  #endif
146 
147 
148  public:
149  CORE_EXPORT static void addLogWriter(LogWriter* writer);
150  CORE_EXPORT static void removeLogWriter(LogWriter* writer);
151  static std::vector<LogWriter*> logwriter;
152 
153  inline LogClass(QIODevice* device) : stream(new Stream(device)) {}
154  inline LogClass(QString* string) : stream(new Stream(string)) {}
155  inline LogClass(LOGLEVEL level, QtMsgType t) : mylevel(level), stream(new Stream(t)) {}
156  inline LogClass(const LogClass& o) : stream(o.stream) { ++stream->ref; }
157  inline LogClass& operator=(const QDebug& other);
158  inline ~LogClass()
159  {
160  if (!--stream->ref)
161  {
162  if (stream->message_output && !stream->buffer.isEmpty())
163  {
164  if (mylevel <= getLogLevel())
165  {
166  char buf2[6000];
167  if (mylevel == D_ALL)
168  sprintf_s(buf2, 6000, "ALL: %*s", ScopedLogger::getIndent() * 2, "");
169  if (mylevel == D_DEBUG)
170  sprintf_s(buf2, 6000, "DEBUG: %*s", ScopedLogger::getIndent() * 2, "");
171  if (mylevel == D_ERROR)
172  sprintf_s(buf2, 6000, "ERROR: %*s", ScopedLogger::getIndent() * 2, "");
173  if (mylevel == D_FATAL)
174  sprintf_s(buf2, 6000, "FATAL: %*s", ScopedLogger::getIndent() * 2, "");
175  if (mylevel == D_WARN)
176  sprintf_s(buf2, 6000, "%*s", ScopedLogger::getIndent() * 2, "");
177  if (mylevel == D_INFO)
178  sprintf_s(buf2, 6000, "INFO: %*s", ScopedLogger::getIndent() * 2, "");
179  if (mylevel == D_OFF)
180  sprintf_s(buf2, 6000, "OFF: %*s", ScopedLogger::getIndent() * 2, "");
181 
182  QString msg = QString(buf2) + QString(stream->buffer);
183  QMessageLogContext context;
184  qt_message_output(stream->type, context, msg);
185  }
186  }
187  delete stream;
188  }
189  }
190  inline LogClass& space()
191  {
192  stream->space = true;
193  stream->ts << " ";
194  return *this;
195  }
196  inline LogClass& nospace()
197  {
198  stream->space = false;
199  return *this;
200  }
202  {
203  if (stream->space)
204  stream->ts << " ";
205  return *this;
206  }
207 
208  inline LogClass& operator<<(const Geom::Pnt2d& p)
209  {
210  stream->ts << qSetRealNumberPrecision(12) << p.x() << " " << p.y();
211  return maybeSpace();
212  }
213  inline LogClass& operator<<(const Geom::Pnt& p)
214  {
215  stream->ts << qSetRealNumberPrecision(12) << p.x() << " " << p.y() << " " << p.z();
216  return maybeSpace();
217  }
218  inline LogClass& operator<<(const Geom::Vec& p)
219  {
220  stream->ts << qSetRealNumberPrecision(12) << p.x() << " " << p.y() << " " << p.z();
221  return maybeSpace();
222  }
223  inline LogClass& operator<<(const Geom::Dir& p)
224  {
225  stream->ts << qSetRealNumberPrecision(12) << p.x() << " " << p.y() << " " << p.z();
226  return maybeSpace();
227  }
228  inline LogClass& operator<<(const Geom::Ax2& p)
229  {
230  //stream->ts << "AX2 DIR: " << p.direction().xyz() << " LOCATION: " << p.location().xyz();
231  return maybeSpace();
232  }
233 
234 
235  inline LogClass& operator<<(QChar t)
236  {
237  stream->ts << "\'" << t << "\'";
238  return maybeSpace();
239  }
240  inline LogClass& operator<<(bool t)
241  {
242  stream->ts << (bool(t) ? "true" : "false");
243  return maybeSpace();
244  }
245  inline LogClass& operator<<(char t)
246  {
247  stream->ts << t;
248  return maybeSpace();
249  }
250  inline LogClass& operator<<(signed short t)
251  {
252  stream->ts << t;
253  return maybeSpace();
254  }
255  inline LogClass& operator<<(unsigned short t)
256  {
257  stream->ts << t;
258  return maybeSpace();
259  }
260  inline LogClass& operator<<(signed int t)
261  {
262  stream->ts << t;
263  return maybeSpace();
264  }
265  inline LogClass& operator<<(unsigned int t)
266  {
267  stream->ts << t;
268  return maybeSpace();
269  }
270  inline LogClass& operator<<(signed long t)
271  {
272  stream->ts << t;
273  return maybeSpace();
274  }
275  inline LogClass& operator<<(unsigned long t)
276  {
277  stream->ts << t;
278  return maybeSpace();
279  }
280  inline LogClass& operator<<(qint64 t)
281  {
282  stream->ts << QString::number(t);
283  return maybeSpace();
284  }
285  inline LogClass& operator<<(quint64 t)
286  {
287  stream->ts << QString::number(t);
288  return maybeSpace();
289  }
290  inline LogClass& operator<<(float t)
291  {
292  stream->ts << t;
293  return maybeSpace();
294  }
295  inline LogClass& operator<<(double t)
296  {
297  stream->ts << t;
298  return maybeSpace();
299  }
300  inline LogClass& operator<<(const char* t)
301  {
302  stream->ts << qPrintable(t);
303  return maybeSpace();
304  }
305  inline LogClass& operator<<(const QString& t)
306  {
307  stream->ts << t;
308  return maybeSpace();
309  }
310  inline LogClass& operator<<(const QStringRef& t) { return operator<<(t.toString()); }
311  inline LogClass& operator<<(const QLatin1String& t)
312  {
313  stream->ts << t.latin1();
314  return maybeSpace();
315  }
316  inline LogClass& operator<<(const QByteArray& t)
317  {
318  stream->ts << t;
319  return maybeSpace();
320  }
321  inline LogClass& operator<<(const void* t)
322  {
323  stream->ts << t;
324  return maybeSpace();
325  }
326  inline LogClass& operator<<(QTextStreamFunction f)
327  {
328  stream->ts << f;
329  return *this;
330  }
331 
332  inline LogClass& operator<<(QTextStreamManipulator m)
333  {
334  stream->ts << m;
335  return *this;
336  }
337 
338  static bool is_activated;
339  };
340 
341 #ifdef LOGGING_ENABLED
342  void LogV(LOGLEVEL level, const QString& s, va_list ap);
344  CORE_EXPORT LogClass Log(Core::LOGLEVEL level, const char* msg, ...);
345 #endif
346 
347  CORE_EXPORT void MessageAtScreen(QString fontName, int fontsize, Geom::Vec2d position, Geom::Vec color, const char* msg, ...);
348  CORE_EXPORT void MessageAtScreen(const char* msg, ...);
349 
350 
351  #define LOGCOMMAND_DEBUG(x) Core::ScopedLogger _____scopedLogger(Core::D_DEBUG, x);
352  #define LOGCOMMAND_INFO(x) Core::ScopedLogger _____scopedLogger(Core::D_INFO, x);
353  #define LOGCOMMAND(x) Core::ScopedLogger _____scopedLogger(Core::D_WARN, x);
354  #define LOGCOMMAND_WARN(x) Core::ScopedLogger _____scopedLogger(Core::D_WARN, x);
355  #define cUserDebug(...) \
356  if (Core::Settings::getInstance()->getDebugUser()) \
357  { \
358  cWarn( __VA_ARGS__); \
359  }
360  #define LOGVAR(var) cDebug() << QString("%1 = %2").arg(#var).arg(var);
361 
363  {
364  public:
366  {
367  static AssertSingleton instance;
368  // volatile int dummy{};
369  return instance;
370  }
371 
372  void setCallBack( std::function<bool(std::string)> f ) { mCallback = f; }
373  bool callCallBack(std::string s) { if(mCallback) return mCallback(s); return false; }
374 
375  private:
376  std::function<bool(std::string)> mCallback;
377  AssertSingleton()= default;
378  ~AssertSingleton()= default;
379  AssertSingleton(const AssertSingleton&)= delete;
380  AssertSingleton& operator=(const AssertSingleton&)= delete;
381 
382  };
383 
384 
385 
386 
387 } // namespace Core
388 
389 #ifdef LOGGING_ENABLED
390 
391 CORE_EXPORT void cDebuggerBreak(char* message);
396 
397 CORE_EXPORT Core::LogClass cDebug(const QString s);
398 CORE_EXPORT Core::LogClass cInfo(const QString s);
399 CORE_EXPORT Core::LogClass cError(const QString s);
400 CORE_EXPORT Core::LogClass cWarn(const QString s);
401 
402 CORE_EXPORT Core::LogClass cDebug(const char* msg, ...);
403 CORE_EXPORT Core::LogClass cInfo(const char* msg, ...);
404 CORE_EXPORT Core::LogClass cError(const char* msg, ...);
405 CORE_EXPORT Core::LogClass cWarn(const char* msg, ...);
406 
407 #else
408 
410 CORE_EXPORT void noDebug(const char*, ...);
411 CORE_EXPORT void noDebug(const QString &);
412 
413 #define cDebug while(false) noDebug
414 #define cInfo while(false) noDebug
415 #define cError while(false) noDebug
416 #define cWarn while(false) noDebug
417 
418 
419 #endif
LogClass & operator<<(const Geom::Pnt2d &p)
Definition: Log.h:208
CORE_EXPORT Core::LogClass cWarn()
double z() const
For this point, returns its X coordinate.
NoDebugClass & maybeQuote(const char='"')
Definition: Log.h:120
LogClass & operator<<(const QLatin1String &t)
Definition: Log.h:311
void LogV(LOGLEVEL level, const QString &s, va_list ap)
#define CORE_EXPORT
Definition: core_defines2.h:10
Definition: Log.h:33
double x() const
Returns the X coordinate for a unit vector.
double y() const
Returns the Y coordinate for a unit vector.
LogClass & operator<<(QTextStreamManipulator m)
Definition: Log.h:332
virtual LogBaseClass & space()=0
double z() const
Returns the Z coordinate for a unit vector.
Definition: Log.h:362
CORE_EXPORT void cDebuggerBreak(char *message)
LogClass(const LogClass &o)
Definition: Log.h:156
Definition: Log.h:22
Defines a non-persistent 3D Cartesian point.
Definition: Pnt.h:42
LogClass & operator<<(char t)
Definition: Log.h:245
LogClass & operator<<(const Geom::Vec &p)
Definition: Log.h:218
NoDebugClass & nospace()
Definition: Log.h:116
NoDebugClass & noquote()
Definition: Log.h:119
CORE_EXPORT Core::LogClass cDebug()
double y() const
For this point, returns its X coordinate.
double x() const
For this vector, returns its X coordinate.
LogClass & space()
Definition: Log.h:190
static CORE_EXPORT void removeLogWriter(LogWriter *writer)
LogClass & operator<<(const void *t)
Definition: Log.h:321
CORE_EXPORT void setLogLevel(LOGLEVEL)
LogClass(LOGLEVEL level, QtMsgType t)
Definition: Log.h:155
LogClass & operator<<(const Geom::Pnt &p)
Definition: Log.h:213
LogClass & operator<<(const Geom::Ax2 &p)
Definition: Log.h:228
LogClass & operator<<(unsigned long t)
Definition: Log.h:275
static int getIndent()
static bool is_activated
Definition: Log.h:338
Definition: Log.h:110
LogClass & operator<<(unsigned int t)
Definition: Log.h:265
virtual void write(const QString &s)=0
static CORE_EXPORT void addLogWriter(LogWriter *writer)
CORE_EXPORT void MessageAtScreen(QString fontName, int fontsize, Geom::Vec2d position, Geom::Vec color, const char *msg,...)
double y() const
For this point, returns its y coordinate.
double x() const
For this point, returns its X coordinate.
LogClass & operator<<(QTextStreamFunction f)
Definition: Log.h:326
void setCallBack(std::function< bool(std::string)> f)
Definition: Log.h:372
Definition: Ax2.h:69
LogClass & operator<<(unsigned short t)
Definition: Log.h:255
virtual LogBaseClass & maybeSpace()=0
double z() const
For this vector, returns its Z coordinate.
LogClass & operator<<(quint64 t)
Definition: Log.h:285
LogClass & operator<<(bool t)
Definition: Log.h:240
LogClass & operator<<(float t)
Definition: Log.h:290
Definition: Dir.h:46
LogClass & operator<<(const QStringRef &t)
Definition: Log.h:310
LogClass & operator<<(signed short t)
Definition: Log.h:250
ScopedLoggerP * _piml
Definition: Log.h:69
LogClass & operator<<(const QByteArray &t)
Definition: Log.h:316
Definition: Base.h:19
LogClass & operator<<(double t)
Definition: Log.h:295
virtual LogBaseClass & operator<<(const Geom::Pnt2d &p)=0
LogClass & operator<<(const QString &t)
Definition: Log.h:305
Defines a non-persistent vector in 2D space.
Definition: Vec2d.h:34
static AssertSingleton & getInstance()
Definition: Log.h:365
Definition: Log.h:27
Definition: Log.h:61
LogClass & operator<<(QChar t)
Definition: Log.h:235
Definition: Log.h:74
bool callCallBack(std::string s)
Definition: Log.h:373
LogClass & operator<<(const Geom::Dir &p)
Definition: Log.h:223
Definition: Log.h:26
CORE_EXPORT LogClass Log(Core::LOGLEVEL level)
LogClass & nospace()
Definition: Log.h:196
LOGLEVEL
Definition: Log.h:19
Definition: Log.h:127
Defines a non-persistent vector in 3D space.
Definition: Vec.h:45
NoDebugClass & quote()
Definition: Log.h:118
LogClass & operator<<(signed int t)
Definition: Log.h:260
LogClass(QString *string)
Definition: Log.h:154
LogClass & operator<<(signed long t)
Definition: Log.h:270
Definition: Log.h:25
double x() const
For this point, returns its x coordinate.
LogClass & maybeSpace()
Definition: Log.h:201
~LogClass()
Definition: Log.h:158
CORE_EXPORT Core::LogClass cError()
virtual LogBaseClass & nospace()=0
LogClass & operator<<(const char *t)
Definition: Log.h:300
Definition: Log.h:24
NoDebugClass & operator<<(QTextStreamFunction)
Definition: Log.h:113
NoDebugClass & operator<<(QTextStreamManipulator)
Definition: Log.h:114
LogClass & operator=(const QDebug &other)
NoDebugClass & space()
Definition: Log.h:115
double y() const
For this vector, returns its Y coordinate.
CORE_EXPORT LOGLEVEL getLogLevel()
LogClass(QIODevice *device)
Definition: Log.h:153
Definition: Log.h:54
Definition: Log.h:21
Defines a non-persistent 2D cartesian point.
Definition: Pnt2d.h:33
static std::vector< LogWriter * > logwriter
Definition: Log.h:151
Definition: Log.h:23
NoDebugClass & operator<<(const T &)
Definition: Log.h:123
LogClass & operator<<(qint64 t)
Definition: Log.h:280
NoDebugClass & maybeSpace()
Definition: Log.h:117
CORE_EXPORT Core::LogClass cInfo()