log.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #pragma once
  2. #include <glog/logging.h>
  3. #include "tool/pathcreator.hpp"
  4. namespace ZX{
  5. static void shut_down_logging(const char *data, size_t size) {
  6. time_t tt;
  7. time(&tt);
  8. tt = tt + 8 * 3600; // transform the time zone
  9. tm *t = gmtime(&tt);
  10. char buf[255] = {0};
  11. sprintf(buf, "./%d%02d%02d-%02d%02d%02d-dump.txt",
  12. t->tm_year + 1900,
  13. t->tm_mon + 1,
  14. t->tm_mday,
  15. t->tm_hour,
  16. t->tm_min,
  17. t->tm_sec);
  18. FILE *tp_file = fopen(buf, "w");
  19. fprintf(tp_file, data, strlen(data));
  20. fclose(tp_file);
  21. }
  22. // 例子: ZX::InitGlog(PROJECT_NAME, ETC_PATH PROJECT_NAME "/LogInfo/");
  23. static bool InitGlog(const char * project_name, const char *logPath) {
  24. PathCreator pc;
  25. if (!pc.Mkdir(logPath)) {return false;}
  26. google::InitGoogleLogging(project_name);
  27. google::SetStderrLogging(google::INFO);
  28. google::SetLogDestination(google::INFO, logPath);
  29. google::SetLogFilenameExtension(".log");
  30. google::InstallFailureSignalHandler();
  31. google::InstallFailureWriter(&ZX::shut_down_logging);
  32. FLAGS_colorlogtostderr = true; // Set log color
  33. FLAGS_logbufsecs = 0; // Set log output speed(s)
  34. FLAGS_max_log_size = 32; // Set max log file size(MB)
  35. FLAGS_stop_logging_if_full_disk = true;
  36. return true;
  37. }
  38. }