Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / MFC
Article

CEditLog - fast logging into an edit control with cout

Rate me:
Please Sign up or sign in to vote.
4.69/5 (32 votes)
1 Nov 2003CPOL 309.4K   4K   86   84
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

VersionComments
1.0First Release
1.1Minor 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.2Minor Revision. Now compiles fine even in VC.NET 2003.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
Daniel Lohmann (daniel@losoft.de) is Assistant Professor at the Distributed Systems and Operating Systems department at Friedrich-Alexander-University Erlangen-Nuremberg, Germany. His main research topic is the design of a highly customizable and scalable operating system product line for deeply embedded systems using static configuration and aspect-oriented techniques. Before joining Universität Erlangen he worked as a freelance trainer and consultant for NT system programming, advanced C++ programming and OOA/OOD. He is interested in upcoming programming techniques like aspect-oriented programming, generative programming and C++ meta coding and has written some nice and handy tools for Windows NT which you can download at his web site.

Comments and Discussions

 
GeneralAnother problem Pin
Zamel9-Jul-02 3:10
Zamel9-Jul-02 3:10 
GeneralRe: Another problem Pin
Daniel Lohmann9-Jul-02 4:45
Daniel Lohmann9-Jul-02 4:45 
GeneralA problem Pin
4-Jul-02 1:49
suss4-Jul-02 1:49 
GeneralRe: A problem Pin
Daniel Lohmann4-Jul-02 4:33
Daniel Lohmann4-Jul-02 4:33 
GeneralAlmost Perfect! Pin
14-May-02 14:03
suss14-May-02 14:03 
GeneralEmptying edit box Pin
Matt Weagle21-Apr-02 9:57
Matt Weagle21-Apr-02 9:57 
GeneralRe: Emptying edit box Pin
Daniel Lohmann22-Apr-02 1:53
Daniel Lohmann22-Apr-02 1:53 
GeneralRe: Emptying edit box Pin
Gernot Frisch9-Dec-02 5:18
Gernot Frisch9-Dec-02 5:18 
How about:
<br />
::SetWindowText(hEdit, "");<br />


-Gernot Frisch
Dream Design
reverse Tonreg to reply...
GeneralRe: Emptying edit box Pin
Daniel Lohmann9-Dec-02 8:03
Daniel Lohmann9-Dec-02 8:03 
GeneralErrors - Help Pin
Darren Schroeder5-Nov-00 13:13
Darren Schroeder5-Nov-00 13:13 
GeneralRe: Errors - Help Pin
Daniel Lohmann8-Nov-00 6:25
Daniel Lohmann8-Nov-00 6:25 
GeneralRe: Errors - Found the answer Pin
braincookie13-May-03 4:00
braincookie13-May-03 4:00 
GeneralA few issues Pin
Colin Johnson31-May-00 11:53
Colin Johnson31-May-00 11:53 
GeneralRe: A few issues Pin
Daniel Lohmann14-Jun-00 13:00
Daniel Lohmann14-Jun-00 13:00 
GeneralRe: A few issues Pin
MordachaiWolf14-Dec-01 5:12
MordachaiWolf14-Dec-01 5:12 
QuestionGreat! How about stdout? Pin
Qi Yang30-May-00 12:01
Qi Yang30-May-00 12:01 
AnswerRe: Great! How about stdout? Pin
Daniel Lohmann14-Jun-00 12:53
Daniel Lohmann14-Jun-00 12:53 
GeneralWay cool Pin
Syver Enstad7-Apr-00 12:42
Syver Enstad7-Apr-00 12:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.