Go Premium for a chance to win a PS4. Enter to Win


Threadsafe alternatives to log4c that support logging in microseconds

Posted on 2010-09-16
Medium Priority
Last Modified: 2012-05-10
Hi guys,

Again, as per title. I need a logging solution that's thread safe and supports logging in microseconds.....
Question by:PMembrey
  • 4
LVL 32

Expert Comment

ID: 33698129
From previous question, you already have a function that can do the logging with microseconds. To make this function thread-safe, you can add take a mutex at the entry point of the function, and release the mutex at the exit point.

Below is code that shows how to use a mutex to protect your function. Code is taken from

// ************* short example *******************
/* Note scope of variable and mutex are the same */
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter=0;

/* Function C */
void functionC()
   pthread_mutex_lock( &mutex1 );
   pthread_mutex_unlock( &mutex1 );

// ******************** A sample Program ***********************

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int  counter = 0;

   int rc1, rc2;
   pthread_t thread1, thread2;

   /* Create independent threads each of which will execute functionC */

   if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
      printf("Thread creation failed: %d\n", rc1);

   if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
      printf("Thread creation failed: %d\n", rc2);

   /* Wait till threads are complete before main continues. Unless we  */
   /* wait we run the risk of executing an exit which will terminate   */
   /* the process and all threads before the threads have completed.   */

   pthread_join( thread1, NULL);
   pthread_join( thread2, NULL); 


void *functionC()
   pthread_mutex_lock( &mutex1 );
   printf("Counter value: %d\n",counter);
   pthread_mutex_unlock( &mutex1 );

Open in new window


Author Comment

ID: 33698139
this is what I have now, I was just hoping that there might be a more featureful solution already available.
LVL 32

Accepted Solution

phoffric earned 2000 total points
ID: 33698149
Couldn't you wrap the useful features of log4c with a mutex (just be careful that one log4c function doesn't call another that is wrapped with the same mutex). Or to do it without analysis, you could have a distinct mutex per log4c function. And then you just call your log4c_ts functions.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 32

Expert Comment

ID: 33698190
I don't know how complex or the performance requirements of your system is. But in one highly real-time system, having each task do its own logging resulted in unacceptable degradation of the system. So, instead, the architects just added a logging task which accepted messages in its queue. With this approach, there were naturally no thread-safe issues. This logging task did the time-stamping so that the real-time tasks didn't need the extra overhead. The time-stamping results were sufficient for our needs even though they may be a ms off.
LVL 32

Expert Comment

ID: 33698211
FYI - I came across this link on log4cplus which says the package is thread-safe.
LVL 53

Expert Comment

ID: 33698787
In your other question (http://www.experts-exchange.com/Programming/Languages/C/Q_26479755.html), it was mentioned how to use log4c in a thread safe way.

I'm curious though : are you sure you really need microsecond precision logs ? That's rarely useful, especially because the logging operation itself will likely take much more than a few microseconds.

I mentioned it in one of your other questions too : if you really need a micro-second-precision timer (from the start of the executable generally) mentioned in the logs, you can always add it. Nothing prevents you from doing that.
A micro-second precision timestamp (with date and time of day) though is a very different matter. Very few systems support something like that.

So, you might want to re-consider this ;)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question