rlillbac
rlillbac

Reputation: 71

CURLOPT_VERBOSE isn't sending to STDERR

Ubuntu 20.04 using libcurl and the following code, I can view the output from the command line. HOWEVER, when redirecting output ./run_me 2>&1 > output.log the cURL output still displays on the screen and does NOT get redirected to the output.log file.

The cURL documentation states that the CURLOPT_VERBOSE without setting CURLOPT_STDERR should go to STDERR. However, it appears it does not.

How do we redirect the cURL output to STDERR?

CURL* curl = curl_easy_init();
char errBuff[CURL_ERROR_SIZE];
if(curl) {
    struct MemoryStruct chunk;
    chunk.size = 0;
    chunk.memory = calloc(1,sizeof(*chunk.memory));
    if ( !chunk.memory ) {
      return CURLE_FAILED_INIT;
    }

    (void)curl_easy_setopt(curl, CURLOPT_URL, url);
    (void)curl_easy_setopt(curl, CURLOPT_POST, 1L);
    (void)curl_easy_setopt(curl, CURLOPT_USERNAME, username);
    (void)curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
    (void)curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT);
    (void)curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    (void)curl_easy_setopt(curl, CURLOPT_CAINFO, trustStore);
    (void)curl_easy_setopt(curl, CURLOPT_SSLCERT, clientCert);
    (void)curl_easy_setopt(curl, CURLOPT_SSLKEY, clientKey);
    (void)curl_easy_setopt(curl, CURLOPT_KEYPASSWD, clientKeyPass);
    (void)curl_easy_setopt(curl, CURLOPT_VERBOSE, 1 );
    (void)curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errBuff );
    errBuff[0] = 0; // empty the error buffer   
    (void)curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    (void)curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

    struct curl_slist* list = NULL;
    list = curl_slist_append(NULL, "Content-Type: application/json");
    list = curl_slist_append(list, "Accept: application/json");
    char clBuf[30];
    (void)snprintf(clBuf, 30, "Content-Length: %d", (int)strlen(postData));
    list = curl_slist_append(list, clBuf);
    (void)curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
    (void)curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
    long httpCode = 0;
    int res = CURLE_FAILED_INIT;
    res = curl_easy_perform(curl);
    (void)curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpCode);
}

Upvotes: 1

Views: 109

Answers (1)

drew010
drew010

Reputation: 69927

The answer has to do with how the shell treats the ordering of the redirections.

From the bash manual:

Note that the order of redirections is significant. For example, the command

  ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command

  ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist.

Changing the command to ./run_me > output.log 2>&1 should result in curl's verbose output also being redirected to the file.

Upvotes: 2

Related Questions