MyLog.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. //日志还需要改进的地方包括:1.线程输出;2.日志内容从缓冲区申请内存放入消息,并还给缓冲区
  3. #include <Windows.h>
  4. #include <string>
  5. #include <memory>
  6. #include <time.h>
  7. #include <vector>
  8. #include <log4cpp\Category.hh>
  9. #include <log4cpp\Appender.hh>
  10. #include <log4cpp\FileAppender.hh>
  11. #include <log4cpp\OstreamAppender.hh>
  12. #include <log4cpp\Layout.hh>
  13. #include <log4cpp\SimpleLayout.hh>
  14. #include <log4cpp\BasicLayout.hh>
  15. #include <log4cpp\PatternLayout.hh>
  16. #include <log4cpp\Priority.hh>
  17. #include <log4cpp\RollingFileAppender.hh>
  18. #include <log4cpp\DailyRollingFileAppender.hh>
  19. #ifdef _WIN32
  20. #include <direct.h>
  21. #include <io.h>
  22. #elif _LINUX
  23. #include <stdarg.h>
  24. #include <sys/stat.h>
  25. #endif
  26. #ifdef _WIN32
  27. #define ACCESS _access
  28. #define MKDIR(a) _mkdir((a))
  29. #elif _LINUX
  30. #define ACCESS access
  31. #define MKDIR(a) mkdir((a),0755)
  32. #endif
  33. #define MAX_FILE_SIZE 200*1024*1024
  34. #define MAX_BACKUP_COUNTS 20
  35. enum FILE_TYPE {
  36. RAW_S = 0,
  37. SYNC_S,
  38. PARSE_S,
  39. DIST_S,
  40. DISTX_S,
  41. DISTEX_S,
  42. DISTXX_S,
  43. DIST_TOF_S,
  44. LOCATE_TOF_S,
  45. JSON_S,
  46. JSON_R,
  47. SQL_S,
  48. SYS_S,
  49. KALMAN_S,
  50. ALGO_S,
  51. FILE_TYPE_TOTAL
  52. };
  53. const std::string my_log_file_name[] = {
  54. "raw_s",
  55. "sync_s",
  56. "parse_s",
  57. "dist_s",
  58. "distx_s",
  59. "distex_s",
  60. "distxx_s",
  61. "dist_tof_s",
  62. "locate_tof_s",
  63. "json_s",
  64. "json_r",
  65. "sql_s",
  66. "sys_s",
  67. "kalman_s",
  68. "algo_s"
  69. };
  70. const std::string log_file_pattern[] = {
  71. "%m%n", //消息,换行符
  72. "[%d] : %m%n"
  73. };
  74. class MyLog
  75. {
  76. public:
  77. MyLog(FILE_TYPE type);
  78. MyLog(FILE_TYPE type,bool status);
  79. ~MyLog();
  80. public:
  81. int WriteLog(const std::string msg);
  82. int WriteLog(const char* pMsg,...);
  83. int test();
  84. public:
  85. bool flag;
  86. private:
  87. bool status; //是否输出时间
  88. private:
  89. //以下函数考虑移出本类
  90. std::string GetExeDir(); //获得exe程序所在目录
  91. bool IsDirExist(std::string path); //检查目录是否存在
  92. bool CreateDir(const char* path); //创建目录
  93. std::string TCHAR2string(TCHAR* tch);
  94. std::string int2string(int value);
  95. std::string getCurTime();
  96. public:
  97. int SetAppender(const char* filename);
  98. int SetAppender(std::string filename);
  99. int SetLayout(bool status);
  100. FILE_TYPE GetFileType(){return fileType;}
  101. bool GetStatus(){return status;}
  102. private:
  103. int InitLogConfig();
  104. long GetFileSize();
  105. std::string GetFileName();
  106. private:
  107. std::string strExeDir;
  108. private:
  109. FILE_TYPE fileType;
  110. std::string curFileName;
  111. int curFileCount;
  112. std::string m_Lasttime;
  113. std::string m_Curtime;
  114. log4cpp::Appender* pAppender;
  115. log4cpp::PatternLayout* pLayout;
  116. log4cpp::Category* pCategory;
  117. };