RTAB-Map 0.25.2
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
ULogger.h
Go to the documentation of this file.
1/*
2Copyright (c) 2010, Mathieu Labbe
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holders nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef ULOGGER_H
29#define ULOGGER_H
30
31#include "rtabmap/utilite/utilite_export.h" // DLL export/import defines
32
33#include "rtabmap/utilite/UMutex.h"
34#include "rtabmap/utilite/UDestroyer.h"
35#include "rtabmap/utilite/UEvent.h"
36#include "rtabmap/utilite/UException.h"
37
38#include <stdio.h>
39#include <time.h>
40#include <string>
41#include <vector>
42#include <map>
43#include <set>
44
45#include <stdarg.h>
46
56/*
57 * Convenient macros for logging...
58 */
59#define ULOGGER_LOG(level, ...) ULogger::write(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
60
61#define ULOGGER_DEBUG(...) ULOGGER_LOG(ULogger::kDebug, __VA_ARGS__)
62#define ULOGGER_INFO(...) ULOGGER_LOG(ULogger::kInfo, __VA_ARGS__)
63#define ULOGGER_WARN(...) ULOGGER_LOG(ULogger::kWarning, __VA_ARGS__)
64#define ULOGGER_ERROR(...) ULOGGER_LOG(ULogger::kError, __VA_ARGS__)
65#define ULOGGER_FATAL(...) ULOGGER_LOG(ULogger::kFatal, __VA_ARGS__) // Throw UException
66
67#define UDEBUG(...) ULOGGER_DEBUG(__VA_ARGS__)
68#define UINFO(...) ULOGGER_INFO(__VA_ARGS__)
69#define UWARN(...) ULOGGER_WARN(__VA_ARGS__)
70#define UERROR(...) ULOGGER_ERROR(__VA_ARGS__)
71#define UFATAL(...) ULOGGER_FATAL(__VA_ARGS__) // Throw UException
72
73// Throw UException
74#define UASSERT(condition) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met!", #condition)
75#define UASSERT_MSG(condition, msg_str) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met! [%s]", #condition, msg_str)
76
129class ULogEvent : public UEvent
130{
131public:
137 ULogEvent(const std::string & msg, int level) :
138 UEvent(level),
139 msg_(msg)
140 {}
141 virtual ~ULogEvent() {}
145 const std::string & getMsg() const {return msg_;}
149 virtual std::string getClassName() const {return "ULogEvent";}
150private:
151 std::string msg_;
152};
153
237class UTILITE_EXPORT ULogger
238{
239
240public:
244 static const std::string kDefaultLogFileName;
245
252 enum Type{kTypeNoLog, kTypeConsole, kTypeFile};
253
260 enum Level{kDebug, kInfo, kWarning, kError, kFatal};
261
273 static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true);
274 static Type type() {return type_;}
275
276 // Setters
281 static void setPrintTime(bool printTime) {printTime_ = printTime;}
282 static bool isPrintTime() {return printTime_;}
283
288 static void setPrintLevel(bool printLevel) {printLevel_ = printLevel;}
289 static bool isPrintLevel() {return printLevel_;}
290
295 static void setPrintEndline(bool printEndline) {printEndline_ = printEndline;}
296 static bool isPrintEndLine() {return printEndline_;}
297
303 static void setPrintColored(bool printColored) {printColored_ = printColored;}
304 static bool isPrintColored() {return printColored_;}
305
310 static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
311 static bool isPrintWhere() {return printWhere_;}
312
317 static void setPrintThreadId(bool printThreadId) {printThreadID_ = printThreadId;}
318 static bool isPrintThreadId() {return printThreadID_;}
319
324 static void setPrintWhereFullPath(bool printWhereFullPath) {printWhereFullPath_ = printWhereFullPath;}
325 static bool isPrintWhereFullPath() {return printWhereFullPath_;}
326
337 static void setBuffered(bool buffered);
338 static bool isBuffered() {return buffered_;}
339
351 static void setLevel(ULogger::Level level) {level_ = level;}
352 static ULogger::Level level() {return level_;}
353
360 static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
361 static ULogger::Level eventLevel() {return eventLevel_;}
362
366 static void setTreadIdFilter(const std::set<unsigned long> & ids) {threadIdFilter_ = ids;}
367 static void setTreadIdFilter(const std::vector<std::string> & ids);
368 static const std::set<unsigned long> & getTreadIdFilter() {return threadIdFilter_;}
369
374 static void registerCurrentThread(const std::string & name);
375 static void unregisterCurrentThread();
376 static std::map<std::string, unsigned long> getRegisteredThreads();
377
381 static void reset();
382
387 static void flush();
388
389 /*
390 * Write a message to logger: use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL() instead.
391 * @param level the log level of this message
392 * @param file the file path
393 * @param line the line in the file
394 * @param function the function name in which the message is logged
395 * @param msg the message to write
396 * @param ... the variable arguments
397 */
398 static void write(ULogger::Level level,
399 const char * file,
400 int line,
401 const char *function,
402 const char* msg,
403 ...)
404#if defined(__GNUC__) || defined(__clang__)
405 // Let the compiler type-check every UDEBUG/UINFO/UWARN/UERROR
406 // format string against its arguments. Without this, a mismatch is
407 // invisible until the line actually executes -- and since
408 // ULogger::write() returns early when the message is below the
409 // current log level, a bad format can sit latent for years and then
410 // segfault only for users running at a lower level. `msg` is the
411 // 5th parameter (no implicit `this`: the function is static) and
412 // the varargs start at 6.
413 __attribute__((format(printf, 5, 6)))
414#endif
415 ;
416
422 static int getTime(std::string &timeStr);
423
424protected:
425 /*
426 * This method is used to have a reference on the
427 * Logger. When no Logger exists, one is
428 * created. There is only one instance in the application.
429 * Must be protected by loggerMutex_.
430 * See the Singleton pattern for further explanation.
431 *
432 * @return the reference on the Logger
433 */
434 static ULogger* getInstance();
435
436 /*
437 * Called only once in getInstance(). It can't be instanciated
438 * by the user.
439 *
440 * @see getInstance()
441 */
442 ULogger() {}
443
444 /*
445 * Only called by a Destroyer.
446 * @see Destroyer
447 */
448 virtual ~ULogger();
449
450 /*
451 * Flush buffered messages
452 */
453 void _flush();
454
455 /*
456 * A Destroyer is used to remove a dynamicaly created
457 * Singleton. It is friend here to have access to the
458 * destructor.
459 *
460 * @see Destroyer
461 */
462 friend class UDestroyer<ULogger>;
463
464 /*
465 * The log file name.
466 */
467 static std::string logFileName_;
468
469 /*
470 * Default true, it doesn't overwrite the file.
471 */
472 static bool append_;
473
474private:
475 /*
476 * Create an instance according to type. See the Abstract factory
477 * pattern for further explanation.
478 * @see type_
479 * @return the reference on the new logger
480 */
481 static ULogger* createInstance();
482
483 /*
484 * Write a message on the output with the format :
485 * "A message". Inherited class
486 * must override this method to output the message. It
487 * does nothing by default.
488 * @param msg the message to write.
489 * @param arg the variable arguments
490 */
491 virtual void _write(const char*, va_list) {} // Do nothing by default
492 virtual void _writeStr(const char*) {} // Do nothing by default
493
494private:
495 /*
496 * The Logger instance pointer.
497 */
498 static ULogger* instance_;
499
500 /*
501 * The Logger's destroyer
502 */
503 static UDestroyer<ULogger> destroyer_;
504
505 /*
506 * If the logger prints the time for each message.
507 * Default is true.
508 */
509 static bool printTime_;
510
511 /*
512 * If the logger prints the level for each message.
513 * Default is true.
514 */
515 static bool printLevel_;
516
517 /*
518 * If the logger prints the end line for each message.
519 * Default is true.
520 */
521 static bool printEndline_;
522
523 /*
524 * If the logger prints text with color.
525 * Default is true.
526 */
527 static bool printColored_;
528
529 /*
530 * If the logger prints where the message is logged (fileName::function():line).
531 * Default is true.
532 */
533 static bool printWhere_;
534
535 /*
536 * If the logger prints the full path of the source file
537 * where the message is written. Only works when
538 * "printWhere_" is true.
539 * Default is false.
540 */
541 static bool printWhereFullPath_;
542
543 /*
544 * If the logger prints the thread ID.
545 * Default is false.
546 */
547 static bool printThreadID_;
548
549 /*
550 * If the logger limit the size of the "where" path to
551 * characters. If the path is over 8 characters, a "~"
552 * is added. Only works when "printWhereFullPath_" is false.
553 * Default is false.
554 */
555 static bool limitWhereLength_;
556
557 /*
558 * The type of the logger.
559 */
560 static Type type_;
561
562 /*
563 * The severity of the log.
564 */
565 static Level level_;
566
567 /*
568 * The severity at which the message is also sent in a ULogEvent.
569 */
570 static Level eventLevel_;
571
572 static const char * levelName_[5];
573
574 /*
575 * Mutex used when accessing public functions.
576 */
577 static UMutex loggerMutex_;
578
579 /*
580 * If the logger prints messages only when ULogger::flush() is called.
581 * Default is false.
582 */
583 static bool buffered_;
584
585 static std::string bufferedMsgs_;
586
587 static std::set<unsigned long> threadIdFilter_;
588 static std::map<std::string, unsigned long> registeredThreads_;
589};
590
591#endif // ULOGGER_H
const std::string & getMsg() const
Definition ULogger.h:145
virtual std::string getClassName() const
Definition ULogger.h:149
ULogEvent(const std::string &msg, int level)
Definition ULogger.h:137
static void reset()
static void setPrintThreadId(bool printThreadId)
Definition ULogger.h:317
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:281
static void setPrintWhereFullPath(bool printWhereFullPath)
Definition ULogger.h:324
static void setPrintLevel(bool printLevel)
Definition ULogger.h:288
static void flush()
static void setPrintColored(bool printColored)
Definition ULogger.h:303
static void registerCurrentThread(const std::string &name)
static void setPrintEndline(bool printEndline)
Definition ULogger.h:295
static void setBuffered(bool buffered)
static void setEventLevel(ULogger::Level eventSentLevel)
Definition ULogger.h:360
static const std::string kDefaultLogFileName
Definition ULogger.h:244
static void setLevel(ULogger::Level level)
Definition ULogger.h:351
static void setTreadIdFilter(const std::set< unsigned long > &ids)
Definition ULogger.h:366
static void setPrintWhere(bool printWhere)
Definition ULogger.h:310