baseFun.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <thread>
  2. #include <time.h>
  3. #include <RestfulFunc.h>
  4. #include "json/json.h"
  5. #include "baseFun.h"
  6. #include <mutex>
  7. #include <condition_variable>
  8. std::mutex m_mutex;
  9. std::condition_variable condition_variable;
  10. long LOG_GET_TIME()
  11. {
  12. return time(NULL);
  13. }
  14. RvcLogSdkManager& RvcLogSdkManager::getInstance() {
  15. static RvcLogSdkManager instance;
  16. return instance;
  17. }
  18. std::list<log_group_builder*> &RvcLogSdkManager::getResendFrameList() {
  19. return m_resend_frame_list;
  20. }
  21. RvcLogSdkManager::RvcLogSdkManager() {
  22. m_resend_frame_list.clear();
  23. m_testLogMode = false;
  24. }
  25. bool RvcLogSdkManager::wait_for_counter_greater_than_one_with_timeout() {
  26. std::unique_lock<std::mutex> lock(m_mutex);
  27. // �ȴ��������㣨�� counter > 1�������ȴ� 100 ����
  28. bool timed_out = !condition_variable.wait_for(lock, std::chrono::milliseconds(100), [&] { return m_logFlush_num > 0; });
  29. m_logFlush_num = 0;//�Ѿ�ˢ���ˣ��������¼���
  30. return true;
  31. }
  32. int RvcLogSdkManager::LOG_OS_TestLogPost(const char* url, const char* body)
  33. {
  34. HttpClientResponseResult result;
  35. HttpClientRequestConfig config(HttpRequestMethod::POST, url);
  36. std::string str((const char*)body);
  37. config.SetJsonBody(str.c_str());
  38. RestfulClient client = RestfulClient::getInstance();
  39. config.PreDo();
  40. client.Do(&config, &result, NULL);//Test Logs
  41. return 0;
  42. }
  43. int RvcLogSdkManager::SendTestLog(const char* body)
  44. {//http://99.12.43.134:9000/upload_msg
  45. if (m_testLogMode == false)
  46. return -1;
  47. HttpClientRequestConfig sendErr(HttpRequestMethod::POST, "");
  48. HttpClientResponseResult curResult;
  49. Json::Value rootReq;
  50. Json::FastWriter writer;
  51. rootReq["req"] = body;
  52. rootReq["ans"] = "Json_not_vaild";
  53. std::string jsonReq = writer.write(rootReq);
  54. sendErr.SetJsonBody(jsonReq.c_str());
  55. RestfulClient client = RestfulClient::getInstance();
  56. sendErr.PreDo();
  57. client.Do(&sendErr, &curResult, NULL);//Test Logs
  58. return 0;
  59. }
  60. int RvcLogSdkManager::SendTestLog_loki(const char* app, const char* env, const char* body)
  61. {//http://99.12.23.49:3100/loki/api/v1/push
  62. if (m_testLogMode == false)
  63. return -1;
  64. // 1. ����Loki���ݵ�JSON������
  65. Json::Value root;
  66. Json::Value stream;
  67. Json::Value values(Json::arrayValue);
  68. // ���ñ�ǩ
  69. stream["app"] = app;
  70. stream["env"] = env;
  71. // ������־���ݺ�ʱ�������ǰʱ��������뼶��
  72. Json::Value logEntry(Json::arrayValue);
  73. logEntry.append(std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(
  74. std::chrono::system_clock::now().time_since_epoch()).count()));
  75. logEntry.append(body);
  76. values.append(logEntry);
  77. // ��װ��������
  78. Json::Value streams(Json::arrayValue);
  79. Json::Value streamObj;
  80. streamObj["stream"] = stream;
  81. streamObj["values"] = values;
  82. streams.append(streamObj);
  83. root["streams"] = streams;
  84. Json::FastWriter writer;
  85. std::string jsonReq = writer.write(root);
  86. auto send_loki = [](std::string jsonReq)
  87. {
  88. HttpClientRequestConfig sendErr(HttpRequestMethod::POST, "");
  89. sendErr.setWithToken(false);
  90. HttpClientResponseResult curResult;
  91. sendErr.SetJsonBody(jsonReq.c_str());
  92. RestfulClient client = RestfulClient::getInstance();
  93. sendErr.PreDo();
  94. client.Do(&sendErr, &curResult, NULL);//Test Logs
  95. };
  96. std::thread(send_loki, jsonReq).detach();//��������̣߳��²�Ϊ�ⲿ������������
  97. return 0;
  98. }
  99. #ifndef _WIN32
  100. #ifndef CLOCK_MONOTONIC_RAW
  101. #define CLOCK_MONOTONIC_RAW 4
  102. #endif
  103. uint32_t GetTickCount(void)
  104. {
  105. uint32_t ticks = 0;
  106. struct timespec ts;
  107. if (!clock_gettime(CLOCK_MONOTONIC_RAW, &ts))
  108. ticks = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
  109. return ticks;
  110. }
  111. #endif