Logger.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. # ifndef LOG_SYS_H
  3. # define LOG_SYS_H
  4. # include <iostream>
  5. # include <fstream>
  6. # include <string>
  7. # include <time.h>
  8. # include <stdio.h>
  9. # include <stdlib.h>
  10. #include <windows.h>
  11. using std::cout;
  12. using std::string;
  13. using std::endl;
  14. using std::to_string;
  15. using std::ios;
  16. class Logger : public SSingleton<Logger> {
  17. public:
  18. enum log_level { debug, info, warning, error };// 日志等级
  19. enum log_target { file, terminal, file_and_terminal };// 日志输出目标
  20. private:
  21. string currTime() {
  22. // 获取当前时间,并规范表示
  23. char tmp[64];
  24. time_t ptime;
  25. time(&ptime); // time_t time (time_t* timer);
  26. strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&ptime));
  27. return tmp;
  28. }
  29. std::ofstream outfile; // 将日志输出到文件的流对象
  30. log_target target; // 日志输出目标
  31. string path; // 日志文件路径
  32. log_level level; // 日志等级
  33. void output(string text, log_level act_level); // 输出行为
  34. public:
  35. Logger(); // 默认构造函数
  36. void Init(log_target target, log_level level, string path = "");
  37. void DEBUG(string text);
  38. void INFO(string text);
  39. void WARNING(string text);
  40. void LERROR(string text);
  41. };
  42. # endif