[LC++]Producing a log of routine entry and exit

Quang Nguyen (Ngo) quang at tapeware.com
Fri Aug 30 03:36:07 UTC 2002


I would recommend using some kind of macros to handle logging/tracing, for 
example:

#if DEBUG
  #define TRACE0(x,msg)			MyTraceFunc(x,msg)
  #define TRACE1(x,msg,p1)		MyTraceFunc(x,msg,p1)
  #define TRACE2(x,msg,p1,p2)		MyTraceFunc(x,msg,p1,p2)
  #define ....
#else
  #define TRACE0(x,msg)
  #define TRACE1(x,msg,p1)
  ...
#endif

x = trace mask value so you can filter out certain trace messages
p1 = parameter 1
p2 = parameter 2
etc...

void MyTraceFunc(unsigned int traceType, const char *msgFormat, ...)
{
	va_list ap;

	va_start(ap, msgFormat);

	switch (traceType)
	{
		case TTYPE_ERROR:
			vprintf(...);
			or vsnprintf(...), etc...
			blah, blah...
			break;

		case TTYTPE_CRITICAL:
			blah, blah...
			break;

	.	default:
			...
			break;

	}

	va_end(ap);
}

Just some idea...

--
Quang

	


On Thursday 29 August 2002 07:32 am, Dr Mark H Phillips wrote:
> Hi,
>
> For debugging purposes, I wished to provide a standard means of
> logging the entry into, and exit from, each of my routines.  This
> log should give the name of the routine, and calling signature.
> On entry it should print the input parameters, and on exit, the
> output parameters.
>
> My current way of doing this is like this:
>
> int myRoutine(string& retString, int a, char b) {
>   cout<<"entering myRoutine(string&, int, char)\n"
>       <<"  second param: "<<a<<'\n'
>       <<"  third  param: "<<b<<endl;
>   int ret;
>
>   // do stuff here
>   // including some things like "goto preExit;"
>
> preExit:
>   cout<<"exiting myRoutine(string&, int, char)\n"
>       <<"  first param: "<<retString<<'\n'
>       <<"       return: "<<ret<<endl;
>   return ret;
> }
>
> The problem with this is that it requires the use of gotos
> which generally should be avoided.  It also means I can't
> create any objects "on the fly" before preExit, or I get
> an error like:
>
> parsing.h: In function `bool moat::getToken(int &, string &)':
> parsing.h:277: jump to label `preExit'
> parsing.h:248:   from here
> parsing.h:274:   crosses initialization of `class string myString'
>
> Now I can think of a range of alternative solutions to this
> kind of logging, but they each have their drawbacks:
>
> 1. Same as above, but replace every instance of "goto preExit;"
> with a local copy of the code that currently follows preExit.  This
> would eliminate gotos, but would make routines very verbose, and
> would be wasteful, both in terms of program size, and in terms of
> program writing and maintenance.
>
> 2. Create general routines "entryLog(...)" and "exitLog(...)".  Call
> entryLog(...) at the start.  Then do as in 1, except that "goto preExit"
> is replaced by a call to exitLog(...).  This is less wasteful than 1,
> but still somewhat so.  And the parameters of these calls need to be
> very general to cater for all kinds of uses - very tricky to get right,
> and involving lots of template variables and function overloading.
>
> 3. Create a "RoutineLogger" object, instanciated at the beginning of
> its routine, and told information about the routine it is responsible
> for logging.  It has the same scope as the routine it is representing,
> so it will be destructed just before the routine exits.  In this way
> it will automatically be called at the right time without the need for
> gotos.  This solution sounds really good, except it's very hard to
> properly pass it the information needed so it can log properly.
>
>
> Any thoughts?
>
> Cheers,
>
> Mark.


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________




More information about the tuxCPProgramming mailing list