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

syslog.c

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

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