vsyslog.c

00001 #include <u/libu_conf.h>
00002 #include <u/missing.h>
00003 
00004 /*
00005  * vsyslog(3) drop-in for Windows and Minix
00006  */ 
00007 
00008 #if !defined(HAVE_VSYSLOG)
00009 #if defined(OS_WIN)
00010 
00011 #include <windows.h>
00012 #include <io.h>
00013 #include <sys/locking.h>
00014 #include <errno.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 
00018 void vsyslog(int priority, const char *fmt, va_list ap)
00019 {
00020 #define LIBU_WIN_LOGFILE "libu.log"
00021     enum { BUFSZ = 1024 };
00022     static FILE *df = NULL, *lock = NULL;
00023     char buf[BUFSZ];
00024     int i;
00025 
00026     /* first time open the log file and the lock file */
00027     if(df == NULL)
00028     {
00029         df = fopen(LIBU_WIN_LOGFILE, "a+");
00030         lock = fopen(LIBU_WIN_LOGFILE ".lock", "a+");
00031         if(df == NULL || lock == NULL)
00032             exit(1);
00033     }
00034 
00035     vsnprintf(buf, BUFSZ, fmt, ap);
00036 
00037     /* get the lock (i.e. lock the first byte of the lock file) */
00038     for(i = 0; 
00039         _locking(fileno(lock), _LK_NBLCK, 1) == EACCES && i < 10; ++i)
00040         Sleep(100);
00041 
00042     if(i < 10)
00043     {   /* we have the lock, write the log msg */
00044         fprintf(df, "%s\n", buf);
00045         fflush(df);
00046         /* unlock the file */
00047         _locking(fileno(lock), _LK_UNLCK, 1);
00048     } else {
00049         /* file is still locked after 10 attempts, give up */
00050         ;
00051     }
00052 
00053     return;
00054 }
00055 
00056 #else   /* !OS_WIN */
00057 
00058 #include <stdarg.h>
00059 #include <stdio.h>
00060 
00061 /* Try with this as last resort (MINIX and QNX for SH target will be glad). */
00062 void vsyslog(int priority, const char *fmt, va_list args)
00063 {
00064     char buf[1024];
00065 
00066     (void) vsnprintf(buf, sizeof buf, fmt, args);
00067     syslog(priority, "%s", buf);
00068 
00069     return;
00070 }
00071 
00072 #endif  /* OS_WIN */
00073 
00074 #else   /* HAVE_VSYSLOG */
00075 
00076 #include <syslog.h>
00077 #include <stdarg.h>
00078 void vsyslog(int priority, const char *fmt, va_list args);
00079 
00080 #endif  /* !HAVE_VSYSLOG */

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