Main Page | Modules | Data Structures | File List | Data Fields

syslog.c

00001 /* $Id: syslog.c,v 1.1 2006/11/20 13:38:01 tho Exp $ */
00002 
00003 #include <u/libu_conf.h>
00004 #include <u/missing.h>
00005 #ifndef HAVE_SYSLOG
00006 
00007 #ifdef OS_WIN
00008 #include <windows.h>
00009 #include <io.h>
00010 #include <sys/locking.h>
00011 #include <errno.h>
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 
00015 void vsyslog(int priority, const char *fmt, va_list ap)
00016 {
00017     #define LIBU_WIN_LOGFILE "libu.log"
00018     enum { BUFSZ = 1024 };
00019     static FILE *df = NULL, *lock = NULL;
00020     char buf[BUFSZ];
00021     int i;
00022 
00023     /* first time open the log file and the lock file */
00024     if(df == NULL)
00025     {
00026         df = fopen(LIBU_WIN_LOGFILE, "a+");
00027         lock = fopen(LIBU_WIN_LOGFILE ".lock", "a+");
00028         if(df == NULL || lock == NULL)
00029             exit(1);
00030     }
00031 
00032     vsnprintf(buf, BUFSZ, fmt, ap);
00033 
00034     /* get the lock (i.e. lock the first byte of the lock file) */
00035     for(i = 0; 
00036         _locking(fileno(lock), _LK_NBLCK, 1) == EACCES && i < 10; ++i)
00037         Sleep(100);
00038 
00039     if(i < 10)
00040     {   /* we have the lock, write the log msg */
00041         fprintf(df, "%s\n", buf);
00042         fflush(df);
00043         /* unlock the file */
00044         _locking(fileno(lock), _LK_UNLCK, 1);
00045     } else {
00046         /* file is still locked after 10 tries, give up */
00047         ;
00048     }
00049 
00050     return;
00051 }
00052 
00053 void syslog(int priority, const char *fmt, ...)
00054 {
00055     va_list ap;
00056 
00057     va_start(ap, fmt); /* init variable list arguments */
00058 
00059     vsyslog(priority, fmt, ap);
00060 
00061     va_end(ap);
00062 }
00063 
00064 #endif /* ifdef OS_WIN */
00065 
00066 #else /* ifndef HAVE_SYSLOG */
00067 #include <syslog.h>
00068 #include <stdarg.h>
00069 void syslog(int priority, const char *fmt, ...);
00070 void vsyslog(int priority, const char *fmt, va_list args);
00071 #endif 

←Products
© 2005-2006 - KoanLogic S.r.l. - All rights reserved