Re: Local or Global ?





akarui.tomodachi@xxxxxxxxx wrote On 03/10/06 11:16,:
I have about twenty functions written in C.
Each time I call a function, it logs the status of the function
operation to a file. I use a central logging function which is called
from each function to report status into a log file. However, I am
using "sprintf" and formatting the log info into a string and sending
to my central logging routine myLogFunc().

Here is the skeleton code:

/*******************
void myFunc1()
{
char logString[100];

/* do some thing here.. */
....
....

sprintf(logString,"%s", "Hello World, everything is OK");
myLogFunc(logString);

....
}
********************/

Now question is that:
1) Should I declare the char array logString[] in every function as a
local variable ? or should I declare as a static global. I know that,
this array is local to each function and it would be disappear once the
function returns.
2) Could you please advice which one is better, local or global ?

Better than either, probably, would be to have
myLogFunc() handle the whole business:

void myFunc1() {
...
myLogFunc("%s", "Hello World, all's well");
...
}

void myLogFunc(const char *fmt, ...) {
va_arg ap;
fputs ("LOG ENTRY: ", logFILE);
va_start(ap, fmt);
vfprintf(logFILE, fmt, ap);
va_end(ap);
fputs (" (END OF LOG ENTRY)\n", logFILE);
}

--
Eric.Sosman@xxxxxxx

.



Relevant Pages