Click here to Skip to main content
15,881,172 members
Articles / Mobile Apps / Windows Mobile
Article

Small and Simple TRACE/Log support for your Windows application

Rate me:
Please Sign up or sign in to vote.
4.56/5 (9 votes)
22 Feb 20062 min read 71.9K   516   19   14
This header file will provide you with the smallest and simplest way to log some trace messages from your MFC application.

Introduction

Sometime while developing your next Windows application, you might want to add some simple logger or trace support into the _DEBUG version of the app. The standard Microsoft TRACE tool is just too inconvenient and weird. All you want is just printout some useful information from your code into text files, or maybe to the VS IDE output window. That is exactly what MyTrace does. Include one single header file into your project, and you are ready to go.

Usage

First of all, you need to include the mytrace.h header into your project. Then before you start logging any messages, you need to initialize MyTrace like this:

MYTRACE_INIT("MyApp_log.txt");
//Init MT with single log file

or if you want all really "bad bugs" to be reported into a separate file, then like this:

MYTRACE_INIT_EX("MyApp_log.txt", "MyApp_errors.txt");
// main log and error log files

Corresponding text files will be created in your application working folder. Optionally, you can set the minimal severity level for the messages you want to see in the logs (the default level is MT::LEVEL_INFO):

MYTRACE_LEVEL(MT::LEVEL_DEBUG);
// set minimal trace messages severity to report

And you can add trace messages like this:

MYTRACE(MT::LEVEL_INFO, 
    "\t\t\t MyApp v.%d.%d.%d started \n\n", 1, 0, 0);
MYTRACE(MT::LEVEL_ERROR, 
    "Wrong parameter: %s", wrongStringParameter);

And just before your application is about to close, do not forget to de-initialize MyTrace by a simple call like this:

MYTRACE_DEINIT;
// stop MT

Notes

You can choose one from the following severity levels for the trace message: LEVEL_ERROR, LEVEL_WARNING, LEVEL_INFO, or LEVEL_DEBUG.

If you initialize MyTrace with MYTRACE_INIT_EX then all messages with LEVEL_ERROR and LEVEL_WARNING levels will also be reported into separate error log text files for debugging convenience.

You can change some settings for MyTrace by commenting/un-commenting #defines on the top of the file.

  • #define MT_USE_ATLTRACE turns on/off the forwarding of messages to the standard Microsoft ATLTRACE macros. It enables you to see trace messages in the “output” window in the VS IDE. This is enabled by default.
  • #define MT_KEEP_ATLTRACE_LEVELS indicates if MyTrace severity levels should be treated as ATLTRACE levels also. By default, all messages forwarded to ATLTRACE with level 0.
  • #define MT_REDIRECT_STDOUT redirects global stdout into the MyTrace log file. You might want to do that if you want to grab all the output from the printf class functions, but that could interfere with your application behavior; so by default, this option is disabled.

This is an example of how the log records look like:

...
INFO    d:\projects\MyApp\MyAppMain.cpp(105) 02/20/06 09:00:41:
Starting Application...
DEBUG   d:\projects\MyApp\MyAppMain.cpp(322) 02/20/06 09:00:42:
Hello World
WARNING d:\projects\MyApp\MyAppMain.cpp(408) 02/20/06 09:00:43:
Runtime Excepton 1234
INFO    d:\projects\MyApp\MyAppMain.cpp(512) 02/20/06 09:00:44:
Closing Application...
...

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGetting it to work on VS2010? Pin
Lars [Large] Werner1-Feb-11 7:30
professionalLars [Large] Werner1-Feb-11 7:30 
QuestionHow to use it when NO Format Msg Pin
chan™17-Jan-07 15:51
chan™17-Jan-07 15:51 
GeneralVariadic macros Pin
Hokei28-Feb-06 18:35
Hokei28-Feb-06 18:35 
GeneralRe: Variadic macros Pin
suggam1-Mar-06 2:37
suggam1-Mar-06 2:37 
GeneralSuggested change Pin
Hokei28-Feb-06 18:31
Hokei28-Feb-06 18:31 
GeneralRe: Suggested change Pin
suggam1-Mar-06 2:33
suggam1-Mar-06 2:33 
GeneralRe: Suggested change Pin
Hokei1-Mar-06 2:52
Hokei1-Mar-06 2:52 
They're not functionally equivalent. The __noop can be used to effectively "comment out" a function, whereas a simple semicolon only works if it is the only/last thing on the line (after macro expansion).

But, if you look at winnt.h (from the SDK), __noop is defined as:
<br />
#ifndef NOP_FUNCTION<br />
  #if (_MSC_VER >= 1210)<br />
    #define NOP_FUNCTION __noop<br />
  #else<br />
    #define NOP_FUNCTION (void)0<br />
  #endif<br />
#endif<br />


So whereas a semicolon would compile to literally nothing in the final executable (not even an assembly no-op instruction), the __noop compiler intrinsic seems to be compiling into something. Something is more than nothing. Still, I will admit it's not going to be noticable.
QuestionHow to clear the o/p window? Pin
Nader Michael28-Feb-06 1:49
Nader Michael28-Feb-06 1:49 
AnswerRe: How to clear the o/p window? Pin
suggam1-Mar-06 2:20
suggam1-Mar-06 2:20 
Generallog4cxx Pin
Uwe Keim22-Feb-06 9:48
sitebuilderUwe Keim22-Feb-06 9:48 
GeneralRe: log4cxx Pin
suggam22-Feb-06 9:57
suggam22-Feb-06 9:57 
GeneralRe: log4cxx Pin
Uwe Keim22-Feb-06 10:06
sitebuilderUwe Keim22-Feb-06 10:06 
GeneralRe: log4cxx Pin
suggam22-Feb-06 16:16
suggam22-Feb-06 16:16 
GeneralRe: log4cxx Pin
Sam NG20-Mar-06 14:57
Sam NG20-Mar-06 14:57 

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.