MyLog.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include <Windows.h>
  3. #include <string>
  4. #include <memory>
  5. #include <time.h>
  6. #include <vector>
  7. #include <log4cpp\Category.hh>
  8. #include <log4cpp\Appender.hh>
  9. #include <log4cpp\FileAppender.hh>
  10. #include <log4cpp\OstreamAppender.hh>
  11. #include <log4cpp\Layout.hh>
  12. #include <log4cpp\SimpleLayout.hh>
  13. #include <log4cpp\BasicLayout.hh>
  14. #include <log4cpp\PatternLayout.hh>
  15. #include <log4cpp\Priority.hh>
  16. #include <log4cpp\RollingFileAppender.hh>
  17. #include <log4cpp\DailyRollingFileAppender.hh>
  18. #define MAX_FILE_SIZE 200*1024*1024
  19. #define MAX_BACKUP_COUNTS 10
  20. enum FILE_TYPE {
  21. RAW_S = 0,
  22. SYNC_S,
  23. DIST_S,
  24. DISTEX_S,
  25. DISTXX_S,
  26. JSON_S,
  27. JSON_R,
  28. SQL_S,
  29. SYS_S,
  30. KALMAN_S,
  31. ALGO_S,
  32. FILE_TYPE_TOTLA
  33. };
  34. const std::string my_log_file_name[] = {
  35. "raw_s",
  36. "sync_s"
  37. "dist_s",
  38. "distex_s",
  39. "distxx_s",
  40. "json_s",
  41. "json_r",
  42. "sql_s",
  43. "sys_s",
  44. "kalman_s",
  45. "algo_s"
  46. };
  47. const std::string log_file_pattern[] = {
  48. "%m%n", //消息,换行符
  49. "[%d] : %m%n"
  50. };
  51. class MyLog
  52. {
  53. public:
  54. MyLog(FILE_TYPE type);
  55. MyLog(FILE_TYPE type,bool status);
  56. ~MyLog();
  57. public:
  58. int WriteLog(const std::string msg);
  59. int WriteLog(const char* pMsg,...);
  60. int test();
  61. public:
  62. bool flag;
  63. private:
  64. bool status; //是否输出时间
  65. private:
  66. //以下函数考虑移出本类
  67. std::string GetExeDir(); //获得exe程序所在目录
  68. bool IsDirExist(std::string path); //检查目录是否存在
  69. bool CreateDir(std::string path); //创建目录
  70. std::string TCHAR2string(TCHAR* tch);
  71. std::string int2string(int value);
  72. public:
  73. int SetAppender(const char* filename);
  74. int SetAppender(std::string filename);
  75. int SetLayout(bool status);
  76. FILE_TYPE GetFileType(){return fileType;}
  77. bool GetStatus(){return status;}
  78. private:
  79. int InitLogConfig();
  80. long GetFileSize();
  81. std::string GetFileName();
  82. private:
  83. std::string strExeDir;
  84. private:
  85. FILE_TYPE fileType;
  86. std::string curFileName;
  87. int curFileCount;
  88. log4cpp::Appender* pAppender;
  89. log4cpp::PatternLayout* pLayout;
  90. log4cpp::Category* pCategory;
  91. };