Note I have not looked at axis2 code, so I am talking canceptually....
I am assuming there is a point where there is some initialization going on
prior to anyone using the trace that is tied to configuration. At that
time, one can set a global file handle to the log file by calling
trace_init() which simply contains the following code:
FILE * traceFp = fopen(logFile, "w+");
setvbuf(traceFp, NULL, _IOFBF, (size_t)(8*1024));
The above assumes trace records are no longer than 8K. This will ensure no
buffering takes place.
Then, in the actual routine that performs the trace:
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <pthread.h>
void trace(const char *fmt, ... )
{
char tempTimeStr[200];
struct timeval t;
struct tm *lTime;
pthread_id_np_t tid;
va_list args;
char myfmt[8*1024];
if (traceFp == NULL)
return;
gettimeofday(&t, NULL);
lTime = localtime((const time_t *)&t.tv_sec);
strftime(tempTimeStr, 100, "%d/%b/%Y %H:%M:%S", lTime);
tid = pthread_getthreadid_np();
sprintf(myfmt, "<%s.%03ld %016llx> %s", tempTimeStr, t.tv_usec/1000,
*((long long *)&tid), fmt);
va_start(args, fmt);
vfprintf(traceFp, myfmt, args);
va_end(args);
fflush(traceFp);
}
The above code is thread-safe and not only prints out the date and time
(included milli-seconds), but the thread ID also.
Nadir K. Amra
Samisa Abeysinghe <[EMAIL PROTECTED]> wrote on 02/03/2006
10:51:11 PM:
> Hi All,
> Once we have the threads in place, we have to ensure that our
> logging mechanism is thread safe.
> May be we can learn some stuff from Apache httpd code.
>
> Unfortunately, we are not lucky as much as Java guys, we do not have
> a log4j equivalent. We have log4c http://log4c.sourceforge.net/. But I
> am not convinced of its maturity and licence.
> Thoughts please...
> Thanks,
> Samisa...