• C

Threadsafe alternatives to log4c that support logging in microseconds

Hi guys,

Again, as per title. I need a logging solution that's thread safe and supports logging in microseconds.....
PMembreyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
phoffricConnect With a Mentor Commented:
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.
0
 
phoffricCommented:
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
     http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#SYNCHRONIZATION


// ************* 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 );
   counter++
   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;

main()
{
   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); 

   exit(0);
}

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

Open in new window

0
 
PMembreyAuthor Commented:
this is what I have now, I was just hoping that there might be a more featureful solution already available.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
phoffricCommented:
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.
0
 
phoffricCommented:
FYI - I came across this link on log4cplus which says the package is thread-safe.
     http://log4cplus.sourceforge.net/index.html
0
 
Infinity08Commented:
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 ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.