1 #include "StWsLogger.h"
17 std::string toString(T& arg)
19 std::ostringstream os;
25 std::string toString(T arg)
27 std::ostringstream os;
33 T fromString(
const std::string& s)
35 std::istringstream stream (s);
41 size_t curl_write_to_string(
void *ptr,
size_t size,
size_t count,
void *stream) {
42 ((std::string*)stream)->append((
char*)ptr, 0, size*count);
48 StWsLogger::StWsLogger()
54 mServiceUrl = getServiceUrl();
56 mTaskID = getTaskID();
59 StWsLogger::~StWsLogger()
64 void StWsLogger::logEvent(LEVEL lvl,
const picojson::object& evt, STAGE stg,
bool nobacklog) {
65 if (mIsDisabled) {
return; }
67 picojson::object event;
80 int ret = makeHttpPostCurl(
81 ( mServiceUrl + std::string(
"?action=event_append&task_id=") + mTaskID + std::string(
"&job_id=") + mJobID ).c_str(),
89 mEventBackLog.push_back(v.serialize());
96 void StWsLogger::logJobUpdate(
const picojson::object& evt,
bool nobacklog) {
97 if (mIsDisabled) {
return; }
99 picojson::object event;
104 std::string response;
106 int ret = makeHttpPostCurl(
107 ( mServiceUrl + std::string(
"?action=job_add_info&task_id=") + mTaskID + std::string(
"&job_id=") + mJobID ).c_str(),
114 mJobUpdateBackLog.push_back(v.serialize());
120 double StWsLogger::getTimeMs()
const {
122 gettimeofday(&tim, NULL);
123 double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
127 std::string StWsLogger::getUserName()
const {
128 register struct passwd *pw;
133 return (std::string(pw->pw_name));
135 return std::string(
"UNKNOWN");
138 std::string StWsLogger::getHostName()
const {
140 gethostname(buf,
sizeof buf);
141 return std::string(buf);
144 void StWsLogger::setServiceUrl(
const std::string& url) {
146 if (!mServiceUrl.empty() && !mJobID.empty() && !mTaskID.empty()) { mIsDisabled =
false; }
150 void StWsLogger::setJobID(
const std::string&
id) {
152 if (!mServiceUrl.empty() && !mJobID.empty() && !mTaskID.empty()) { mIsDisabled =
false; }
156 void StWsLogger::setTaskID(
const std::string&
id) {
158 if (!mServiceUrl.empty() && !mJobID.empty() && !mTaskID.empty()) { mIsDisabled =
false; }
162 std::string StWsLogger::getServiceUrl() {
163 if (!mServiceUrl.empty()) {
return mServiceUrl; }
166 surl = getenv(
"WS_LOG_URL");
168 return std::string(surl);
172 return std::string(
"");
175 std::string StWsLogger::getJobID() {
176 if (!mJobID.empty()) {
return mJobID; }
179 jid = getenv(
"WS_JOB_ID");
181 return std::string(jid);
185 return std::string(
"");
188 std::string StWsLogger::getTaskID() {
189 if (!mTaskID.empty()) {
return mTaskID; }
192 tid = getenv(
"WS_TASK_ID");
194 return std::string(tid);
198 return std::string(
"");
201 int StWsLogger::makeHttpPostCurl(
const char* url,
const std::string& json_data, std::string& response) {
207 curl = curl_easy_init();
209 curl_easy_setopt(curl, CURLOPT_URL, url);
211 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
212 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
213 curl_easy_setopt(curl, CURLOPT_POST, 1L);
216 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_to_string);
217 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
219 struct curl_slist *chunk = NULL;
220 chunk = curl_slist_append(chunk,
"Expect:");
221 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
223 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data.c_str());
224 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, json_data.length());
226 res = curl_easy_perform(curl);
227 if(res != CURLE_OK) {
228 std::cerr <<
"HTTPS POST request failed: " << curl_easy_strerror(res) << std::endl;
231 curl_slist_free_all(chunk);
232 curl_easy_cleanup(curl);
236 void StWsLogger::checkBackLogs() {
237 if (!mEventBackLog.empty()) {
238 std::string response;
239 for (std::vector<std::string>::iterator it = mEventBackLog.begin(); it != mEventBackLog.end(); ++it) {
240 int ret = makeHttpPostCurl(
241 ( mServiceUrl + std::string(
"?action=event_append&task_id=") + mTaskID + std::string(
"&job_id=") + mJobID ).c_str(),
245 if (ret != 0)
return;
246 it = mEventBackLog.erase(it);
250 if (!mJobUpdateBackLog.empty()) {
251 std::string response;
252 for (std::vector<std::string>::iterator it = mJobUpdateBackLog.begin(); it != mJobUpdateBackLog.end(); ++it) {
253 int ret = makeHttpPostCurl(
254 ( mServiceUrl + std::string(
"?action=job_add_info&task_id=") + mTaskID + std::string(
"&job_id=") + mJobID ).c_str(),
258 if (ret != 0)
return;
259 it = mJobUpdateBackLog.erase(it);