Threadsafe alternatives to log4c that support logging in microseconds

Posted on 2010-09-16
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 500 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.
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

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 (, 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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now