ULogger.h
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef ULOGGER_H
21 #define ULOGGER_H
22 
23 #include "rtabmap/utilite/UtiLiteExp.h" // DLL export/import defines
24 
25 #include "rtabmap/utilite/UMutex.h"
27 #include "rtabmap/utilite/UEvent.h"
29 
30 #include <stdio.h>
31 #include <time.h>
32 #include <string>
33 #include <vector>
34 #include <map>
35 #include <set>
36 
37 #include <stdarg.h>
38 
48 /*
49  * Convenient macros for logging...
50  */
51 #define ULOGGER_LOG(level, ...) ULogger::write(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
52 
53 #define ULOGGER_DEBUG(...) ULOGGER_LOG(ULogger::kDebug, __VA_ARGS__)
54 #define ULOGGER_INFO(...) ULOGGER_LOG(ULogger::kInfo, __VA_ARGS__)
55 #define ULOGGER_WARN(...) ULOGGER_LOG(ULogger::kWarning, __VA_ARGS__)
56 #define ULOGGER_ERROR(...) ULOGGER_LOG(ULogger::kError, __VA_ARGS__)
57 #define ULOGGER_FATAL(...) ULOGGER_LOG(ULogger::kFatal, __VA_ARGS__) // Throw UException
58 
59 #define UDEBUG(...) ULOGGER_DEBUG(__VA_ARGS__)
60 #define UINFO(...) ULOGGER_INFO(__VA_ARGS__)
61 #define UWARN(...) ULOGGER_WARN(__VA_ARGS__)
62 #define UERROR(...) ULOGGER_ERROR(__VA_ARGS__)
63 #define UFATAL(...) ULOGGER_FATAL(__VA_ARGS__) // Throw UException
64 
65 // Throw UException
66 #define UASSERT(condition) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met!", #condition)
67 #define UASSERT_MSG(condition, msg_str) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met! [%s]", #condition, msg_str)
68 
121 class ULogEvent : public UEvent
122 {
123 public:
129  ULogEvent(const std::string & msg, int level) :
130  UEvent(level),
131  msg_(msg)
132  {}
133  virtual ~ULogEvent() {}
137  const std::string & getMsg() const {return msg_;}
141  virtual std::string getClassName() const {return "ULogEvent";}
142 private:
143  std::string msg_;
144 };
145 
230 {
231 
232 public:
236  static const std::string kDefaultLogFileName;
237 
244  enum Type{kTypeNoLog, kTypeConsole, kTypeFile};
245 
252  enum Level{kDebug, kInfo, kWarning, kError, kFatal};
253 
265  static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true);
266  static Type type() {return type_;}
267 
268  // Setters
273  static void setPrintTime(bool printTime) {printTime_ = printTime;}
274  static bool isPrintTime() {return printTime_;}
275 
280  static void setPrintLevel(bool printLevel) {printLevel_ = printLevel;}
281  static bool isPrintLevel() {return printLevel_;}
282 
287  static void setPrintEndline(bool printEndline) {printEndline_ = printEndline;}
288  static bool isPrintEndLine() {return printEndline_;}
289 
295  static void setPrintColored(bool printColored) {printColored_ = printColored;}
296  static bool isPrintColored() {return printColored_;}
297 
302  static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
303  static bool isPrintWhere() {return printWhere_;}
304 
309  static void setPrintThreadId(bool printThreadId) {printThreadID_ = printThreadId;}
310  static bool isPrintThreadId() {return printThreadID_;}
311 
316  static void setPrintWhereFullPath(bool printWhereFullPath) {printWhereFullPath_ = printWhereFullPath;}
317  static bool isPrintWhereFullPath() {return printWhereFullPath_;}
318 
325  static void setBuffered(bool buffered);
326  static bool isBuffered() {return buffered_;}
327 
339  static void setLevel(ULogger::Level level) {level_ = level;}
340  static ULogger::Level level() {return level_;}
341 
348  static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
349  static ULogger::Level eventLevel() {return eventLevel_;}
350 
354  static void setTreadIdFilter(const std::set<unsigned long> & ids) {threadIdFilter_ = ids;}
355  static void setTreadIdFilter(const std::vector<std::string> & ids);
356  static const std::set<unsigned long> & getTreadIdFilter() {return threadIdFilter_;}
357 
362  static void registerCurrentThread(const std::string & name);
363  static void unregisterCurrentThread();
364  static std::map<std::string, unsigned long> getRegisteredThreads();
365 
369  static void reset();
370 
375  static void flush();
376 
383  static void write(const char* msg, ...);
384 
385  /*
386  * Write a message to logger: use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL() instead.
387  * @param level the log level of this message
388  * @param file the file path
389  * @param line the line in the file
390  * @param function the function name in which the message is logged
391  * @param msg the message to write
392  * @param ... the variable arguments
393  */
394  static void write(ULogger::Level level,
395  const char * file,
396  int line,
397  const char *function,
398  const char* msg,
399  ...);
400 
406  static int getTime(std::string &timeStr);
407 
408 protected:
409  /*
410  * This method is used to have a reference on the
411  * Logger. When no Logger exists, one is
412  * created. There is only one instance in the application.
413  * Must be protected by loggerMutex_.
414  * See the Singleton pattern for further explanation.
415  *
416  * @return the reference on the Logger
417  */
418  static ULogger* getInstance();
419 
420  /*
421  * Called only once in getInstance(). It can't be instanciated
422  * by the user.
423  *
424  * @see getInstance()
425  */
426  ULogger() {}
427 
428  /*
429  * Only called by a Destroyer.
430  * @see Destroyer
431  */
432  virtual ~ULogger();
433 
434  /*
435  * Flush buffered messages
436  */
437  void _flush();
438 
439  /*
440  * A Destroyer is used to remove a dynamicaly created
441  * Singleton. It is friend here to have access to the
442  * destructor.
443  *
444  * @see Destroyer
445  */
446  friend class UDestroyer<ULogger>;
447 
448  /*
449  * The log file name.
450  */
451  static std::string logFileName_;
452 
453  /*
454  * Default true, it doesn't overwrite the file.
455  */
456  static bool append_;
457 
458 private:
459  /*
460  * Create an instance according to type. See the Abstract factory
461  * pattern for further explanation.
462  * @see type_
463  * @return the reference on the new logger
464  */
465  static ULogger* createInstance();
466 
467  /*
468  * Write a message on the output with the format :
469  * "A message". Inherited class
470  * must override this method to output the message. It
471  * does nothing by default.
472  * @param msg the message to write.
473  * @param arg the variable arguments
474  */
475  virtual void _write(const char*, va_list) {} // Do nothing by default
476  virtual void _writeStr(const char*) {} // Do nothing by default
477 
478 private:
479  /*
480  * The Logger instance pointer.
481  */
483 
484  /*
485  * The Logger's destroyer
486  */
488 
489  /*
490  * If the logger prints the time for each message.
491  * Default is true.
492  */
493  static bool printTime_;
494 
495  /*
496  * If the logger prints the level for each message.
497  * Default is true.
498  */
499  static bool printLevel_;
500 
501  /*
502  * If the logger prints the end line for each message.
503  * Default is true.
504  */
505  static bool printEndline_;
506 
507  /*
508  * If the logger prints text with color.
509  * Default is true.
510  */
511  static bool printColored_;
512 
513  /*
514  * If the logger prints where the message is logged (fileName::function():line).
515  * Default is true.
516  */
517  static bool printWhere_;
518 
519  /*
520  * If the logger prints the full path of the source file
521  * where the message is written. Only works when
522  * "printWhere_" is true.
523  * Default is false.
524  */
525  static bool printWhereFullPath_;
526 
527  /*
528  * If the logger prints the thread ID.
529  * Default is false.
530  */
531  static bool printThreadID_;
532 
533  /*
534  * If the logger limit the size of the "where" path to
535  * characters. If the path is over 8 characters, a "~"
536  * is added. Only works when "printWhereFullPath_" is false.
537  * Default is false.
538  */
539  static bool limitWhereLength_;
540 
541  /*
542  * The type of the logger.
543  */
544  static Type type_;
545 
546  /*
547  * The severity of the log.
548  */
549  static Level level_;
550 
551  /*
552  * The severity at which the message is also sent in a ULogEvent.
553  */
555 
556  static const char * levelName_[5];
557 
558  /*
559  * Mutex used when accessing public functions.
560  */
562 
563  /*
564  * If the logger prints messages only when ULogger::flush() is called.
565  * Default is false.
566  */
567  static bool buffered_;
568 
569  static std::string bufferedMsgs_;
570 
571  static std::set<unsigned long> threadIdFilter_;
572  static std::map<std::string, unsigned long> registeredThreads_;
573 };
574 
575 #endif // ULOGGER_H
static void setPrintThreadId(bool printThreadId)
Definition: ULogger.h:309
static Level eventLevel_
Definition: ULogger.h:554
static bool buffered_
Definition: ULogger.h:567
static const std::string kDefaultLogFileName
Definition: ULogger.h:236
Definition: UEvent.h:57
static void setPrintLevel(bool printLevel)
Definition: ULogger.h:280
static std::map< std::string, unsigned long > registeredThreads_
Definition: ULogger.h:572
static bool isBuffered()
Definition: ULogger.h:326
#define UTILITE_EXP
Definition: UtiLiteExp.h:33
static std::string bufferedMsgs_
Definition: ULogger.h:569
static bool printThreadID_
Definition: ULogger.h:531
static bool printEndline_
Definition: ULogger.h:505
static bool limitWhereLength_
Definition: ULogger.h:539
static ULogger * instance_
Definition: ULogger.h:482
static ULogger::Level eventLevel()
Definition: ULogger.h:349
static void setTreadIdFilter(const std::set< unsigned long > &ids)
Definition: ULogger.h:354
static void setLevel(ULogger::Level level)
Definition: ULogger.h:339
static Level level_
Definition: ULogger.h:549
static bool printLevel_
Definition: ULogger.h:499
static bool isPrintLevel()
Definition: ULogger.h:281
std::string msg_
Definition: ULogger.h:143
virtual void _writeStr(const char *)
Definition: ULogger.h:476
static bool printWhereFullPath_
Definition: ULogger.h:525
virtual ~ULogEvent()
Definition: ULogger.h:133
static void setPrintTime(bool printTime)
Definition: ULogger.h:273
static Type type()
Definition: ULogger.h:266
string name
Definition: UMutex.h:54
const std::string & getMsg() const
Definition: ULogger.h:137
static void setEventLevel(ULogger::Level eventSentLevel)
Definition: ULogger.h:348
static void setPrintEndline(bool printEndline)
Definition: ULogger.h:287
static const std::set< unsigned long > & getTreadIdFilter()
Definition: ULogger.h:356
static std::string logFileName_
Definition: ULogger.h:451
static bool printColored_
Definition: ULogger.h:511
static void setPrintWhere(bool printWhere)
Definition: ULogger.h:302
static void setPrintWhereFullPath(bool printWhereFullPath)
Definition: ULogger.h:316
static ULogger::Level level()
Definition: ULogger.h:340
static bool isPrintEndLine()
Definition: ULogger.h:288
virtual std::string getClassName() const
Definition: ULogger.h:141
static bool isPrintWhereFullPath()
Definition: ULogger.h:317
ULogEvent(const std::string &msg, int level)
Definition: ULogger.h:129
virtual void _write(const char *, va_list)
Definition: ULogger.h:475
static bool isPrintWhere()
Definition: ULogger.h:303
static bool append_
Definition: ULogger.h:456
static bool isPrintColored()
Definition: ULogger.h:296
Base * createInstance(const std::string &derived_class_name, ClassLoader *loader)
ULogger()
Definition: ULogger.h:426
static bool printWhere_
Definition: ULogger.h:517
static bool printTime_
Definition: ULogger.h:493
static bool isPrintThreadId()
Definition: ULogger.h:310
static bool isPrintTime()
Definition: ULogger.h:274
static Type type_
Definition: ULogger.h:544
static UDestroyer< ULogger > destroyer_
Definition: ULogger.h:487
static std::set< unsigned long > threadIdFilter_
Definition: ULogger.h:571
static UMutex loggerMutex_
Definition: ULogger.h:561
static void setPrintColored(bool printColored)
Definition: ULogger.h:295


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:58