Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ULOGGER_H
00021 #define ULOGGER_H
00022
00023 #include "rtabmap/utilite/UtiLiteExp.h"
00024
00025 #include "rtabmap/utilite/UMutex.h"
00026 #include "rtabmap/utilite/UDestroyer.h"
00027 #include "rtabmap/utilite/UEvent.h"
00028 #include "rtabmap/utilite/UException.h"
00029
00030 #include <stdio.h>
00031 #include <time.h>
00032 #include <string>
00033 #include <vector>
00034
00035 #include <stdarg.h>
00036
00046
00047
00048
00049 #define ULOGGER_LOG(level, ...) ULogger::write(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
00050
00051 #define ULOGGER_DEBUG(...) ULOGGER_LOG(ULogger::kDebug, __VA_ARGS__)
00052 #define ULOGGER_INFO(...) ULOGGER_LOG(ULogger::kInfo, __VA_ARGS__)
00053 #define ULOGGER_WARN(...) ULOGGER_LOG(ULogger::kWarning, __VA_ARGS__)
00054 #define ULOGGER_ERROR(...) ULOGGER_LOG(ULogger::kError, __VA_ARGS__)
00055 #define ULOGGER_FATAL(...) ULOGGER_LOG(ULogger::kFatal, __VA_ARGS__) // Throw UException
00056
00057 #define UDEBUG(...) ULOGGER_DEBUG(__VA_ARGS__)
00058 #define UINFO(...) ULOGGER_INFO(__VA_ARGS__)
00059 #define UWARN(...) ULOGGER_WARN(__VA_ARGS__)
00060 #define UERROR(...) ULOGGER_ERROR(__VA_ARGS__)
00061 #define UFATAL(...) ULOGGER_FATAL(__VA_ARGS__) // Throw UException
00062
00063
00064 #define UASSERT(condition) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met!", #condition)
00065 #define UASSERT_MSG(condition, msg_str) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met! [%s]", #condition, msg_str)
00066
00119 class ULogEvent : public UEvent
00120 {
00121 public:
00127 ULogEvent(const std::string & msg, int level) :
00128 UEvent(level),
00129 msg_(msg)
00130 {}
00131 virtual ~ULogEvent() {}
00135 const std::string & getMsg() const {return msg_;}
00139 virtual std::string getClassName() const {return "ULogEvent";}
00140 private:
00141 std::string msg_;
00142 };
00143
00227 class UTILITE_EXP ULogger
00228 {
00229
00230 public:
00234 static const std::string kDefaultLogFileName;
00235
00242 enum Type{kTypeNoLog, kTypeConsole, kTypeFile};
00243
00250 enum Level{kDebug, kInfo, kWarning, kError, kFatal};
00251
00263 static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true);
00264 static Type type() {return type_;}
00265
00266
00271 static void setPrintTime(bool printTime) {printTime_ = printTime;}
00272 static bool isPrintTime() {return printTime_;}
00273
00278 static void setPrintLevel(bool printLevel) {printLevel_ = printLevel;}
00279 static bool isPrintLevel() {return printLevel_;}
00280
00285 static void setPrintEndline(bool printEndline) {printEndline_ = printEndline;}
00286 static bool isPrintEndLine() {return printEndline_;}
00287
00293 static void setPrintColored(bool printColored) {printColored_ = printColored;}
00294 static bool isPrintColored() {return printColored_;}
00295
00300 static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
00301 static bool isPrintWhere() {return printWhere_;}
00302
00307 static void setPrintThreadId(bool printThreadId) {printThreadID_ = printThreadId;}
00308 static bool isPrintThreadId() {return printThreadID_;}
00309
00314 static void setPrintWhereFullPath(bool printWhereFullPath) {printWhereFullPath_ = printWhereFullPath;}
00315 static bool isPrintWhereFullPath() {return printWhereFullPath_;}
00316
00323 static void setBuffered(bool buffered);
00324 static bool isBuffered() {return buffered_;}
00325
00337 static void setLevel(ULogger::Level level) {level_ = level;}
00338 static ULogger::Level level() {return level_;}
00339
00346 static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
00347 static ULogger::Level eventLevel() {return eventLevel_;}
00348
00352 static void reset();
00353
00358 static void flush();
00359
00366 static void write(const char* msg, ...);
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 static void write(ULogger::Level level,
00378 const char * file,
00379 int line,
00380 const char *function,
00381 const char* msg,
00382 ...);
00383
00389 static int getTime(std::string &timeStr);
00390
00391 protected:
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 static ULogger* getInstance();
00402
00403
00404
00405
00406
00407
00408
00409 ULogger() {}
00410
00411
00412
00413
00414
00415 virtual ~ULogger();
00416
00417
00418
00419
00420 void _flush();
00421
00422
00423
00424
00425
00426
00427
00428
00429 friend class UDestroyer<ULogger>;
00430
00431
00432
00433
00434 static std::string logFileName_;
00435
00436
00437
00438
00439 static bool append_;
00440
00441 private:
00442
00443
00444
00445
00446
00447
00448 static ULogger* createInstance();
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458 virtual void _write(const char* msg, va_list arg) {}
00459 virtual void _writeStr(const char* msg) {}
00460
00461 private:
00462
00463
00464
00465 static ULogger* instance_;
00466
00467
00468
00469
00470 static UDestroyer<ULogger> destroyer_;
00471
00472
00473
00474
00475
00476 static bool printTime_;
00477
00478
00479
00480
00481
00482 static bool printLevel_;
00483
00484
00485
00486
00487
00488 static bool printEndline_;
00489
00490
00491
00492
00493
00494 static bool printColored_;
00495
00496
00497
00498
00499
00500 static bool printWhere_;
00501
00502
00503
00504
00505
00506
00507
00508 static bool printWhereFullPath_;
00509
00510
00511
00512
00513
00514 static bool printThreadID_;
00515
00516
00517
00518
00519
00520
00521
00522 static bool limitWhereLength_;
00523
00524
00525
00526
00527 static Type type_;
00528
00529
00530
00531
00532 static Level level_;
00533
00534
00535
00536
00537 static Level eventLevel_;
00538
00539 static const char * levelName_[5];
00540
00541
00542
00543
00544 static UMutex loggerMutex_;
00545
00546
00547
00548
00549
00550 static bool buffered_;
00551
00552 static std::string bufferedMsgs_;
00553 };
00554
00555 #endif // ULOGGER_H