MyLog.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <mutex>
  9. #include <log4cpp\Category.hh>
  10. #include <log4cpp\Appender.hh>
  11. #include <log4cpp\FileAppender.hh>
  12. #include <log4cpp\OstreamAppender.hh>
  13. #include <log4cpp\Layout.hh>
  14. #include <log4cpp\SimpleLayout.hh>
  15. #include <log4cpp\BasicLayout.hh>
  16. #include <log4cpp\PatternLayout.hh>
  17. #include <log4cpp\Priority.hh>
  18. #include <log4cpp\RollingFileAppender.hh>
  19. #include <log4cpp\DailyRollingFileAppender.hh>
  20. #ifdef _WIN32
  21. #include <direct.h>
  22. #include <io.h>
  23. #elif _LINUX
  24. #include <stdarg.h>
  25. #include <sys/stat.h>
  26. #endif
  27. #ifdef _WIN32
  28. #define ACCESS _access
  29. #define MKDIR(a) _mkdir((a))
  30. #elif _LINUX
  31. #define ACCESS access
  32. #define MKDIR(a) mkdir((a),0755)
  33. #endif
  34. #define MAX_FILE_SIZE 200*1024*1024
  35. #define MAX_BACKUP_COUNTS 20
  36. enum FILE_TYPE {
  37. RAW_S = 0,
  38. SYNC_S,
  39. PARSE_S,
  40. PARSE_CARD_S,
  41. PARSE_LIGHT_S,
  42. DIST_S,
  43. DISTEX_S,
  44. DISTXX_S,
  45. DIST_TOF_S,
  46. LOCATE_TOF_S,
  47. JSON_S,
  48. JSON_R,
  49. SQL_S,
  50. SYS_S,
  51. KALMAN_S,
  52. ALGO_S,
  53. VIBRATE_S,
  54. STARTUP_RATIO,
  55. RAW_LOCATION_DATA,
  56. POSITION_ORIGIN,
  57. POSITION_OPTIMIZED,
  58. MOVING_READER_PARSE_DATA,
  59. FILE_TYPE_TOTAL
  60. };
  61. const std::string my_log_file_name[] = {
  62. "raw_s",
  63. "sync_s",
  64. "parse_s",
  65. "parse_card_s",
  66. "parse_light_s",
  67. "dist_s",
  68. "distex_s",
  69. "distxx_s",
  70. "dist_tof_s",
  71. "locate_tof_s",
  72. "json_s",
  73. "json_r",
  74. "sql_s",
  75. "sys_s",
  76. "kalman_s",
  77. "algo_s",
  78. "vibrate_s",
  79. "startup_ratio",
  80. "position_raw",
  81. "position_origin",
  82. "position_ajusted",
  83. "moving_reader_parse_s"
  84. };
  85. const std::string log_file_pattern[] = {
  86. "%m%n", //消息,换行符
  87. "[%d] : %m%n"
  88. };
  89. class MyLog
  90. {
  91. public:
  92. MyLog(FILE_TYPE type);
  93. MyLog(FILE_TYPE type,bool status);
  94. ~MyLog();
  95. public:
  96. int WriteLog(const std::string msg);
  97. int WriteLog(const char* pMsg,...);
  98. int test();
  99. public:
  100. bool flag;
  101. private:
  102. bool status; //是否输出时间
  103. private:
  104. //以下函数考虑移出本类
  105. std::string GetExeDir(); //获得exe程序所在目录
  106. std::string TCHAR2string(TCHAR* tch);
  107. std::string int2string(int value);
  108. std::string getCurTime();
  109. public:
  110. int SetAppender(const char* filename);
  111. int SetAppender(std::string filename);
  112. int SetLayout(bool status);
  113. static bool IsDirExist(std::string path); //检查目录是否存在
  114. static bool CreateDir(const char* path); //创建目录
  115. FILE_TYPE GetFileType(){return fileType;}
  116. bool GetStatus(){return status;}
  117. private:
  118. int InitLogConfig();
  119. long GetFileSize();
  120. std::string GetFileName();
  121. private:
  122. std::string strExeDir;
  123. private:
  124. FILE_TYPE fileType;
  125. std::string curFileName;
  126. int curFileCount;
  127. std::string m_Lasttime;
  128. std::string m_Curtime;
  129. log4cpp::Appender* pAppender;
  130. log4cpp::PatternLayout* pLayout;
  131. log4cpp::Category* pCategory;
  132. };