Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CEditLog - fast logging into an edit control with cout

0.00/5 (No votes)
1 Nov 2003 1  
Use an edit control for logging messages and redirect cout

Problem

I once wanted to use an edit control for fast, asynchronous text output. I need to log messages from a lot of threads, sometimes more than 1000 per second. And since I created already functions to dump my objects to std::cout I also wanted to use an edit control as std::ostream.

Features

  • Asynchronous, very fast output to any edit control
  • User can scroll back, select and copy text and so on during output
  • Can be used with or without MFC (Sample is a MFC project)
  • Uses window-subclassing, so you can use it even with CEdit-derived controls
  • Save to be used by multiple threads
  • Special basic_streambuf class to create or redirect a std::ostream (such as std::cout) to an edit control

Using CEditLog

Just create an Instance of CEditLog and attach an edit control to it using the SetEditControl() function. Then you can add text to the end of the edit control using one of the AddText() members:

class CEditLog : protected CSubclassWnd
{
public:
    typedef CSubclassWnd root_type;

    // Constructs the CEditLog. You can pass the edit controls handle


    // here or set it later using the SetEditCtrl() function.


    // To increase performance CEditLog repaints the edit-control


    // only if the caret resides in the last line. Otherwise the


    // control will be refreshed only every nMaxRefreshDelay msec.


    CEditLog( HWND hEdit = NULL, UINT nMaxRefreshDelay = 500 );
    
    // Adds some text to the end of the edit control. Works asynchronously


    // (using PostMessage()) and is save to be called by multiple threads.


    // If you pass true for bLFtoCRLF every LF (ASCII 10) (as it is used in


    // most cases for end-of-line) will be converted to a CR/LF (ASCII 10/13)


    // sequence as it is needed by the windows edit control. 


    virtual void AddText( LPCWSTR pwszAdd, bool bLFtoCRLF = false );
    
    // Converts pszAdd to UNICODE and calls the above


    void AddText( LPCSTR pszAdd, bool bLFtoCRLF = false );

    // Sets the edit-control to be used for logging.


    // Pass NULL to stop text logging.


    virtual void SetEditCtrl( HWND hEdit );

    HWND GetEditCtrl() const

    ...
};

The sample project

The included sample project shows the usage of CEditLog and how to use basic_editstrbuf to redirect std::cout to use the edit log.

CEditLog uses the CSubclassWnd class, written by William E. Kempf for implementation. I have included the soure of CSubclassWnd in the sample project, the complete article is also available here at CodeProject.

History

Version Comments
1.0 First Release
1.1 Minor Revision. Now uses V2.0 of SubclassWindow.
Now really works on Win9x (V1.0 used UNICODE for internal data handling and failed on Win9x systems. Now the code checks if it runs on Win9x and does the required conversions.)
1.2 Minor Revision. Now compiles fine even in VC.NET 2003.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here