#include #include #include #include "json/json.h" #include "baseFun.h" #include #include std::mutex m_mutex; std::condition_variable condition_variable; long LOG_GET_TIME() { return time(NULL); } RvcLogSdkManager& RvcLogSdkManager::getInstance() { static RvcLogSdkManager instance; return instance; } std::list &RvcLogSdkManager::getResendFrameList() { return m_resend_frame_list; } RvcLogSdkManager::RvcLogSdkManager() { m_resend_frame_list.clear(); m_testLogMode = false; } bool RvcLogSdkManager::wait_for_counter_greater_than_one_with_timeout() { std::unique_lock lock(m_mutex); // �ȴ��������㣨�� counter > 1�������ȴ� 100 ���� bool timed_out = !condition_variable.wait_for(lock, std::chrono::milliseconds(100), [&] { return m_logFlush_num > 0; }); m_logFlush_num = 0;//�Ѿ�ˢ���ˣ��������¼��� return true; } int RvcLogSdkManager::LOG_OS_TestLogPost(const char* url, const char* body) { HttpClientResponseResult result; HttpClientRequestConfig config(HttpRequestMethod::POST, url); std::string str((const char*)body); config.SetJsonBody(str.c_str()); RestfulClient client = RestfulClient::getInstance(); config.PreDo(); client.Do(&config, &result, NULL);//Test Logs return 0; } int RvcLogSdkManager::SendTestLog(const char* body) {//http://99.12.43.134:9000/upload_msg if (m_testLogMode == false) return -1; HttpClientRequestConfig sendErr(HttpRequestMethod::POST, ""); HttpClientResponseResult curResult; Json::Value rootReq; Json::FastWriter writer; rootReq["req"] = body; rootReq["ans"] = "Json_not_vaild"; std::string jsonReq = writer.write(rootReq); sendErr.SetJsonBody(jsonReq.c_str()); RestfulClient client = RestfulClient::getInstance(); sendErr.PreDo(); client.Do(&sendErr, &curResult, NULL);//Test Logs return 0; } int RvcLogSdkManager::SendTestLog_loki(const char* app, const char* env, const char* body) {//http://99.12.23.49:3100/loki/api/v1/push if (m_testLogMode == false) return -1; // 1. ����Loki���ݵ�JSON������ Json::Value root; Json::Value stream; Json::Value values(Json::arrayValue); // ���ñ�ǩ stream["app"] = app; stream["env"] = env; // ������־���ݺ�ʱ�������ǰʱ��������뼶�� Json::Value logEntry(Json::arrayValue); logEntry.append(std::to_string(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count())); logEntry.append(body); values.append(logEntry); // ��װ�������� Json::Value streams(Json::arrayValue); Json::Value streamObj; streamObj["stream"] = stream; streamObj["values"] = values; streams.append(streamObj); root["streams"] = streams; Json::FastWriter writer; std::string jsonReq = writer.write(root); auto send_loki = [](std::string jsonReq) { HttpClientRequestConfig sendErr(HttpRequestMethod::POST, ""); sendErr.setWithToken(false); HttpClientResponseResult curResult; sendErr.SetJsonBody(jsonReq.c_str()); RestfulClient client = RestfulClient::getInstance(); sendErr.PreDo(); client.Do(&sendErr, &curResult, NULL);//Test Logs }; std::thread(send_loki, jsonReq).detach();//��������̣߳��²�Ϊ�ⲿ������������ return 0; } #ifndef _WIN32 #ifndef CLOCK_MONOTONIC_RAW #define CLOCK_MONOTONIC_RAW 4 #endif uint32_t GetTickCount(void) { uint32_t ticks = 0; struct timespec ts; if (!clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) ticks = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000); return ticks; } #endif