StartUpBase.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "precompile.h"
  2. #include "StartUpBase.h"
  3. #include <json/json.h>
  4. #include <winpr/sysinfo.h>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include "log_define.h"
  9. #include <SpBase.h>
  10. class StartupLog {
  11. public:
  12. struct Step {
  13. std::string timestamp;
  14. std::string event;
  15. std::string status;
  16. int from;
  17. int to;
  18. int result;
  19. Step(const std::string& ts, const std::string& ev, const std::string t_status, int t_from, int t_to, int t_result)
  20. : timestamp(ts), event(ev), status(t_status), from(t_from), to(t_to), result(t_result) {}
  21. };
  22. StartupLog();
  23. StartupLog(const std::string& module_entity, int req_id = -1);
  24. ~StartupLog();
  25. void initialize(int req_id, const std::string& module_entity);
  26. void addStep(const Step& step);
  27. std::string toJSON() const;
  28. private:
  29. int request_id;
  30. std::string module_entity_;
  31. std::vector<Step> steps_;
  32. };
  33. class StartupLogManager {
  34. public:
  35. static StartupLogManager& getInstance();
  36. //StartupLogManager::getInstance().addLog(entityName, entity_id);
  37. void addLog(const std::string& module_entity, int idx); // 新增 addLog 方法
  38. bool checkLogExist(int idx);
  39. StartupLog& getLog(int idx);
  40. /*
  41. StartupLogManager::getInstance().addStep(entityName, { GetCurrentTimeStr(), "handle_req" });
  42. */
  43. void addStep(int idx, const StartupLog::Step& step);
  44. void removeLog(int idx);
  45. private:
  46. StartupLogManager();
  47. ~StartupLogManager();
  48. std::map<int, StartupLog> logs_;
  49. };
  50. std::string GetCurrentTimeStr();
  51. SPBASE_API int addStartupLog(const char* module_entity, int idx)
  52. {
  53. StartupLogManager::getInstance().addLog(module_entity, idx);
  54. return 0;
  55. }
  56. SPBASE_API int addStartupStep(int idx, const char* event, const char* status, int from, int to, int result)
  57. {
  58. StartupLogManager::getInstance().addStep(idx, StartupLog::Step(GetCurrentTimeStr(), std::string(event), std::string(status), from, to, result));
  59. return 0;
  60. }
  61. std::string GetCurrentTimeStr()
  62. {
  63. char unitedNowTime[30] = "";
  64. SYSTEMTIME st;
  65. FILETIME utc_ft, local_ft, ft;
  66. #ifdef _WIN32
  67. GetSystemTime(&st);
  68. SystemTimeToFileTime(&st, &ft);
  69. utc_ft.dwLowDateTime = (DWORD)ft.dwLowDateTime;
  70. utc_ft.dwHighDateTime = (DWORD)ft.dwHighDateTime;
  71. FileTimeToLocalFileTime(&utc_ft, &local_ft);
  72. FileTimeToSystemTime(&local_ft, &st);
  73. #else
  74. GetLocalTime(&st);
  75. #endif // _WIN32
  76. sprintf(unitedNowTime, "%02d:%02d:%02d.%03d",
  77. st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  78. return std::string(unitedNowTime);
  79. }
  80. StartupLog::StartupLog() : request_id(0) {}
  81. StartupLog::~StartupLog() {}
  82. void StartupLog::initialize(int req_id, const std::string& module_entity) {
  83. request_id = req_id;
  84. module_entity_ = module_entity;
  85. }
  86. StartupLog::StartupLog(const std::string& module_entity, int req_id)
  87. {
  88. module_entity_ = module_entity;
  89. request_id = req_id;
  90. }
  91. void StartupLog::addStep(const Step& step) {
  92. steps_.push_back(step);
  93. }
  94. std::string StartupLog::toJSON() const {
  95. Json::Value root;
  96. root["event"] = "entity_startup";
  97. root["request_id"] = request_id;
  98. root["module_entity"] = module_entity_;
  99. Json::Value steps_array(Json::arrayValue);
  100. for (const auto& step : steps_) {
  101. Json::Value step_obj;
  102. step_obj["timestamp"] = step.timestamp;
  103. step_obj["event"] = step.event;
  104. if (!step.status.empty()) {
  105. step_obj["status"] = step.status;
  106. }
  107. if (step.from != 0 && step.to != 0) {
  108. step_obj["from"] = step.from;
  109. step_obj["to"] = step.to;
  110. }
  111. if (step.result != 0) {
  112. step_obj["result"] = step.result;
  113. }
  114. steps_array.append(step_obj);
  115. }
  116. root["steps"] = steps_array;
  117. Json::FastWriter writer;
  118. std::string jsonReq = writer.write(root);
  119. return jsonReq;
  120. }
  121. StartupLogManager::StartupLogManager() {}
  122. StartupLogManager::~StartupLogManager() {}
  123. void StartupLogManager::addLog(const std::string& module_entity, int idx) {
  124. if (logs_.find(idx) == logs_.end()) {
  125. logs_[idx] = StartupLog(module_entity, idx); // 直接拷贝 log 对象
  126. } // 如果已存在,则忽略
  127. }
  128. StartupLog& StartupLogManager::getLog(int idx) {
  129. if (logs_.find(idx) == logs_.end()) {
  130. logs_[idx] = StartupLog("", idx); // 仍然保留隐式创建的逻辑,方便使用
  131. }
  132. return logs_[idx];
  133. }
  134. bool StartupLogManager::checkLogExist(int idx)
  135. {
  136. if (logs_.find(idx) == logs_.end())
  137. return false;
  138. else
  139. return true;
  140. }
  141. void StartupLogManager::addStep(int idx, const StartupLog::Step& step) {
  142. if (checkLogExist(idx))
  143. {
  144. StartupLog &current = getLog(idx);
  145. current.addStep(step);
  146. if (step.result != 0 || step.status == "startup_end")
  147. {//print the logs and remove obj
  148. std::string resultMsg = current.toJSON();
  149. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)(resultMsg.c_str());
  150. removeLog(idx);
  151. }
  152. }
  153. }
  154. void StartupLogManager::removeLog(int idx)
  155. {
  156. if (logs_.find(idx) == logs_.end())
  157. logs_.erase(idx);
  158. }
  159. StartupLogManager& StartupLogManager::getInstance() {
  160. static StartupLogManager instance;
  161. return instance;
  162. }