Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MACRO: HowTo Where can I read about it?

Status
Not open for further replies.

AMosmann

Programmer
May 27, 2003
82
DE
I want to write a macro that works like TRACE, but writing that strings in files. My problem is, that the (pre)compiler translates

XTRACE(MACROHELPFILE,CString("QualityList.m_n Count: %i",QualityList.m_nCount));
CString a;a.Format("Das ist ein Test %i",3);
XTRACE(MACROHELPFILE,a);

into

16:36:01§ Datei E:\ANDREAS\PROJEKTE\OptimierungXiolet\ModeTable.cpp§ Zeile 604§ QualityList.m_n Count: %i
16:36:01§ Datei E:\ANDREAS\PROJEKTE\OptimierungXiolet\ModeTable.cpp§ Zeile 606§ Das ist ein Test 3

defined in "stdafx.h"
#include <afx.h>
extern CString XTraceStringOnlyInMacro;
extern CString XTraceStringOnlyInMacro2;
#include <fstream.h>
extern ofstream XTraceStreamOnlyInMacro;


#define XTRACE(FILE,STRING)XTraceStringOnlyInMacro2=STRING; XTraceStreamOnlyInMacro.open(FILE,ios::eek:ut | ios::app ); XTraceStringOnlyInMacro.Format(&quot;%s§ Datei %s§ Zeile %4i§ %s\n&quot;, __TIME__,__FILE__, __LINE__, XTraceStringOnlyInMacro2); XTraceStreamOnlyInMacro << XTraceStringOnlyInMacro; XTraceStreamOnlyInMacro.close();

who can help?

Greetings Andreas
 
Hi Andreas,

Years ago I have been through more or less the same process you are are in.
I never found exactly what I wanted, but ended up with a simple log function, that I call from important places in my programs.
The function prototype looks like this :

void TLog(const char * file, long int line, long int type, const char * format, ...)

A typical call to this function looks like this :
TLog(__FILE__, __LINE__, LOG_TYPE_INFO, &quot;CRequestIF::mf_CreateObjects() success&quot;);

or with a parameter :
TLog(__FILE__, __LINE__, LOG_TYPE_DEBUG, &quot;m_mem_size = %d&quot;, m_mem_size );

The TLog function can be configured to only log entries of a certain type (Info, Debug, FuncEntry, FuncExit, Dump, Error, Warning) - or a combination using a bitmask.

The function also logs date + time (Which I think is a *must*).
The function also skip the path included with the __FILE__ argument.

All my programs (Almost all are batch programs) produce a log when running. The log is overwritten each time.

The log is also active in Release builds - it's extremly nice to have a log *after* an error occurs - also in a production environment.

/JOlesen
 
Yeah logging == good [lol]

Unless instructions are critical don’t do it with macros. Use a singleton object and then if you really want you can use a simple macro or two to compile out the methods to do nothing in release mode. However I recommend using a log level strategy that can be configured so that you can build the logging into release modes as JOLESEN suggests. You cans use a factory pattern that will accomplish almost the same thing as compiling out the code will in terms of processor overhead. However if you are doing a real-time app and instructions are critical you need to stick with compiling out the code.


-pete
 
Thanx to both of you

The problem, it is time critical and complicated
Thats why I need lots of outputs up to the error that maybe occure. But in release version nothing of it should be done.
cause it is very laborious to write all the times

#ifdef ...
...
#endif

I would like to use something like
XTRACE (&quot;test.txt&quot;,Formatstring,ParamList)

for release I could easy define

XTRACE(....) ;

that programmer before me did lots of things with macros, and so I know that its sometimes unreadable. But some of
them I have to understand too..

and nowhere to find a doc about macros ...



Greetings Andreas
 
I don't think logging from strategic places is that expensive in terms of CPU usage. Try and create a program that logs hundred thousand lines - In many applications the overhead isn't really significant.
If you still find it's too expensive, try to simply return from the log function and measure the overhead caused by the function call itself (100.000 calls + returns is very cheap!). This would make it possible to dynamically switch logging on as Palpano suggests.
If you need to support a remote installation of your software you will eventually be in a situation where a detailed log is worth a million dollar !

A program without a log-feature is not a real program ! ;-)

What kind of application are you working on ?

/JOlesen
 
thank you for all the answers, but the right one (where can I get any stuff about macros?) was not there ;-)

I am working on planning machines of an oil-raffinery, and the algorithm, that was developed by our mathematicans is complicated, because of lots of data, lots of different structures, losts of conditions and recursive function calls, where you have to keep the heap and stack clean ...

interesting, but it is a lot...

[ponder]


Greetings Andreas
 
I think you will find all the information about macros you need in MSDN.

Search for &quot;C/C++ Preprocessor Reference&quot;

/JOlesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top