logger.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* ---------------------------------------------------------------------------
  2. ** This software is in the public domain, furnished "as is", without technical
  3. ** support, and with no warranty, express or implied, as to its usefulness for
  4. ** any purpose.
  5. **
  6. ** logger.h
  7. **
  8. ** -------------------------------------------------------------------------*/
  9. #ifndef LOGGER_H
  10. #define LOGGER_H
  11. #include <unistd.h>
  12. #ifdef HAVE_LOG4CPP
  13. #include "log4cpp/Category.hh"
  14. #include "log4cpp/FileAppender.hh"
  15. #include "log4cpp/PatternLayout.hh"
  16. #define LOG(__level) log4cpp::Category::getRoot() << log4cpp::Priority::__level << __FILE__ << ":" << __LINE__ << "\n\t"
  17. inline void initLogger(int verbose)
  18. {
  19. // initialize log4cpp
  20. log4cpp::Category &log = log4cpp::Category::getRoot();
  21. log4cpp::Appender *app = new log4cpp::FileAppender("root", fileno(stdout));
  22. if (app)
  23. {
  24. log4cpp::PatternLayout *plt = new log4cpp::PatternLayout();
  25. if (plt)
  26. {
  27. plt->setConversionPattern("%d [%-6p] - %m%n");
  28. app->setLayout(plt);
  29. }
  30. log.addAppender(app);
  31. }
  32. switch (verbose)
  33. {
  34. case 2: log.setPriority(log4cpp::Priority::DEBUG); break;
  35. case 1: log.setPriority(log4cpp::Priority::INFO); break;
  36. default: log.setPriority(log4cpp::Priority::NOTICE); break;
  37. }
  38. LOG(INFO) << "level:" << log4cpp::Priority::getPriorityName(log.getPriority());
  39. }
  40. #else
  41. typedef enum {EMERG = 0,
  42. FATAL = 0,
  43. ALERT = 100,
  44. CRIT = 200,
  45. ERROR = 300,
  46. WARN = 400,
  47. NOTICE = 500,
  48. INFO = 600,
  49. DEBUG = 700,
  50. NOTSET = 800
  51. } PriorityLevel;
  52. #include <iostream>
  53. extern int LogLevel;
  54. #define LOG(__level) if (__level<=LogLevel) std::cout << "\n[" << #__level << "] " << __FILE__ << ":" << __LINE__ << "\n\t"
  55. //#define LOG(__level) if (__level<=LogLevel) std::cout << "\n[" << #__level << "] " << __FILE__ << ":" << __LINE__ << "\n\t"
  56. //#define LOG(__level) if (__level<=LogLevel) std::cout<< "\n"
  57. inline void initLogger(int verbose)
  58. {
  59. switch (verbose)
  60. {
  61. case 2: LogLevel=DEBUG; break;
  62. case 1: LogLevel=INFO; break;
  63. default: LogLevel=NOTICE; break;
  64. }
  65. std::cout << "log level:" << LogLevel << std::endl;
  66. #endif
  67. #endif
  68. }