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 #include <map>
00035 #include <set>
00036
00037 #include <stdarg.h>
00038
00048
00049
00050
00051 #define ULOGGER_LOG(level, ...) ULogger::write(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
00052
00053 #define ULOGGER_DEBUG(...) ULOGGER_LOG(ULogger::kDebug, __VA_ARGS__)
00054 #define ULOGGER_INFO(...) ULOGGER_LOG(ULogger::kInfo, __VA_ARGS__)
00055 #define ULOGGER_WARN(...) ULOGGER_LOG(ULogger::kWarning, __VA_ARGS__)
00056 #define ULOGGER_ERROR(...) ULOGGER_LOG(ULogger::kError, __VA_ARGS__)
00057 #define ULOGGER_FATAL(...) ULOGGER_LOG(ULogger::kFatal, __VA_ARGS__) // Throw UException
00058
00059 #define UDEBUG(...) ULOGGER_DEBUG(__VA_ARGS__)
00060 #define UINFO(...) ULOGGER_INFO(__VA_ARGS__)
00061 #define UWARN(...) ULOGGER_WARN(__VA_ARGS__)
00062 #define UERROR(...) ULOGGER_ERROR(__VA_ARGS__)
00063 #define UFATAL(...) ULOGGER_FATAL(__VA_ARGS__) // Throw UException
00064
00065
00066 #define UASSERT(condition) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met!", #condition)
00067 #define UASSERT_MSG(condition, msg_str) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met! [%s]", #condition, msg_str)
00068
00121 class ULogEvent : public UEvent
00122 {
00123 public:
00129 ULogEvent(const std::string & msg, int level) :
00130 UEvent(level),
00131 msg_(msg)
00132 {}
00133 virtual ~ULogEvent() {}
00137 const std::string & getMsg() const {return msg_;}
00141 virtual std::string getClassName() const {return "ULogEvent";}
00142 private:
00143 std::string msg_;
00144 };
00145
00229 class UTILITE_EXP ULogger
00230 {
00231
00232 public:
00236 static const std::string kDefaultLogFileName;
00237
00244 enum Type{kTypeNoLog, kTypeConsole, kTypeFile};
00245
00252 enum Level{kDebug, kInfo, kWarning, kError, kFatal};
00253
00265 static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true);
00266 static Type type() {return type_;}
00267
00268
00273 static void setPrintTime(bool printTime) {printTime_ = printTime;}
00274 static bool isPrintTime() {return printTime_;}
00275
00280 static void setPrintLevel(bool printLevel) {printLevel_ = printLevel;}
00281 static bool isPrintLevel() {return printLevel_;}
00282
00287 static void setPrintEndline(bool printEndline) {printEndline_ = printEndline;}
00288 static bool isPrintEndLine() {return printEndline_;}
00289
00295 static void setPrintColored(bool printColored) {printColored_ = printColored;}
00296 static bool isPrintColored() {return printColored_;}
00297
00302 static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
00303 static bool isPrintWhere() {return printWhere_;}
00304
00309 static void setPrintThreadId(bool printThreadId) {printThreadID_ = printThreadId;}
00310 static bool isPrintThreadId() {return printThreadID_;}
00311
00316 static void setPrintWhereFullPath(bool printWhereFullPath) {printWhereFullPath_ = printWhereFullPath;}
00317 static bool isPrintWhereFullPath() {return printWhereFullPath_;}
00318
00325 static void setBuffered(bool buffered);
00326 static bool isBuffered() {return buffered_;}
00327
00339 static void setLevel(ULogger::Level level) {level_ = level;}
00340 static ULogger::Level level() {return level_;}
00341
00348 static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
00349 static ULogger::Level eventLevel() {return eventLevel_;}
00350
00354 static void setTreadIdFilter(const std::set<unsigned long> & ids) {threadIdFilter_ = ids;}
00355 static void setTreadIdFilter(const std::vector<std::string> & ids);
00356 static const std::set<unsigned long> & getTreadIdFilter() {return threadIdFilter_;}
00357
00362 static void registerCurrentThread(const std::string & name);
00363 static void unregisterCurrentThread();
00364 static std::map<std::string, unsigned long> getRegisteredThreads();
00365
00369 static void reset();
00370
00375 static void flush();
00376
00383 static void write(const char* msg, ...);
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 static void write(ULogger::Level level,
00395 const char * file,
00396 int line,
00397 const char *function,
00398 const char* msg,
00399 ...);
00400
00406 static int getTime(std::string &timeStr);
00407
00408 protected:
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 static ULogger* getInstance();
00419
00420
00421
00422
00423
00424
00425
00426 ULogger() {}
00427
00428
00429
00430
00431
00432 virtual ~ULogger();
00433
00434
00435
00436
00437 void _flush();
00438
00439
00440
00441
00442
00443
00444
00445
00446 friend class UDestroyer<ULogger>;
00447
00448
00449
00450
00451 static std::string logFileName_;
00452
00453
00454
00455
00456 static bool append_;
00457
00458 private:
00459
00460
00461
00462
00463
00464
00465 static ULogger* createInstance();
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475 virtual void _write(const char* msg, va_list arg) {}
00476 virtual void _writeStr(const char* msg) {}
00477
00478 private:
00479
00480
00481
00482 static ULogger* instance_;
00483
00484
00485
00486
00487 static UDestroyer<ULogger> destroyer_;
00488
00489
00490
00491
00492
00493 static bool printTime_;
00494
00495
00496
00497
00498
00499 static bool printLevel_;
00500
00501
00502
00503
00504
00505 static bool printEndline_;
00506
00507
00508
00509
00510
00511 static bool printColored_;
00512
00513
00514
00515
00516
00517 static bool printWhere_;
00518
00519
00520
00521
00522
00523
00524
00525 static bool printWhereFullPath_;
00526
00527
00528
00529
00530
00531 static bool printThreadID_;
00532
00533
00534
00535
00536
00537
00538
00539 static bool limitWhereLength_;
00540
00541
00542
00543
00544 static Type type_;
00545
00546
00547
00548
00549 static Level level_;
00550
00551
00552
00553
00554 static Level eventLevel_;
00555
00556 static const char * levelName_[5];
00557
00558
00559
00560
00561 static UMutex loggerMutex_;
00562
00563
00564
00565
00566
00567 static bool buffered_;
00568
00569 static std::string bufferedMsgs_;
00570
00571 static std::set<unsigned long> threadIdFilter_;
00572 static std::map<std::string, unsigned long> registeredThreads_;
00573 };
00574
00575 #endif // ULOGGER_H