Click here to Skip to main content
12,820,521 members (29,576 online)
Click here to Skip to main content
Add your own
alternative version


80 bookmarked
Posted 5 Apr 2000

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.


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.


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

Questionhow I can add color to ceditlog? Pin
Member 328166623-Apr-13 6:18
memberMember 328166623-Apr-13 6:18 
GeneralPassed on compiling, but failed on running on Windows Vista Pin
Golden Lee3-Dec-09 20:25
memberGolden Lee3-Dec-09 20:25 
GeneralRe: Passed on compiling, but failed on running on Windows Vista Pin
Daniel Lohmann7-Dec-09 1:37
memberDaniel Lohmann7-Dec-09 1:37 
AnswerRe: Passed on compiling, but failed on running on Windows Vista Pin
ysliou19-May-11 4:53
memberysliou19-May-11 4:53 
GeneralRe: Passed on compiling, but failed on running on Windows Vista Pin
Randor 7-Jul-12 14:50
member Randor 7-Jul-12 14:50 
GeneralRe: Passed on compiling, but failed on running on Windows Vista Pin
Walter Schmoll19-Sep-12 23:40
memberWalter Schmoll19-Sep-12 23:40 
GeneralRe: Passed on compiling, but failed on running on Windows Vista Pin
Randor 17-Oct-12 15:09
member Randor 17-Oct-12 15:09 
QuestionHow can I change the text color in this Class Pin
huyphuc9-Dec-08 16:42
memberhuyphuc9-Dec-08 16:42 
QuestionHow do i use it for CRichEditCtrl? Pin
smalti16-Aug-07 4:02
membersmalti16-Aug-07 4:02 
GeneralMax. number of lines control Pin
Vic Siu7-Oct-06 16:59
memberVic Siu7-Oct-06 16:59 
QuestionWhat about printf Pin
Avri27-Sep-06 20:54
memberAvri27-Sep-06 20:54 
AnswerRe: What about printf Pin
nikestone24-Nov-06 18:31
membernikestone24-Nov-06 18:31 
GeneralNice but some error in VC2005 Pin
ansoncat13-Jun-06 20:02
memberansoncat13-Jun-06 20:02 
GeneralRe: Nice but some error in VC2005 Pin
w00tzenheimer21-Jul-06 20:39
memberw00tzenheimer21-Jul-06 20:39 
GeneralNice Pin
ucc8013-Feb-06 0:03
memberucc8013-Feb-06 0:03 
GeneralSlow log Pin
vdaanen25-Nov-05 2:26
membervdaanen25-Nov-05 2:26 
GeneralRe: Slow log Pin
Daniel Lohmann26-Nov-05 4:28
memberDaniel Lohmann26-Nov-05 4:28 
QuestionApplication Crash on exit Pin
Prof.S.Mukherjee13-Nov-05 21:47
memberProf.S.Mukherjee13-Nov-05 21:47 
AnswerRe: Application Crash on exit Pin
churl-hee Jo9-May-06 19:29
memberchurl-hee Jo9-May-06 19:29 
QuestionHow to change font Pin
Marko Winter13-Jul-05 2:38
memberMarko Winter13-Jul-05 2:38 
AnswerRe: How to change font Pin
Daniel Lohmann13-Jul-05 23:24
memberDaniel Lohmann13-Jul-05 23:24 
GeneralRe: How to change font Pin
Marko Winter15-Jul-05 0:42
memberMarko Winter15-Jul-05 0:42 
Hello Daniel!

Thanks for the hint - no that isn't a CEditLog related problem! I see same behaviour if I use a plain CEditView derived class.

That surprises me - because such code works with other CView derived classes.

But now I get a solution: creating a CFont member of class, setting the properties and set these font in onCreate

Thank you very much - thanks for the code too!

I wish you a nice weekend! with kind regards

Marko Winter Smile | :)
Software & Systems
SysMik GmbH Dresden |
Dezentral automatisieren
LON over IP mit INLINE von Phoenix Contact
SysMik | ICS – InlineControlServer
die neue Controllergeneration
SysMik GmbH Dresden
Bertolt-Brecht-Allee 24
01309 Dresden, Germany
Tel/Fax +49-(0)351-43358 -30 / -29
Generalx64 (AMD64) portability problem Pin
ModMa28-Jun-05 7:36
memberModMa28-Jun-05 7:36 
AnswerRe: x64 (AMD64) portability problem Pin
pc_jisakker5-Apr-09 6:19
memberpc_jisakker5-Apr-09 6:19 
GeneralVery Good Pin
Iain Fraser / Zeus9-May-05 5:21
memberIain Fraser / Zeus9-May-05 5:21 
GeneralNo output at all when switching between several output destinations Pin
asango29-Apr-05 10:49
sussasango29-Apr-05 10:49 
GeneralRe: No output at all when switching between several output destinations Pin
Daniel Lohmann2-May-05 3:56
memberDaniel Lohmann2-May-05 3:56 
GeneralRe: No output at all when switching between several output destinations Pin
asango@gmx.net2-May-05 9:07
memberasango@gmx.net2-May-05 9:07 
GeneralSave the log content into file. Pin
ronenw5-Dec-04 5:02
memberronenw5-Dec-04 5:02 
GeneralUseful tool. Pin
ronenw24-Nov-04 5:16
memberronenw24-Nov-04 5:16 
Generalnot using standard c++ libraries Pin
sammyc21-Sep-04 18:16
membersammyc21-Sep-04 18:16 
QuestionIs it really thread safed? Pin
yuancc@21cn.com15-Jun-04 3:59
sussyuancc@21cn.com15-Jun-04 3:59 
AnswerRe: Is it really thread safed? Pin
Daniel Lohmann18-Jun-04 1:06
memberDaniel Lohmann18-Jun-04 1:06 
GeneralRe: Is it really thread safed? Pin
yuancc@21cn.com19-Jun-04 1:30
sussyuancc@21cn.com19-Jun-04 1:30 
QuestionHow to reduce Flicker? Pin
pmackell17-Sep-03 10:08
memberpmackell17-Sep-03 10:08 
AnswerRe: How to reduce Flicker? Pin
gbiddison24-Jun-04 21:09
membergbiddison24-Jun-04 21:09 
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 
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 

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.

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