Local or Global ?

A

akarui.tomodachi

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 ?
 
E

Eric Sosman

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);
}
 
P

pemo

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 ?

Why not define myLogFunc() as void myLogFunc(const char *) and then simply
call it like this:

myLogFunc("Hello World, everything is OK");

Then ...

void myLogFunc(const char * message)
{
FILE * file;

file = fopen(...);

fputs(message, file);

fclose(file);
}

Then you've got no need to use sprintf.

If you want to pass a message with more detail, you could easily create a
variadic version of myLogFunc(), and do the only sprintf stuff in there
using something like vsnprintf.
 
B

Ben Pfaff

Eric Sosman said:
void myFunc1() {
...
myLogFunc("%s", "Hello World, all's well");

I realize that the OP used %s gratuitously, but I don't think
there's any reason to carry it through on the follow-up.
 
P

pemo

pemo said:
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 ?

Why not define myLogFunc() as void myLogFunc(const char *) and then
simply call it like this:

myLogFunc("Hello World, everything is OK");

Then ...

void myLogFunc(const char * message)
{
FILE * file;

file = fopen(...);

fputs(message, file);

fclose(file);
}

Then you've got no need to use sprintf.

If you want to pass a message with more detail, you could easily
create a variadic version of myLogFunc(), and do the only sprintf
stuff in there using something like vsnprintf.

Maybe something like this - *disclaimer*: not very well tested or thought
about!!!

#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <stdarg.h>

void myLogFunc(const char * format, ...)
{
// size = guess how much we need.
//
int size = 10;

int n = 0;

char * p = NULL;

va_list va;

if((p = malloc(size)) != NULL)
{
for(;;)
{
// Try to print into what we've got.
//
va_start(va, format);

n = vsnprintf(p, size, format, va);

va_end(va);

// If it worked, god - write out the string.
//
if (n > -1 && n < size)
{
FILE * file = NULL;

if((file = fopen("text.txt", "a")) != NULL)
{
fputs(p, file);

fclose(file);
}

else

{
fprintf(stderr, "Couldn't write '%s' to the log file\n",
p);
}

free(p);
}

// Try again with more space.
//
else

{
if((p = realloc(p, ++size)) == NULL)
{
abort();
}
}

return;
}
}

else

{
// What else can we do!
//
abort();
}
}


int main(void)
{

myLogFunc("%d %s\n", 10, "Boo hoo hoo");

return 0;
}
 
E

Eric Sosman

Ben Pfaff wrote On 03/10/06 12:23,:
I realize that the OP used %s gratuitously, but I don't think
there's any reason to carry it through on the follow-up.

It's silly as used here, yes. But I kept it because
I wanted to show a myLogFunction() that could actually do
formatting, rather than just handle an already-formatted
single string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top