#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <varargs.h>
#include <sys/file.h>

#define LOGFILE "out.x"
char *getdate(time_t timevector)
{
   char *tptr;
   tptr = ctime(&timevector);
   *(tptr + strlen(tptr) - 6) = 0;
   return (&tptr[4]);
}

void logfile(va_alist)
va_dcl
{
   va_list va;
   char *format;
   FILE *fp;
   unsigned long time_pointer = 0L;
   if ((fp = fopen(LOGFILE, "a"))) {
      flock(fileno(fp), LOCK_EX);
      va_start(va);
      format = (char *) va_arg(va, char *);
      time_pointer = time(NULL);
      fprintf(fp, "[%s] [%lu] [%d] ", getdate(time_pointer), time_pointer,
	      getpid());
      vfprintf(fp, format, va);
      va_end(va);
      fputc('\n', fp);
      fflush(fp);
      flock(fileno(fp), LOCK_UN);
      fclose(fp);
   }
}

#if 1
int main()
{
   int i = 0;
   while (i++ < 60)
      logfile("%d test", i);
   return 0;
}
#endif
