RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
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/utilite_export.h" // DLL export/import defines
24
25#include "rtabmap/utilite/UMutex.h"
26#include "rtabmap/utilite/UDestroyer.h"
27#include "rtabmap/utilite/UEvent.h"
28#include "rtabmap/utilite/UException.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
121class ULogEvent : public UEvent
122{
123public:
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";}
142private:
143 std::string msg_;
144};
145
229class UTILITE_EXPORT ULogger
230{
231
232public:
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
329 static void setBuffered(bool buffered);
330 static bool isBuffered() {return buffered_;}
331
343 static void setLevel(ULogger::Level level) {level_ = level;}
344 static ULogger::Level level() {return level_;}
345
352 static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
353 static ULogger::Level eventLevel() {return eventLevel_;}
354
358 static void setTreadIdFilter(const std::set<unsigned long> & ids) {threadIdFilter_ = ids;}
359 static void setTreadIdFilter(const std::vector<std::string> & ids);
360 static const std::set<unsigned long> & getTreadIdFilter() {return threadIdFilter_;}
361
366 static void registerCurrentThread(const std::string & name);
367 static void unregisterCurrentThread();
368 static std::map<std::string, unsigned long> getRegisteredThreads();
369
373 static void reset();
374
379 static void flush();
380
381 /*
382 * Write a message to logger: use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL() instead.
383 * @param level the log level of this message
384 * @param file the file path
385 * @param line the line in the file
386 * @param function the function name in which the message is logged
387 * @param msg the message to write
388 * @param ... the variable arguments
389 */
390 static void write(ULogger::Level level,
391 const char * file,
392 int line,
393 const char *function,
394 const char* msg,
395 ...);
396
402 static int getTime(std::string &timeStr);
403
404protected:
405 /*
406 * This method is used to have a reference on the
407 * Logger. When no Logger exists, one is
408 * created. There is only one instance in the application.
409 * Must be protected by loggerMutex_.
410 * See the Singleton pattern for further explanation.
411 *
412 * @return the reference on the Logger
413 */
414 static ULogger* getInstance();
415
416 /*
417 * Called only once in getInstance(). It can't be instanciated
418 * by the user.
419 *
420 * @see getInstance()
421 */
422 ULogger() {}
423
424 /*
425 * Only called by a Destroyer.
426 * @see Destroyer
427 */
428 virtual ~ULogger();
429
430 /*
431 * Flush buffered messages
432 */
433 void _flush();
434
435 /*
436 * A Destroyer is used to remove a dynamicaly created
437 * Singleton. It is friend here to have access to the
438 * destructor.
439 *
440 * @see Destroyer
441 */
442 friend class UDestroyer<ULogger>;
443
444 /*
445 * The log file name.
446 */
447 static std::string logFileName_;
448
449 /*
450 * Default true, it doesn't overwrite the file.
451 */
452 static bool append_;
453
454private:
455 /*
456 * Create an instance according to type. See the Abstract factory
457 * pattern for further explanation.
458 * @see type_
459 * @return the reference on the new logger
460 */
461 static ULogger* createInstance();
462
463 /*
464 * Write a message on the output with the format :
465 * "A message". Inherited class
466 * must override this method to output the message. It
467 * does nothing by default.
468 * @param msg the message to write.
469 * @param arg the variable arguments
470 */
471 virtual void _write(const char*, va_list) {} // Do nothing by default
472 virtual void _writeStr(const char*) {} // Do nothing by default
473
474private:
475 /*
476 * The Logger instance pointer.
477 */
478 static ULogger* instance_;
479
480 /*
481 * The Logger's destroyer
482 */
483 static UDestroyer<ULogger> destroyer_;
484
485 /*
486 * If the logger prints the time for each message.
487 * Default is true.
488 */
489 static bool printTime_;
490
491 /*
492 * If the logger prints the level for each message.
493 * Default is true.
494 */
495 static bool printLevel_;
496
497 /*
498 * If the logger prints the end line for each message.
499 * Default is true.
500 */
501 static bool printEndline_;
502
503 /*
504 * If the logger prints text with color.
505 * Default is true.
506 */
507 static bool printColored_;
508
509 /*
510 * If the logger prints where the message is logged (fileName::function():line).
511 * Default is true.
512 */
513 static bool printWhere_;
514
515 /*
516 * If the logger prints the full path of the source file
517 * where the message is written. Only works when
518 * "printWhere_" is true.
519 * Default is false.
520 */
521 static bool printWhereFullPath_;
522
523 /*
524 * If the logger prints the thread ID.
525 * Default is false.
526 */
527 static bool printThreadID_;
528
529 /*
530 * If the logger limit the size of the "where" path to
531 * characters. If the path is over 8 characters, a "~"
532 * is added. Only works when "printWhereFullPath_" is false.
533 * Default is false.
534 */
535 static bool limitWhereLength_;
536
537 /*
538 * The type of the logger.
539 */
540 static Type type_;
541
542 /*
543 * The severity of the log.
544 */
545 static Level level_;
546
547 /*
548 * The severity at which the message is also sent in a ULogEvent.
549 */
550 static Level eventLevel_;
551
552 static const char * levelName_[5];
553
554 /*
555 * Mutex used when accessing public functions.
556 */
557 static UMutex loggerMutex_;
558
559 /*
560 * If the logger prints messages only when ULogger::flush() is called.
561 * Default is false.
562 */
563 static bool buffered_;
564
565 static std::string bufferedMsgs_;
566
567 static std::set<unsigned long> threadIdFilter_;
568 static std::map<std::string, unsigned long> registeredThreads_;
569};
570
571#endif // ULOGGER_H
const std::string & getMsg() const
Definition ULogger.h:137
virtual std::string getClassName() const
Definition ULogger.h:141
ULogEvent(const std::string &msg, int level)
Definition ULogger.h:129
static void reset()
static void setPrintThreadId(bool printThreadId)
Definition ULogger.h:309
static void setType(Type type, const std::string &fileName=kDefaultLogFileName, bool append=true)
static int getTime(std::string &timeStr)
static void setPrintTime(bool printTime)
Definition ULogger.h:273
static void setPrintWhereFullPath(bool printWhereFullPath)
Definition ULogger.h:316
static void setPrintLevel(bool printLevel)
Definition ULogger.h:280
static void flush()
static void setPrintColored(bool printColored)
Definition ULogger.h:295
static void registerCurrentThread(const std::string &name)
static void setPrintEndline(bool printEndline)
Definition ULogger.h:287
static void setBuffered(bool buffered)
static void setEventLevel(ULogger::Level eventSentLevel)
Definition ULogger.h:352
static const std::string kDefaultLogFileName
Definition ULogger.h:236
static void setLevel(ULogger::Level level)
Definition ULogger.h:343
static void setTreadIdFilter(const std::set< unsigned long > &ids)
Definition ULogger.h:358
static void setPrintWhere(bool printWhere)
Definition ULogger.h:302