Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!samsung!crackers!jjmhome!smds!rh From: rh@smds.UUCP (Richard Harter) Newsgroups: comp.lang.c Subject: Re: error handling techniques? Summary: Simpler than it sounds like Message-ID: <239@smds.UUCP> Date: 13 Nov 90 06:29:17 GMT References: <1990Nov2.205831.23696@elroy.jpl.nasa.gov> <234@smds.UUCP> <4246@goanna.cs.rmit.oz.au> Organization: SMDS Inc., Concord, MA Lines: 58 In article <4246@goanna.cs.rmit.oz.au>, ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes: > In article <234@smds.UUCP>, rh@smds.UUCP (Richard Harter) writes: > > The code maintains a history of the last 128 function calls in a circular > > buffer; this information is dumped in the error report. > I wouldn't mind doing something like this, but how do you do it? > Have you a set of macros to ease the job, or what? > Is _every_ function call included, or only selected ones? > I tend to write recursive code, the trouble with that is that > when things go wrong all the calls in the buffer tend to be to > the same function (or a small set of mutually recursive functions), > have you found a good way around that? Actually the implementation I use is brutally simple. I set up a global array of char pointers and a global integer used as an index into the array, e.g. char *TR[128]; int TI; An initialization routine zeroes out the array and sets TI=0; The standard include file contains the macro #define trace(foo) TR[TI--]=foo;if (TI<0) TI =127;TR[TI]=0 The first statement in each routine (of interest) invokes trace with the name of the routine, e.g. trace("somefunc"); It's not terribly sophisticated, but it's very useful. As you note the buffer can fill up with a few names if you are doing a lot of recursion (or are calling a routine within a loop). A simple technique for dealing with this (which I've never bothered to implement) is to add a count array and increment the count if the pointers are equal. Something like the following should work: #define trace(foo) \ if (TR[TI] != foo) {\ TI--;\ if (TI<0) TI=127;\ TR[TI] = foo;\ TRCNT[TI]=1;\ }\ else TRCNT[TI]++ The error dump routine has the requisite code for going through the strings, getting their lengths, and printing out the array in nice neat columns starting at the right place. -- Richard Harter, Software Maintenance and Development Systems, Inc. Net address: jjmhome!smds!rh Phone: 508-369-7398 US Mail: SMDS Inc., PO Box 555, Concord MA 01742 This sentence no verb. This sentence short. This signature done.