Click here to Skip to main content
11,929,675 members (50,360 online)
Click here to Skip to main content
Add your own
alternative version


80 bookmarked

CEditLog - fast logging into an edit control with cout

, 1 Nov 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
Use an edit control for logging messages and redirect cout


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.


  • 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
    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.


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.


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


About the Author

Daniel Lohmann
Germany Germany
Daniel Lohmann ( 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.

You may also be interested in...

Comments and Discussions

GeneralRe: How to reduce Flicker? Pin
RJSoft23-Aug-04 4:04
memberRJSoft23-Aug-04 4:04 
AnswerRe: How to reduce Flicker? Pin
lano11064-Jul-06 19:47
memberlano11064-Jul-06 19:47 
Questionwhat about printf? Pin
omasoud14-Jul-03 15:32
memberomasoud14-Jul-03 15:32 
AnswerRe: what about printf? Pin
Daniel Lohmann14-Jul-03 22:28
memberDaniel Lohmann14-Jul-03 22:28 
GeneralRe: what about printf? Pin
omasoud15-Jul-03 1:35
memberomasoud15-Jul-03 1:35 
GeneralRe: what about printf? Pin
Daniel Lohmann16-Jul-03 6:32
memberDaniel Lohmann16-Jul-03 6:32 
GeneralHi Pin
annum14-Jun-03 17:19
memberannum14-Jun-03 17:19 
GeneralRe: Hi Pin
Daniel Lohmann15-Jun-03 2:33
memberDaniel Lohmann15-Jun-03 2:33 
annum wrote:
What I wan't to do is add coloring capability to it, that dpending on the text I can change its color.

This should be possible by building CEditLog around a RichEdit control instead of an ordinary edit control. (As a side effect this would also remove the 64kb limit on Win9x - however, I never really cared about this Poke tongue | ;-P )

Since RichEdit understoods most (all) of the messages of an edit control, the task should not be that difficult. The point that let me intentionally avoid RichEdit in the days I wrote this code, was the versioning chaos around it (it seemed to depend on Windows /IE / common-control and whatever versions...).

Have fun!


Daniel Lohmann
(Hey, this page is worth looking! You can find some free and handy NT tools there Big Grin | :-D )
GeneralRe: Hi Coloring Pin
pmackell17-Sep-03 10:06
memberpmackell17-Sep-03 10:06 
GeneralCEditLog and FormView Pin
abstract312-Jun-03 9:59
memberabstract312-Jun-03 9:59 
GeneralRe: CEditLog and FormView Pin
Daniel Lohmann15-Jun-03 2:40
memberDaniel Lohmann15-Jun-03 2:40 
GeneralRe: CEditLog and FormView Pin
sammyc6-Dec-04 15:52
membersammyc6-Dec-04 15:52 
GeneralRe: CEditLog and FormView Pin
sammyc6-Dec-04 16:13
membersammyc6-Dec-04 16:13 
GeneralRe: CEditLog and FormView Pin
Daniel Lohmann6-Dec-04 22:42
memberDaniel Lohmann6-Dec-04 22:42 
GeneralDon't work in studio .net 2003 Pin
wukas15-May-03 11:29
memberwukas15-May-03 11:29 
GeneralRe: Don't work in studio .net 2003 Pin
Daniel Lohmann26-May-03 6:29
memberDaniel Lohmann26-May-03 6:29 
GeneralRe: Don't work in studio .net 2003 Pin
Anonymous17-Jun-03 8:20
sussAnonymous17-Jun-03 8:20 
GeneralRe: Don't work in studio .net 2003 Pin
ajedlitschka26-Oct-03 10:50
memberajedlitschka26-Oct-03 10:50 
GeneralNow compiles fine in VC.NET 2003 :-) Pin
Daniel Lohmann3-Nov-03 0:25
memberDaniel Lohmann3-Nov-03 0:25 
Generalrestrict amount of text/cut first lines Pin
dontsmoke14-Apr-03 10:43
memberdontsmoke14-Apr-03 10:43 
GeneralRe: restrict amount of text/cut first lines Pin
DoubleApexCa20-Aug-04 6:40
memberDoubleApexCa20-Aug-04 6:40 
Generalusage with CEditView, handling of WM_DESTROY message Pin
luedi6-Mar-03 6:14
memberluedi6-Mar-03 6:14 
GeneralRe: usage with CEditView, handling of WM_DESTROY message Pin
Daniel Lohmann6-Mar-03 8:39
memberDaniel Lohmann6-Mar-03 8:39 
GeneralA little unrelated but... Pin
Anthony Winters28-Feb-03 14:13
memberAnthony Winters28-Feb-03 14:13 
GeneralRe: A little unrelated but... Pin
Larry Antram28-Feb-03 14:40
memberLarry Antram28-Feb-03 14:40 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 2 Nov 2003
Article Copyright 2000 by Daniel Lohmann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid