Skip to content

Commit 10085e0

Browse files
Bohdan Kurylovychborzun
authored andcommitted
Improved logging for NetworkCurl
Improved logging in NetworkCurl implementation by adding request ids, this pointer and url when do logging. Relates to: OLPEDGE-1051 Signed-off-by: Bohdan Kurylovych <[email protected]>
1 parent 374f69a commit 10085e0

File tree

1 file changed

+78
-51
lines changed

1 file changed

+78
-51
lines changed

olp-cpp-sdk-core/src/http/curl/NetworkCurl.cpp

Lines changed: 78 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,15 @@ static curl_code SslctxFunction(CURL* curl, void* sslctx, void*) {
222222

223223
NetworkCurl::NetworkCurl(size_t max_requests_count)
224224
: handles_(max_requests_count),
225-
static_handle_count_(std::max(1lu, max_requests_count / 4)) {}
225+
static_handle_count_(std::max(1lu, max_requests_count / 4)) {
226+
OLP_SDK_LOG_TRACE(kLogTag, "Created NetworkCurl with address="
227+
<< this
228+
<< ", handles_count=" << max_requests_count);
229+
}
226230

227231
NetworkCurl::~NetworkCurl() {
232+
OLP_SDK_LOG_TRACE(kLogTag, "Destroyed NetworkCurl object, this=" << this);
233+
228234
if (state_ == WorkerState::STARTED) {
229235
Deinitialize();
230236
}
@@ -236,18 +242,18 @@ NetworkCurl::~NetworkCurl() {
236242
bool NetworkCurl::Initialize() {
237243
std::lock_guard<std::mutex> init_lock(init_mutex_);
238244
if (state_ != WorkerState::STOPPED) {
239-
OLP_SDK_LOG_DEBUG(kLogTag, "Already initialized");
245+
OLP_SDK_LOG_DEBUG(kLogTag, "Already initialized, this=" << this);
240246
return true;
241247
}
242248

243249
#ifdef NETWORK_HAS_PIPE2
244250
if (pipe2(pipe_, O_NONBLOCK)) {
245-
OLP_SDK_LOG_ERROR(kLogTag, "pipe2 failed");
251+
OLP_SDK_LOG_ERROR(kLogTag, "pipe2 failed, this=" << this);
246252
return false;
247253
}
248254
#elif defined NETWORK_HAS_PIPE
249255
if (pipe(pipe_)) {
250-
OLP_SDK_LOG_ERROR(kLogTag, "pipe failed");
256+
OLP_SDK_LOG_ERROR(kLogTag, "pipe failed, this=" << this);
251257
return false;
252258
}
253259
// Set pipes non blocking
@@ -256,14 +262,14 @@ bool NetworkCurl::Initialize() {
256262
flags = 0;
257263
}
258264
if (fcntl(pipe_[0], F_SETFL, flags | O_NONBLOCK)) {
259-
OLP_SDK_LOG_ERROR(kLogTag, "fcntl for pipe failed");
265+
OLP_SDK_LOG_ERROR(kLogTag, "fcntl for pipe failed, this=" << this);
260266
return false;
261267
}
262268
if (-1 == (flags = fcntl(pipe_[1], F_GETFL, 0))) {
263269
flags = 0;
264270
}
265271
if (fcntl(pipe_[1], F_SETFL, flags | O_NONBLOCK)) {
266-
OLP_SDK_LOG_ERROR(kLogTag, "fcntl for pipe failed");
272+
OLP_SDK_LOG_ERROR(kLogTag, "fcntl for pipe failed, this=" << this);
267273
return false;
268274
}
269275
#endif
@@ -280,7 +286,7 @@ bool NetworkCurl::Initialize() {
280286
// cURL setup
281287
curl_ = curl_multi_init();
282288
if (!curl_) {
283-
OLP_SDK_LOG_ERROR(kLogTag, "curl_multi_init failed");
289+
OLP_SDK_LOG_ERROR(kLogTag, "curl_multi_init failed, this=" << this);
284290
return false;
285291
}
286292

@@ -310,9 +316,12 @@ bool NetworkCurl::Initialize() {
310316
void NetworkCurl::Deinitialize() {
311317
// Stop worker thread
312318
if (state_ != WorkerState::STARTED) {
313-
OLP_SDK_LOG_DEBUG(kLogTag, "Already deinitialized");
319+
OLP_SDK_LOG_DEBUG(kLogTag, "Already deinitialized, this=" << this);
314320
return;
315321
}
322+
323+
OLP_SDK_LOG_TRACE(kLogTag, "Deinitialize NetworkCurl, this=" << this);
324+
316325
{
317326
std::lock_guard<std::mutex> lock(event_mutex_);
318327
state_ = WorkerState::STOPPING;
@@ -331,7 +340,8 @@ void NetworkCurl::Teardown() {
331340
#if (defined NETWORK_HAS_PIPE) || (defined NETWORK_HAS_PIPE2)
332341
char tmp = 1;
333342
if (write(pipe_[1], &tmp, 1) < 0) {
334-
OLP_SDK_LOG_INFO(kLogTag, "deinitialize - failed to write pipe " << errno);
343+
OLP_SDK_LOG_INFO(kLogTag, "Deinitialize, failed to write pipe, err="
344+
<< errno << ", this=" << this);
335345
}
336346
#endif
337347

@@ -410,6 +420,8 @@ SendOutcome NetworkCurl::Send(NetworkRequest request,
410420
Network::DataCallback data_callback) {
411421
if (!Initialized()) {
412422
if (!Initialize()) {
423+
OLP_SDK_LOG_ERROR(kLogTag, "Send failed - network is uninitialized, url="
424+
<< request.GetUrl());
413425
return SendOutcome(ErrorCode::OFFLINE_ERROR);
414426
}
415427
}
@@ -445,9 +457,9 @@ ErrorCode NetworkCurl::SendImplementation(
445457
const std::shared_ptr<std::ostream>& payload,
446458
Network::HeaderCallback header_callback,
447459
Network::DataCallback data_callback, Network::Callback callback) {
448-
OLP_SDK_LOG_TRACE(kLogTag, "send with id = " << id);
449-
450460
if (!IsStarted()) {
461+
OLP_SDK_LOG_ERROR(
462+
kLogTag, "Send failed - network is offline, url=" << request.GetUrl());
451463
return ErrorCode::IO_ERROR;
452464
}
453465

@@ -456,10 +468,12 @@ ErrorCode NetworkCurl::SendImplementation(
456468
RequestHandle* handle = GetHandle(id, callback, header_callback,
457469
data_callback, payload, request.GetBody());
458470
if (!handle) {
459-
OLP_SDK_LOG_WARNING(kLogTag, "curl handle is nullptr");
460471
return ErrorCode::NETWORK_OVERLOAD_ERROR;
461472
}
462473

474+
OLP_SDK_LOG_DEBUG(kLogTag, "Send request with url=" << request.GetUrl()
475+
<< ", id=" << id);
476+
463477
handle->transfer_timeout = config.GetTransferTimeout();
464478
handle->max_retries = config.GetRetries();
465479
handle->ignore_offset = false; // request.IgnoreOffset();
@@ -557,9 +571,10 @@ ErrorCode NetworkCurl::SendImplementation(
557571
CURLcode error = curl_easy_setopt(handle->handle, CURLOPT_CAINFO,
558572
curl_ca_bundle.c_str());
559573
if (CURLE_OK != error) {
574+
OLP_SDK_LOG_ERROR(kLogTag, "Send failed - curl_easy_setopt error="
575+
<< error << ", id=" << id);
560576
return ErrorCode::UNKNOWN_ERROR;
561577
}
562-
OLP_SDK_LOG_DEBUG(kLogTag, "curl bundle path: " << curl_ca_bundle);
563578
}
564579
#endif
565580

@@ -592,10 +607,8 @@ ErrorCode NetworkCurl::SendImplementation(
592607
curl_easy_setopt(handle->handle, CURLOPT_ERRORBUFFER, handle->error_text);
593608

594609
#if (LIBCURL_VERSION_MAJOR >= 7) && (LIBCURL_VERSION_MINOR >= 21)
595-
// if (config && config->IsAutoDecompressionEnabled()) {
596610
curl_easy_setopt(handle->handle, CURLOPT_ACCEPT_ENCODING, "");
597611
curl_easy_setopt(handle->handle, CURLOPT_TRANSFER_ENCODING, 1L);
598-
// }
599612
#endif
600613

601614
#if (LIBCURL_VERSION_MAJOR >= 7) && (LIBCURL_VERSION_MINOR >= 25)
@@ -613,21 +626,21 @@ ErrorCode NetworkCurl::SendImplementation(
613626
}
614627

615628
void NetworkCurl::Cancel(RequestId id) {
616-
OLP_SDK_LOG_TRACE(kLogTag, "cancel with id = " << id);
617-
618629
if (!IsStarted()) {
630+
OLP_SDK_LOG_ERROR(kLogTag, "Cancel failed - network is offline, id=" << id);
619631
return;
620632
}
621633
std::lock_guard<std::mutex> lock(event_mutex_);
622634
for (auto& handle : handles_) {
623635
if (handle.in_use && (handle.id == id)) {
624636
handle.cancelled = true;
625637
AddEvent(EventInfo::Type::CANCEL_EVENT, &handle);
638+
639+
OLP_SDK_LOG_TRACE(kLogTag, "Cancel request with id=" << id);
626640
return;
627641
}
628642
}
629-
OLP_SDK_LOG_WARNING(kLogTag, "cancel for non-existing request " << id);
630-
return;
643+
OLP_SDK_LOG_WARNING(kLogTag, "Cancel non-existing request with id=" << id);
631644
}
632645

633646
void NetworkCurl::AddEvent(EventInfo::Type type, RequestHandle* handle) {
@@ -636,10 +649,12 @@ void NetworkCurl::AddEvent(EventInfo::Type type, RequestHandle* handle) {
636649
#if (defined NETWORK_HAS_PIPE) || (defined NETWORK_HAS_PIPE2)
637650
char tmp = 1;
638651
if (write(pipe_[1], &tmp, 1) < 0) {
639-
OLP_SDK_LOG_INFO(kLogTag, "addEvent - failed " << errno);
652+
OLP_SDK_LOG_INFO(kLogTag, "AddEvent - failed for id=" << handle->id
653+
<< ", err=" << errno);
640654
}
641655
#else
642-
OLP_SDK_LOG_WARNING(kLogTag, "addEvent - no pipe");
656+
OLP_SDK_LOG_WARNING(kLogTag, "AddEvent for id=" << handle->id
657+
<< " - no pipe");
643658
#endif
644659
}
645660

@@ -649,7 +664,8 @@ NetworkCurl::RequestHandle* NetworkCurl::GetHandle(
649664
Network::DataCallback data_callback, Network::Payload payload,
650665
NetworkRequest::RequestBodyType body) {
651666
if (!IsStarted()) {
652-
OLP_SDK_LOG_ERROR(kLogTag, "Network is not started");
667+
OLP_SDK_LOG_ERROR(kLogTag,
668+
"GetHandle failed - network is offline, id=" << id);
653669
return nullptr;
654670
}
655671
std::lock_guard<std::mutex> lock(event_mutex_);
@@ -658,7 +674,8 @@ NetworkCurl::RequestHandle* NetworkCurl::GetHandle(
658674
if (!handle.handle) {
659675
handle.handle = curl_easy_init();
660676
if (!handle.handle) {
661-
OLP_SDK_LOG_ERROR(kLogTag, "curl_easy_init returns nullptr");
677+
OLP_SDK_LOG_ERROR(kLogTag,
678+
"GetHandle - curl_easy_init failed, id=" << id);
662679
return nullptr;
663680
}
664681
curl_easy_setopt(handle.handle, CURLOPT_NOSIGNAL, 1);
@@ -691,7 +708,8 @@ NetworkCurl::RequestHandle* NetworkCurl::GetHandle(
691708
}
692709
}
693710

694-
OLP_SDK_LOG_WARNING(kLogTag, "all curl handles are in use");
711+
OLP_SDK_LOG_DEBUG(kLogTag,
712+
"GetHandle failed - all CURL handles are busy, id=" << id);
695713
return nullptr;
696714
}
697715

@@ -718,7 +736,8 @@ size_t NetworkCurl::RxFunction(void* ptr, size_t size, size_t nmemb,
718736
RequestHandle* handle) {
719737
const size_t len = size * nmemb;
720738

721-
OLP_SDK_LOG_TRACE(kLogTag, "Received " << len << " bytes");
739+
OLP_SDK_LOG_TRACE(kLogTag, "Received " << len
740+
<< " bytes for id=" << handle->id);
722741

723742
std::shared_ptr<NetworkCurl> that = handle->self.lock();
724743
if (!that) {
@@ -741,9 +760,8 @@ size_t NetworkCurl::RxFunction(void* ptr, size_t size, size_t nmemb,
741760
if (handle->payload->tellp() != std::streampos(handle->count)) {
742761
handle->payload->seekp(handle->count);
743762
if (handle->payload->fail()) {
744-
OLP_SDK_LOG_WARNING(
745-
kLogTag,
746-
"Reception stream doesn't support setting write point");
763+
OLP_SDK_LOG_WARNING(kLogTag,
764+
"Payload seekp() failed, id=" << handle->id);
747765
handle->payload->clear();
748766
}
749767
}
@@ -825,10 +843,12 @@ size_t NetworkCurl::HeaderFunction(char* ptr, size_t size, size_t nitems,
825843
} else if ((str[21] >= '0') && (str[21] <= '9')) {
826844
handle->offset = std::stoll(str.substr(21));
827845
} else {
828-
OLP_SDK_LOG_WARNING(kLogTag, "Invalid Content-Range header: " << str);
846+
OLP_SDK_LOG_WARNING(kLogTag, "Invalid Content-Range header for id="
847+
<< handle->id << " : " << str);
829848
}
830849
} else {
831-
OLP_SDK_LOG_WARNING(kLogTag, "Invalid Content-Range header: " << str);
850+
OLP_SDK_LOG_WARNING(kLogTag, "Invalid Content-Range header for id="
851+
<< handle->id << " : " << str);
832852
}
833853
}
834854
return len;
@@ -867,7 +887,8 @@ void NetworkCurl::CompleteMessage(CURL* handle, CURLcode result) {
867887
std::string etag = handles_[index].etag;
868888
std::string contentType = handles_[index].content_type;
869889
if (!callback) {
870-
OLP_SDK_LOG_WARNING(kLogTag, "Complete to request without callback");
890+
OLP_SDK_LOG_DEBUG(kLogTag, "Request completed - no callback, id="
891+
<< handles_[index].id);
871892
ReleaseHandleUnlocked(&handles_[index]);
872893
return;
873894
}
@@ -909,21 +930,24 @@ void NetworkCurl::CompleteMessage(CURL* handle, CURLcode result) {
909930
}
910931
}
911932

933+
const char* url;
934+
curl_easy_getinfo(handles_[index].handle, CURLINFO_EFFECTIVE_URL, &url);
935+
912936
if ((status > 0) && ((status < 200) || (status >= 500))) {
913937
if (!handles_[index].cancelled &&
914938
(handles_[index].retry_count++ < handles_[index].max_retries)) {
915-
const char* url;
916-
curl_easy_getinfo(handles_[index].handle, CURLINFO_EFFECTIVE_URL, &url);
917-
OLP_SDK_LOG_DEBUG(kLogTag, "Retry after: " << error << "; " << url);
939+
OLP_SDK_LOG_DEBUG(kLogTag, "Retrying request with id="
940+
<< handles_[index].id << ", url=" << url
941+
<< " err=" << error);
918942
handles_[index].count = 0;
919943
lock.lock();
920944
events_.emplace_back(EventInfo::Type::SEND_EVENT, &handles_[index]);
921945
return;
922946
}
923947
}
924-
925-
OLP_SDK_LOG_DEBUG(
926-
kLogTag, "Completed message " << handles_[index].id << " " << error);
948+
OLP_SDK_LOG_DEBUG(kLogTag, "Completed message id=" << handles_[index].id
949+
<< ", url=" << url
950+
<< ", status=" << error);
927951

928952
auto response = NetworkResponse()
929953
.WithRequestId(handles_[index].id)
@@ -932,7 +956,7 @@ void NetworkCurl::CompleteMessage(CURL* handle, CURLcode result) {
932956
ReleaseHandle(&handles_[index]);
933957
callback(response);
934958
} else {
935-
OLP_SDK_LOG_WARNING(kLogTag, "Complete to unknown message");
959+
OLP_SDK_LOG_WARNING(kLogTag, "Complete message to unknown request");
936960
}
937961
}
938962

@@ -965,9 +989,10 @@ void NetworkCurl::Run() {
965989
CURLMcode res =
966990
curl_multi_add_handle(curl_, event.handle->handle);
967991
if ((res != CURLM_OK) && (res != CURLM_CALL_MULTI_PERFORM)) {
968-
OLP_SDK_LOG_ERROR(kLogTag, "Send failed with "
969-
<< res << " "
970-
<< curl_multi_strerror(res));
992+
OLP_SDK_LOG_ERROR(
993+
kLogTag, "Send failed for id="
994+
<< event.handle->id << " with result=" << res
995+
<< ", error=" << curl_multi_strerror(res));
971996
msgs.push_back(event.handle->handle);
972997
}
973998
}
@@ -1018,8 +1043,8 @@ void NetworkCurl::Run() {
10181043
CompleteMessage(handle, result);
10191044
lock.lock();
10201045
} else {
1021-
OLP_SDK_LOG_ERROR(kLogTag,
1022-
"Message complete with unknown state " << msg->msg);
1046+
OLP_SDK_LOG_ERROR(kLogTag, "Message complete with unknown state "
1047+
<< msg->msg);
10231048
int handle_index = GetHandleIndex(handle);
10241049
if (handle_index >= 0) {
10251050
if (!handles_[handle_index].callback) {
@@ -1127,19 +1152,21 @@ void NetworkCurl::Run() {
11271152
curl_multi_remove_handle(curl_, handle);
11281153

11291154
if (remove_handle_status == CURLM_OK) {
1130-
OLP_SDK_LOG_WARNING(kLogTag, "Releasing lost handle for " << url);
1155+
OLP_SDK_LOG_WARNING(kLogTag,
1156+
"Releasing lost handle for url=" << url);
11311157
CompleteMessage(handle, CURLE_OPERATION_TIMEDOUT);
11321158
} else {
1133-
OLP_SDK_LOG_ERROR(
1134-
kLogTag, "lost handle curl_multi_remove_handle error %s "
1135-
<< std::to_string(remove_handle_status) << " for "
1136-
<< url);
1159+
OLP_SDK_LOG_ERROR(kLogTag,
1160+
"Lost handle curl_multi_remove_handle error="
1161+
<< std::to_string(remove_handle_status)
1162+
<< ", url=" << url);
11371163

11381164
int handle_index = GetHandleIndex(handle);
11391165
if (handle_index >= 0) {
11401166
if (!handles_[handle_index].callback) {
1141-
OLP_SDK_LOG_WARNING(kLogTag,
1142-
"Complete to request without callback");
1167+
OLP_SDK_LOG_DEBUG(kLogTag,
1168+
"Request completed - no callback, id="
1169+
<< handles_[handle_index].id);
11431170
} else {
11441171
auto response =
11451172
NetworkResponse()
@@ -1224,7 +1251,7 @@ void NetworkCurl::Run() {
12241251
state_ = WorkerState::STOPPED;
12251252
event_condition_.notify_one();
12261253
}
1227-
OLP_SDK_LOG_DEBUG(kLogTag, "Thread exit");
1254+
OLP_SDK_LOG_DEBUG(kLogTag, "Thread exit, this=" << this);
12281255
}
12291256

12301257
} // namespace http

0 commit comments

Comments
 (0)