What is the point of hammering?

I was wondering what is the point of clogging up? As far as I can tell, clog is similar to cerr, but with buffering, so it is more efficient. Usually stderr is the same as stdout, so clog is similar to cout. It seems very lame to me, so I suppose it must be misunderstood. If I have log messages that go to the same place, I get error messages (maybe something in / var / log / messages), then I probably don't write too much (so not so much lost, buffered cerr). In my experience, I want my log messages to be updated (not buffered), so I can help find a crash (so I don't want to use a buffered shutter). Apparently, I should always use cerr.

I would like to be able to redirect a glitch inside my program. It would be useful to redirect cerr so that when I call the library procedure, I can control where cerr and clog happen. Can some compilers support this? I just checked DJGPP, and stdout is defined as the address of the FILE structure, so it is illegal to do something like "stdout = freopen (...)".

  • Is it possible to redirect clog, cerr, cout, stdin, stdout and / or stderr?
  • The only difference between buffers and cerr buffering?
  • How do I (or find) a more reliable logging tool (links)?
+50
c ++ logging log4cpp
Sep 09 '08 at 17:08
source share
4 answers

Is it possible to redirect clog, cerr, cout, stdin, stdout and / or stderr?

Yes. Do you want rdbuf .

 ofstream ofs("logfile"); cout.rdbuf(ofs.rdbuf()); cout << "Goes to file." << endl; 

Is the difference between clog and cerr buffering?

As far as I know, yes.

+35
Sep 09 '08 at 17:13
source share

If you are in the posix shell environment (I really think of bash), you can redirect any file descriptor to any other file descriptor, so for redirection you can simply:

 $ myprogram 2>&5 

redirect stderr to the file represented by fd = 5.

Edit: On a second thought, I like @Konrad Rudolph, answer about redirecting better. rdbuf () is a more consistent and portable way to do this.

As for logging, well ... I start with the Boost library for all C ++ things that are not in the std library. Here: Boost Logging v2

Edit : Boost Logging is not part of the Boost libraries; It has been reviewed but not accepted.

Edit : 2 years later, back in May 2010, Boost adopted the registration library, now called Boost.Log .

Of course there are alternatives:

There is also a Windows event logger.

And a few articles that may be helpful:

+14
Sep 09 '08 at 17:16
source share

Primary Registrar

 #define myerr(e) {CriticalSectionLocker crit; std::cerr << e << std::endl;} 

Used as myerr("ERR: " << message); or myerr("WARN: " << message << code << etc);

Very effective.

Then do:

 ./programname.exe 2> ./stderr.log perl parsestderr.pl stderr.log 

or just parse stderr.log manually

I admit that this is not for extremely critical performance code. But who writes it anyway.

0
Dec 18 2018-10-18T00:
source share

Since there are several answers about redirection here, I will add this wonderful gem with which I recently came across a redirect:

 #include <fstream> #include <iostream> class redirecter { public: redirecter(std::ostream & dst, std::ostream & src) : src(src), sbuf(src.rdbuf(dst.rdbuf())) {} ~redirecter() { src.rdbuf(sbuf); } private: std::ostream & src; std::streambuf * const sbuf; }; void hello_world() { std::cout << "Hello, world!\n"; } int main() { std::ofstream log("hello-world.log"); redirecter redirect(log, std::cout); hello_world(); return 0; } 

This is basically a redirection class that allows you to redirect any two threads and restore it when you are done.

0
May 25 '19 at 20:00
source share



All Articles