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#if defined(__GNUC__) || defined(__clang__)
397 // Let the compiler type-check every UDEBUG/UINFO/UWARN/UERROR
398 // format string against its arguments. Without this, a mismatch is
399 // invisible until the line actually executes -- and since
400 // ULogger::write() returns early when the message is below the
401 // current log level, a bad format can sit latent for years and then
402 // segfault only for users running at a lower level. `msg` is the
403 // 5th parameter (no implicit `this`: the function is static) and
404 // the varargs start at 6.
405 __attribute__((format(printf, 5, 6)))
406#endif
407 ;
408
414 static int getTime(std::string &timeStr);
415
416protected:
417 /*
418 * This method is used to have a reference on the
419 * Logger. When no Logger exists, one is
420 * created. There is only one instance in the application.
421 * Must be protected by loggerMutex_.
422 * See the Singleton pattern for further explanation.
423 *
424 * @return the reference on the Logger
425 */
426 static ULogger* getInstance();
427
428 /*
429 * Called only once in getInstance(). It can't be instanciated
430 * by the user.
431 *
432 * @see getInstance()
433 */
434 ULogger() {}
435
436 /*
437 * Only called by a Destroyer.
438 * @see Destroyer
439 */
440 virtual ~ULogger();
441
442 /*
443 * Flush buffered messages
444 */
445 void _flush();
446
447 /*
448 * A Destroyer is used to remove a dynamicaly created
449 * Singleton. It is friend here to have access to the
450 * destructor.
451 *
452 * @see Destroyer
453 */
454 friend class UDestroyer<ULogger>;
455
456 /*
457 * The log file name.
458 */
459 static std::string logFileName_;
460
461 /*
462 * Default true, it doesn't overwrite the file.
463 */
464 static bool append_;
465
466private:
467 /*
468 * Create an instance according to type. See the Abstract factory
469 * pattern for further explanation.
470 * @see type_
471 * @return the reference on the new logger
472 */
473 static ULogger* createInstance();
474
475 /*
476 * Write a message on the output with the format :
477 * "A message". Inherited class
478 * must override this method to output the message. It
479 * does nothing by default.
480 * @param msg the message to write.
481 * @param arg the variable arguments
482 */
483 virtual void _write(const char*, va_list) {} // Do nothing by default
484 virtual void _writeStr(const char*) {} // Do nothing by default
485
486private:
487 /*
488 * The Logger instance pointer.
489 */
490 static ULogger* instance_;
491
492 /*
493 * The Logger's destroyer
494 */
495 static UDestroyer<ULogger> destroyer_;
496
497 /*
498 * If the logger prints the time for each message.
499 * Default is true.
500 */
501 static bool printTime_;
502
503 /*
504 * If the logger prints the level for each message.
505 * Default is true.
506 */
507 static bool printLevel_;
508
509 /*
510 * If the logger prints the end line for each message.
511 * Default is true.
512 */
513 static bool printEndline_;
514
515 /*
516 * If the logger prints text with color.
517 * Default is true.
518 */
519 static bool printColored_;
520
521 /*
522 * If the logger prints where the message is logged (fileName::function():line).
523 * Default is true.
524 */
525 static bool printWhere_;
526
527 /*
528 * If the logger prints the full path of the source file
529 * where the message is written. Only works when
530 * "printWhere_" is true.
531 * Default is false.
532 */
533 static bool printWhereFullPath_;
534
535 /*
536 * If the logger prints the thread ID.
537 * Default is false.
538 */
539 static bool printThreadID_;
540
541 /*
542 * If the logger limit the size of the "where" path to
543 * characters. If the path is over 8 characters, a "~"
544 * is added. Only works when "printWhereFullPath_" is false.
545 * Default is false.
546 */
547 static bool limitWhereLength_;
548
549 /*
550 * The type of the logger.
551 */
552 static Type type_;
553
554 /*
555 * The severity of the log.
556 */
557 static Level level_;
558
559 /*
560 * The severity at which the message is also sent in a ULogEvent.
561 */
562 static Level eventLevel_;
563
564 static const char * levelName_[5];
565
566 /*
567 * Mutex used when accessing public functions.
568 */
569 static UMutex loggerMutex_;
570
571 /*
572 * If the logger prints messages only when ULogger::flush() is called.
573 * Default is false.
574 */
575 static bool buffered_;
576
577 static std::string bufferedMsgs_;
578
579 static std::set<unsigned long> threadIdFilter_;
580 static std::map<std::string, unsigned long> registeredThreads_;
581};
582
583#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