linux版本中间件

Log.h 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #ifndef _DNC_LOG
  3. #define _DNC_LOG
  4. #include<log4cpp/Category.hh>
  5. #include<log4cpp/OstreamAppender.hh>
  6. #include<log4cpp/Priority.hh>
  7. #include<log4cpp/PatternLayout.hh>
  8. #include<log4cpp/FileAppender.hh>
  9. #include "log4cpp/PropertyConfigurator.hh"
  10. #include <string>
  11. #include <sstream>
  12. #include <boost/algorithm/string.hpp>
  13. #include <boost/format.hpp>
  14. using Format = boost::format;
  15. #define NONE "\e[0m"
  16. #define L_RED "\e[1;31m"
  17. #define YELLOW "\e[1;33m"
  18. #define L_BLUE "\e[1;34m"
  19. #define L_CYAN "\e[1;36m"
  20. #define LOG_DEBUG MYLog::instance()->getLogType()->debug
  21. #define LOG_INFO MYLog::instance()->getLogType()->info
  22. #define LOG_WARN MYLog::instance()->getLogType()->warn
  23. #define LOG_ERROR MYLog::instance()->getLogType()->error
  24. #define LOG_DEBUG_S(msg) \
  25. printf(L_BLUE); \
  26. LOG4CPP_DEBUG_S((*(MYLog::instance()->getLogType())))<<msg<< "\n" <<"("<<__FILE__<< ":" << __FUNCTION__ << ":" << __LINE__ << ")"; \
  27. printf(NONE);
  28. #define LOG_INFO_S(msg) \
  29. printf(L_CYAN); \
  30. LOG4CPP_INFO_S((*(MYLog::instance()->getLogType())))<<msg<< "\n" <<"("<<__FILE__<< ":" << __FUNCTION__ << ":" << __LINE__ << ")"; \
  31. printf(NONE);
  32. #define LOG_WARN_S(msg) \
  33. printf(YELLOW); \
  34. LOG4CPP_WARN_S((*(MYLog::instance()->getLogType())))<<msg<< "\n" <<"("<<__FILE__<< ":" << __FUNCTION__ << ":" << __LINE__ << ")"; \
  35. printf(NONE);
  36. #define LOG_ERROR_S(msg) \
  37. printf(L_RED); \
  38. LOG4CPP_ERROR_S((*(MYLog::instance()->getLogType()))) << msg << "\n" <<"("<<__FILE__<< ":" << __FUNCTION__ << ":" << __LINE__ << ")"; \
  39. printf(NONE);
  40. class MYLog {
  41. public:
  42. int Init(std::string initfilename) {
  43. try {
  44. log4cpp::PropertyConfigurator::configure(initfilename);
  45. }
  46. catch (log4cpp::ConfigureFailure&f) {
  47. std::cout << "Configure Problem " << f.what() << std::endl;//失败
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. static MYLog* instance() {
  53. return &_instance;
  54. }
  55. log4cpp::Category* getLogType()
  56. {
  57. return _category;
  58. }
  59. protected:
  60. MYLog() {};
  61. static MYLog _instance;
  62. static log4cpp::Category* _category;
  63. };
  64. #endif