Wiingaard
Wiingaard

Reputation: 4302

View stderr from c-library, Debian Linux

I'm very new to linux programming, and is just learning my ways around the OS. I have made an application, but it isn't working as intended to. I'm using a library, to enable some functionalities. In the source code of the library, I see that a macro is defined:

#ifdef __DEBUG
#define DEBUG_PRINTF(FORMAT, ...) fprintf(stderr, FORMAT, ## __VA_ARGS__)
#else
#define DEBUG_PRINTF(FORMAT, ...)
#endif

I want to see the messages send DEBUG_PRINTF, so I guess I'll need a way to view the stderr.. I have no clue about how to do this. I've reasearched a bit, but can't seem to access the filedescriptor of the application.

I'm using a BeagleBone Black, running a Debian Distribution "Linux beaglebone 3.8.13-bone47"

Any help would be much appreciated...

Upvotes: 0

Views: 3255

Answers (1)

Samidamaru
Samidamaru

Reputation: 1081

stderr is not an application, it is just another output stream on the console, just like stdout. The main differences are that stderr is not (usually) buffered and is not redirected by default if you pipe your program's output somewhere. For instance if you type:

program > file.txt

Your programs output on stdout stream would be redirected to file.txt but anything printed on the stderr stream will still be displayed on the console and not written to the file. (As 23ars suggests in the comments, the way to redirect stderr as well is to use 2>&1 which redirects stderr to stdout).

You don't need to do anything differently to read stderr than you do to read stdout. It should just appear on the console. printf by default outputs to stdout which is why you have to use fprintf as this allows you to specify a different output stream - stderr.


Example:

Try the following simple hello world program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("I am printed on stdout\n");
    fprintf(stderr, "I am printed on stderr\n");

    return 0;
}

Compile and run this and you should see:

I am printed on stdout
I am printed on stderr

Now run this but pipe to a file: program > file.txt and you should now only see:

I am printed on stderr

appear on your console. If you inspect file.txt you should only see

I am printed on stdout

Finally you can try program > file.txt 2>&1 and you should see nothing printed on the console, but inspecting the file should reveal both lines have been sent there.

I hope this clears it up a bit :).

Upvotes: 1

Related Questions