logutils.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "logutils.h"
  2. #include "util/print.h"
  3. #include "util/time.h"
  4. #include "util/codec.h"
  5. #include "util/file.h"
  6. #include "core/define.h"
  7. #include "core/config.h"
  8. #ifdef _WIN32
  9. #include <Windows.h>
  10. #endif
  11. #define DEBUG_INFO 0
  12. void LogUtils::print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) {
  13. std::string __now_time = time::now_time("%m-%d %H:%M:%S");
  14. printf("[%s] ", __now_time.c_str());
  15. #if DEBUG_INFO == 1
  16. printf("%s", ylib::file::filename(filepath).c_str());
  17. printf("%s ", std::string(":" + func + ":" + std::to_string(line) + "\t").c_str());
  18. #endif
  19. ylib::print(" "+type +" ", (ylib::ConsoleTextColor)color);
  20. #ifdef _WIN32
  21. ylib::println(codec::to_gbk(msg), (ylib::ConsoleTextColor)color);
  22. #else
  23. ylib::println(msg, (ylib::ConsoleTextColor)color);
  24. #endif
  25. if (sConfig->log.enable)
  26. {
  27. std::string logcontent = __now_time + " " + type + " " + msg
  28. #ifdef _WIN32
  29. + "\r\n";
  30. #else
  31. + "\n";
  32. #endif
  33. std::string new_time = time::now_time(sConfig->log.name);
  34. if (new_time == "")
  35. {
  36. std::cout << "error: The log file name is incorrect: " << sConfig->log.name << std::endl;
  37. return;
  38. }
  39. if (m_current_name != new_time)
  40. {
  41. ylib::file::create_dir(sConfig->log.dir, true);
  42. std::string newfilepath = sConfig->log.dir + "/" + new_time;
  43. m_file.close();
  44. if (m_file.open(newfilepath) == false)
  45. {
  46. std::cout << "error: open log file failed, filepath: " << newfilepath << std::endl;
  47. return;
  48. }
  49. }
  50. m_file.appead(logcontent);
  51. }
  52. }
  53. LogUtils::LogUtils()
  54. {
  55. }
  56. LogUtils::~LogUtils()
  57. {
  58. }
  59. void LogUtils::success(const std::string& msg, const std::string& filepath, const std::string& func, int line)
  60. {
  61. //log_error();
  62. this->print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN
  63. #ifdef _WIN32
  64. |FOREGROUND_INTENSITY
  65. #endif
  66. ,false);
  67. }
  68. void LogUtils::info(const std::string& msg, const std::string& filepath, const std::string& func, int line)
  69. {
  70. this->print("[INFO ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
  71. }
  72. void LogUtils::error(const std::string& msg, const std::string& filepath, const std::string& func, int line)
  73. {
  74. //#ifdef _DEBUG
  75. this->print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED
  76. #ifdef _WIN32
  77. | FOREGROUND_INTENSITY
  78. #endif
  79. , true);
  80. //#else
  81. //#endif
  82. }
  83. void LogUtils::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line)
  84. {
  85. this->print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW
  86. #ifdef _WIN32
  87. | FOREGROUND_INTENSITY
  88. #endif
  89. , false);
  90. }
  91. void LogUtils::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line)
  92. {
  93. this->print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE
  94. #ifdef _WIN32
  95. | FOREGROUND_INTENSITY
  96. #endif
  97. , false);
  98. }