Solved

Redirect cout to a log file for a CWinApp

Posted on 2003-11-21
3
896 Views
Last Modified: 2011-04-14
I want to redirect the cout and cerr to a logfile for my application.
The following code works but only in InitInstance().
How do i make the redirect work for the entire app??

BOOL CDwgFilesApp::InitInstance()
{
//Create Runtime debug log
    std::ofstream logFile("c:\\IBitS\\tmp\\SDBSdwg.log");
    std::streambuf *outbuf = std::cout.rdbuf(logFile.rdbuf());
    std::streambuf *errbuf = std::cerr.rdbuf(logFile.rdbuf());
    std::cout << "SDBSdwg Runtime Debug Log\n\n";
}
else if (nResponse == IDCANCEL)
{
   // restore the buffers
    std::cout.rdbuf(outbuf);
    std::cerr.rdbuf(errbuf);
}

this does not work!!!!
BOOL CDwgFilesDlg::OnInitDialog()
{
  std::cout << "this is a test\n";
}
0
Comment
Question by:MCofer
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 125 total points
Comment Utility
The biggest issue here is that the logFile object goes out of scope after InitInstance (and so does the streambuf associated with it).  Add std::ofstream logFile; as a data member to your CDwgFileApp class, and call logFile.open("c:\\logfile.log"); in InitInstance, then it should work.  Good luck!
0
 

Author Comment

by:MCofer
Comment Utility
Thanks
  It's working great (except)
  It creates the log file at InitInstance() but does not write anything until I cancel the App.
  Is there a way to make it write each time cout is called?
0
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
If you call DoModal() inside your InitInstance, then the function will not return until the Ok or Cancel buttons are clicked (or equivalent commands).  You may want to do the cout's inside the dialog.  Also, if you're already doing this, try to insert flush to flush the data into the file, like:
std::cout << "This is my message" << std::endl << std::flush;

If you set the unitbuf flag (std::cout.setf(std::ios_base::unitbuf);), then it will flush every time you insert something into the stream.  I hope this helps.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now