3

I need a way to write the logs that libcurl produces on stderr, to a file. I find this as a common question, for example in the one below,

http://curl.haxx.se/mail/lib-2002-04/0051.html

but I believe this will write the actual data to file, not the underlying logs.

I need logs such as below, from stderr instead written to a file.

About to connect() to IP.IP.IP.IPa port 22 (#0) Trying IP.IP.IP.IPa ... == Info: connected Connected to IP... ( **...) port 22 (#0)

......etc

Connection #0 to host IP.IP.IP.IPa left intact 34 bytes retrieved Closing connection #0

I also looked into debug.c, http://curl.haxx.se/libcurl/c/debug.html

where it uses the following options,

curl_easy_setopt(curl_handle, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl_handle, CURLOPT_DEBUGDATA, &config);

But this again, gives more info regarding the actual data that is transferred. It does not show anything regarding connection, authentication etc.

Please note that I am not using the command line tool.

Edit: It turns out redirection of stderr/stdout is not an acceptable solution for my application since there are multiple threads.

Nanc
  • 464
  • 5
  • 15
Tahseen
  • 87
  • 2
  • 9
  • possible duplicate of [How could I temporary redirect stdout to a file in a C program?](http://stackoverflow.com/questions/4832603/how-could-i-temporary-redirect-stdout-to-a-file-in-a-c-program) – aruisdante Aug 15 '14 at 16:40
  • possible duplicate of [logging in libcurl](http://stackoverflow.com/questions/15337319/logging-in-libcurl) – Sean Bright Aug 15 '14 at 18:45
  • @aruisdante: yes, the solution would be simlar – Tahseen Aug 15 '14 at 19:32
  • @SeanBright: No, that is not what I am asking for as I mentioned already in my question about debug.c. – Tahseen Aug 15 '14 at 19:39
  • This question would remove the confusion one might have with "libcurl option" of logging to files – Tahseen Aug 15 '14 at 19:40
  • I edited the question. Unfortunately, redirection is not acceptable – Tahseen Aug 15 '14 at 20:38

3 Answers3

1

Using curl_easy_setopt(curl_handle, CURLOPT_DEBUGFUNCTION, my_trace) will redirect all messages libcurl prints to stderr to this function instead. You can then dispose of the messages in your my_trace function however you want.

If you've tried that and you're still seeing messages being printed to stderr then it's probably either because of two reasons: 1) You've created multiple libcurl handles and you're not setting CURLOPT_DEBUGFUNCTION on all of them or 2) Something other than libcurl is printing the messages.

Since you just want redirect libcurl's debug messages to a file you can also do curl_easy_setopt(curl_handle, CURLOPT_STDERR, my_file_stream) instead, but if CURLOPT_DEBUGFUCTION didn't work neither will this.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • using CURLOPT_STDERR works. Can I do the exact same with CURLOPT_DEBUGFUNCTION. CURLOPT_DEBUGFUNCTION returns several curl_infotype, and based on those you choose actions. How do you redirect stderr from this? – Tahseen Aug 18 '14 at 14:19
  • I put the C code with *CURLOPT_STDERR* as a separate answer – Tahseen Aug 18 '14 at 14:20
  • I put the C code with stdout/stderr redirection as a separate answer – Tahseen Aug 18 '14 at 15:20
1

Deriving from Ross Ridge's answer, the following C code worked for me

    /* open file for writing */
    FILE *file_debug=NULL;
    file_debug = fopen("/localhome/username/file.txt", "a+");   //open the specified file on local host
    curl_easy_setopt(curl_handle, CURLOPT_STDERR,file_debug);

and after curl_easy_perform(handle); you can do fclose(file_debug);

Tahseen
  • 87
  • 2
  • 9
  • 1
    You should wait until you call `curl_easy_cleanup(handle)` before you call `fclose(file_debug)`. libcurl may try to write debugging message in `curl_easy_cleanup` or any other library function you call after `curl_easy_perform`. – Ross Ridge Aug 18 '14 at 15:29
0

Deriving from aruisdante's comment, this problem could be solved outside libcurl, by redirecting stdout to write to a file.

I post a sample solution below (ref: http://codewiki.wikidot.com/c:system-calls:dup2)

int file = open("/path/to/file.txt", O_APPEND | O_WRONLY);
dup2(file, 2); //1 for stdout and 2 for stderr

But for multithreaded applications, this is not applicable.

Tahseen
  • 87
  • 2
  • 9