Solved

Redirect cout to a log file for a CWinApp

Posted on 2003-11-21
3
903 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
ID: 9800927
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
ID: 9801152
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
ID: 9801454
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

896 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