Solved

Redirect cout to a log file for a CWinApp

Posted on 2003-11-21
3
921 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 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