Thread: Cout, Cerr, and Clog

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Cout, Cerr, and Clog

    Hi, I'm just wondering what's the difference of std::cout, std::cerr, and std::clog? Thanks in advance.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    purpose

    cout output
    cerr error
    clog logging

    they can be redirected independently of each other.

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by robwhit View Post
    purpose

    cout output
    cerr error
    clog logging

    they can be redirected independently of each other.
    Yeah, I've read the reference. But I still don't get it. How does each implemented? What should be used in a certain situation?

    E.g.: If I want to display some error message, I should've used cerr. But what the difference with using cerr or cout for displaying error message? I mean it always went straight to the monitor anyway.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    >> How does each implemented?

    How is each implemented? Well, that's it, I guess -- the language doesn't specify. In my Windows Vista console window, I think that each buffer leads to the console window. The point is, as was said, each can be redirected independently. System implementers are implicity encouraged to direct cerr someplace where errors can be received, cout where a user can see, and clog where things can be stored.

    >> But what the difference with using cerr or cout for displaying error message?
    In my console enviornment, nothing. Unless I make it different.
    Last edited by CodeMonkey; 07-09-2007 at 10:41 PM. Reason: grammar
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    there may be situations where cout and cerr are different devices.

    say a bank is using microsoft software on an ATM and something goes wrong. to the person at the ATM they are presented with a cover-up of the error (cout), while another message is sent to a terminal in the back room of the bank (cerr) so that someone knows what happened and to act on it.

    most of the time, your software will simply be displayed on a computer monitor to someone sitting at a desk, and any output your program does (to either cout or cerr) will be sent to the same device--the monitor.

    hope im on the right track for helping to explain.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    How does each implemented?
    they're all ostreams.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"cout";
        cerr<<"cerr";
        clog<<"clog";
        return 0;
    }
    Code:
    C:\>g++ main.cc -o a.exe
    
    C:\>a 1> o 2> i
    
    C:\>more o
    cout
    
    C:\>more i
    cerrclog
    
    C:\>
    Last edited by robwhit; 07-09-2007 at 11:06 PM.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by g4j31a5 View Post
    E.g.: If I want to display some error message, I should've used cerr. But what the difference with using cerr or cout for displaying error message? I mean it always went straight to the monitor anyway.
    The operating system usually allows the user to redirect each of the three standard streams (cin, cout, cerr) to separate locations. If a program produces output AND logging/error messages, it is helpful to be able to direct ONLY the output to some destination, while still sending the error messages to the screen. Or, send errors to a file for later inspection. At any rate, you usually do not want the error output interleaved with the "normal" output, even though this is what happens when it goes to a screen.

  8. #8
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Thanks to all of you. I finally understand more of them. I thought cout and cerr will always be sent to a screen. So if I'm not mistaken we can redirect the output of cerr (or clog) to another device while cout will always be directed to a standart output (screen), right? I get it now. Thanks.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, not right. You can redirect standard output wherever you want, too.

    First, cout, cerr and clog are ostream objects. You can call rdbuf() on them to redirect their output independently wherever you want, from within the application. You can open a network socket, wrap it in a stream buffer and redirect there, if you want.

    By default, cout is tied to the application's standard output. By default, the standard output is the screen. You can direct the OS to redirect stdout elsewhere. Or it might do it by itself - the nohup utility in Linux, for example, does. Services in Windows also have their standard streams redirected, I think.

    By default, cerr and clog are tied to the application's standard error. By default the standard error is the screen. You can again redirect stderr elsewhere.
    Another issue here is that clog, by default, is buffered like cout, whereas cerr is unit-buffered, meaning it automatically calls flush() after every complete output operation. This is very useful, since it means that the output is not lost in the buffer if the application crashes directly afterwards.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by CornedBee View Post
    No, not right. You can redirect standard output wherever you want, too.

    First, cout, cerr and clog are ostream objects. You can call rdbuf() on them to redirect their output independently wherever you want, from within the application. You can open a network socket, wrap it in a stream buffer and redirect there, if you want.

    By default, cout is tied to the application's standard output. By default, the standard output is the screen. You can direct the OS to redirect stdout elsewhere. Or it might do it by itself - the nohup utility in Linux, for example, does. Services in Windows also have their standard streams redirected, I think.

    By default, cerr and clog are tied to the application's standard error. By default the standard error is the screen. You can again redirect stderr elsewhere.
    Another issue here is that clog, by default, is buffered like cout, whereas cerr is unit-buffered, meaning it automatically calls flush() after every complete output operation. This is very useful, since it means that the output is not lost in the buffer if the application crashes directly afterwards.
    OIC, thanks. I always assume that standart output will always be the screen because that's the common thing. Ok, now I think I really get it. Thanks again.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed