?
Solved

Threadsafe alternatives to log4c that support logging in microseconds

Posted on 2010-09-16
6
Medium Priority
?
1,237 Views
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.....
0
Comment
Question by:PMembrey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 32

Expert Comment

by:phoffric
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
     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
 

Author Comment

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

Accepted Solution

by:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:phoffric
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.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33698211
FYI - I came across this link on log4cplus which says the package is thread-safe.
     http://log4cplus.sourceforge.net/index.html
0
 
LVL 53

Expert Comment

by:Infinity08
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 ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use conditional statements in the C programming language.

719 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