Local or Global ?

Discussion in 'C Programming' started by akarui.tomodachi@gmail.com, Mar 10, 2006.

  1. Guest

    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 ?
    , Mar 10, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

    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, Mar 10, 2006
    #2
    1. Advertising

  3. pemo Guest

    wrote:
    > 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.

    --
    ==============
    *Not a pedant*
    ==============
    pemo, Mar 10, 2006
    #3
  4. Ben Pfaff Guest

    Eric Sosman <> writes:

    > 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.

    > ...
    > }


    --
    "If I've told you once, I've told you LLONG_MAX times not to
    exaggerate."
    --Jack Klein
    Ben Pfaff, Mar 10, 2006
    #4
  5. pemo Guest

    pemo wrote:
    > wrote:
    >> 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;
    }



    --
    ==============
    *Not a pedant*
    ==============
    pemo, Mar 10, 2006
    #5
  6. Eric Sosman Guest

    Ben Pfaff wrote On 03/10/06 12:23,:
    > Eric Sosman <> writes:
    >
    >
    >> 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.


    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.

    --
    Eric Sosman, Mar 10, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karim
    Replies:
    1
    Views:
    756
    George Ter-Saakov
    Jun 26, 2003
  2. =?Utf-8?B?WVNVVA==?=

    Access local port or Running local exe file

    =?Utf-8?B?WVNVVA==?=, Jan 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    498
    =?Utf-8?B?WVNVVA==?=
    Jan 14, 2006
  3. Jim in Arizona
    Replies:
    8
    Views:
    4,957
    Jim in Arizona
    Jan 24, 2006
  4. lurker
    Replies:
    1
    Views:
    689
    Disco Octopus
    Apr 5, 2005
  5. Lyndon
    Replies:
    1
    Views:
    505
    Leonard Blaisdell
    Jul 25, 2005
Loading...

Share This Page