Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #534

Thread safe logging (or protecting global variables)

From Jef Driesen <jefdriesen@hotmail.com.invalid>
Newsgroups comp.programming.threads
Subject Thread safe logging (or protecting global variables)
Message-ID <Co89q.7290$cb.3785@newsfe14.ams2> (permalink)
Organization telenet.be
Date 2011-09-05 20:19 +0200

Show all headers | View raw


Hi,

I have a C library that is supposed to be thread-safe under the
condition that you access each library object only from a single thread.
This is done by ensuring that objects do not share any state (e.g. no
global variables) and where necessary reentrant variants of certain C
library functions are used (e.g. localtime_r).

Now, I want to add a logging feature for debugging purposes. The idea
would be to implement a logging infrastructure where the amount of
output can be controlled with a loglevel parameter (defaulting to no
output) and redirected to a file (defaulting to stderr), like in the
pseudo code below:

static int g_loglevel = 0;
static FILE *g_logfile = stderr;
/* Some other globals used for the logging here */

message_set_loglevel (int loglevel)
{
    g_loglevel = loglevel;
}

message_set_logfile (const char *logfile)
{
    /* Open/close the new/old logfiles here. */
    g_logfile = ...;
}

message (const char *fmt, ...)
{
    va_list ap;

    /* This is just an example, the real code does more
       and relies on a number of extra global variables. */
    if (g_loglevel) {
       va_start (ap, fmt);
       vfprintf (g_logfile, fmt, ap);
       va_end (ap);
    }
}

Obviously, this is not thread-safe due to the use of global variables.
Thus my next idea was to add a global mutex (e.g. pthread_mutex_t on
unix or CRITICAL_SECTION on windows) protecting these global variables.
But how do I initialize the mutex, considering that my library doesn't
have (or need) any initialization functions? For the unix pthread there
is PTHREAD_MUTEX_INITIALIZER, but as far as I know there is no such
thing for a windows critical section? How can this problem be solved?

Would it be an acceptable solution to mark the message_set_*()
functions as not thread-safe and require applications to call them
before/after any threads are created/destroyed? (For my use case such a
restriction would be acceptable, although I'm not sure it would be
fully thread-safe.) Then those functions could initialize the mutex. Or will 
that cause any trouble?

Can I even go one step futher and use simple a simple flag (like the
g_loglevel variable above) that can only be modified from the
message_set_*() functions, and which is only checked in the message()
function but not modified.

Jef

Back to comp.programming.threads | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-05 20:19 +0200
  Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-05 20:30 +0200
    Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-06 18:48 +0200
      Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-07 10:34 +0200
        Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-08 22:00 +0200
          Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-09 18:13 +0200
            Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-13 17:58 +0200
              Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-13 13:18 -0700
                Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-14 07:16 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 13:18 -0700
                Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-14 09:33 +0200
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-14 13:17 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 13:03 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 13:19 -0700
                Re: Thread safe logging (or protecting global variables) "James" <no@spam.invalid> - 2011-09-14 19:19 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 21:03 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-15 13:12 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-15 12:24 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-15 22:22 +0200
                Re: Thread safe logging (or protecting global variables) "James" <no@spam.invalid> - 2011-09-15 13:33 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-15 15:36 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-15 15:36 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-16 16:28 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-16 15:06 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-17 12:50 +0200
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-15 12:50 +0200
              Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-14 09:17 +0200
  Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-08 16:28 -0700
    Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-08 16:33 -0700
      Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-08 20:55 -0700
    Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-12 00:45 -0700

csiph-web