C++ Learning Community Forum
September 09, 2010, 07:44:21 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Logging to a console and a file in a Win32 C++ GUI application  (Read 1332 times)
TheoLogic
Nerd
****
Posts: 92



View Profile
« on: March 16, 2009, 11:00:08 AM »

http://www.theologic.eu/CPP/Logger/Logging%20to%20a%20console%20and%20a%20file%20in%20a%20Win32%20C++%20GUI%20application.pdf
Logged

TheoLogic - Portfolio.
Wiki - Personal wiki pages.
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #1 on: March 16, 2009, 06:10:37 PM »

i dont know what half of those headers are yet and a bunch of that code but it was good reading. thanks
Logged
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #2 on: March 16, 2009, 08:28:17 PM »

Looks like it's not quite finished Smiley
Quote
Remark the includes needed to create, show and write to the console.
Quote
Remark we use “try-catch” error handling.

I noticed you're using the potentially buggy code that everyone seems to find for adding a console and opening the standard handles. Casting the handles to long instead of intptr_t could chop the values in half on 64-bit Windows leaving them with the wrong values.

Also:
Code
GeSHi (cpp):
GetConsoleTitle(static_cast<LPWSTR>(sConsoleWindowName), 1024);
hConsole = FindWindow(NULL, sConsoleWindowName);
[/url]
can be just
[source=cpp]
hConsole = GetConsoleWindow();
[/source]
Created by GeSHI 1.0.7.18
Logged

TheoLogic
Nerd
****
Posts: 92



View Profile
« Reply #3 on: March 17, 2009, 08:56:39 AM »

This is true, but I cropped the code where I set the consolewindows's name Smiley
Offcourse, many ways to do this, but I haven't got any problems with this...
Logged

TheoLogic - Portfolio.
Wiki - Personal wiki pages.
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #4 on: March 17, 2009, 05:28:03 PM »


A file and the console are just streams.
You could simply write a simple wrapper that sent data to both!

Code:

class TwoOutputs
{
    public: TwoOutputs(std::ostream& one,std::ostream& two)
                  : m_one(one)
                  , m_two(two)
              {}

        std::ostream& m_one;
        std::ostream& m_two;
};

template<typename T>
TwoOutputs& operator<<(TwoOutputs& dst,T const& data)
{
    dst.m_one << data;
    dst.m_two << data;

    return *this;
}

int main()
{
    std::ofstream   file(plop);
    TwoOutputs     log(file,std::cerr);

    log << "Output To both streams\n";
};
Logged
TheoLogic
Nerd
****
Posts: 92



View Profile
« Reply #5 on: March 18, 2009, 08:31:40 AM »

Agreed, but I think your code is pretty unreadable for beginners C++, and you'd still need RAII for safe writting to a file. But this idea is a good thing to think about.
Keep the remarks comming!
Logged

TheoLogic - Portfolio.
Wiki - Personal wiki pages.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!