baseFun.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <time.h>
  2. #include <RestfulFunc.h>
  3. #include "json/json.h"
  4. #include "baseFun.h"
  5. #include <mutex>
  6. #include <condition_variable>
  7. std::mutex m_mutex;
  8. std::condition_variable condition_variable;
  9. long LOG_GET_TIME()
  10. {
  11. return time(NULL);
  12. }
  13. RvcLogSdkManager& RvcLogSdkManager::getInstance() {
  14. static RvcLogSdkManager instance;
  15. return instance;
  16. }
  17. std::list<log_group_builder*> &RvcLogSdkManager::getResendFrameList() {
  18. return m_resend_frame_list;
  19. }
  20. RvcLogSdkManager::RvcLogSdkManager() {
  21. m_resend_frame_list.clear();
  22. m_testLogMode = false;
  23. }
  24. bool RvcLogSdkManager::wait_for_counter_greater_than_one_with_timeout() {
  25. std::unique_lock<std::mutex> lock(m_mutex);
  26. // 等待条件满足(即 counter > 1),最多等待 100 毫秒
  27. bool timed_out = !condition_variable.wait_for(lock, std::chrono::milliseconds(100), [&] { return m_logFlush_num > 0; });
  28. m_logFlush_num = 0;//已经刷新了,后续重新计数
  29. return true;
  30. }
  31. int RvcLogSdkManager::LOG_OS_TestLogPost(const char* url, const char* body)
  32. {
  33. HttpClientResponseResult result;
  34. HttpClientRequestConfig config(HttpRequestMethod::POST, url);
  35. std::string str((const char*)body);
  36. config.SetJsonBody(str.c_str());
  37. RestfulClient client = RestfulClient::getInstance();
  38. config.PreDo();
  39. client.Do(&config, &result, NULL);//Test Logs
  40. return 0;
  41. }
  42. int RvcLogSdkManager::SendTestLog(const char* body)
  43. {
  44. if (m_testLogMode == false)
  45. return -1;
  46. HttpClientRequestConfig sendErr(HttpRequestMethod::POST, "http://99.12.43.134:9000/upload_msg");
  47. HttpClientResponseResult curResult;
  48. Json::Value rootReq;
  49. Json::FastWriter writer;
  50. rootReq["req"] = body;
  51. rootReq["ans"] = "Json_not_vaild";
  52. std::string jsonReq = writer.write(rootReq);
  53. sendErr.SetJsonBody(jsonReq.c_str());
  54. RestfulClient client = RestfulClient::getInstance();
  55. sendErr.PreDo();
  56. client.Do(&sendErr, &curResult, NULL);//Test Logs
  57. return 0;
  58. }
  59. #ifndef _WIN32
  60. #ifndef CLOCK_MONOTONIC_RAW
  61. #define CLOCK_MONOTONIC_RAW 4
  62. #endif
  63. uint32_t GetTickCount(void)
  64. {
  65. uint32_t ticks = 0;
  66. struct timespec ts;
  67. if (!clock_gettime(CLOCK_MONOTONIC_RAW, &ts))
  68. ticks = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
  69. return ticks;
  70. }
  71. #endif