Re: Local or Global ?
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Fri, 10 Mar 2006 11:39:36 -0500
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
.
- Follow-Ups:
- Re: Local or Global ?
- From: Ben Pfaff
- Re: Local or Global ?
- References:
- Local or Global ?
- From: akarui . tomodachi
- Local or Global ?
- Prev by Date: Re: Pointer
- Next by Date: Re: Multiple Assignment Evaluation Debate
- Previous by thread: Local or Global ?
- Next by thread: Re: Local or Global ?
- Index(es):
Relevant Pages
|
|