Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Redirect cout to a log file for a CWinApp

Posted on 2003-11-21
3
Medium Priority
?
934 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 500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

670 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