123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- #include <thread>
- #include <time.h>
- #include <RestfulFunc.h>
- #include "json/json.h"
- #include "baseFun.h"
- #include <mutex>
- #include <condition_variable>
- 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<log_group_builder*> &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<std::mutex> 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::nanoseconds>(
- 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
|