log.c
pdirt/drv/src/log.c · 5.2 KB
From the archive pDirt-1_tar.gz
Download the archive « Back to the archive contents
#include "kernel.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include "log.h"
#include "bprintf.h"
int syslogfd = -1;
/******************************************************************************
** openMudSyslog: Opens a syslog file with the given name, in the given mode.
**
** Input Parameters:
** char *filename : Name & path of the syslogfile
** char *mode : Mode in which to open the syslog file.
** valid modes are:
** r = readonly
** w = writeonly
** x = read/write
** t = truncate
** a = append
** If the file doesnt exsist, the file is created.
**
** Output Parameters:
** int : The fd number of the syslog or a negative number if open failed.
****************************************************************************/
int openMudSyslog(char *fn,char *mode,int which)
{ int modebits = O_CREAT;
int i;
int fd;
if (syslogfd != -1 && which == MUDLOG)
closeMudSyslog();
if (mode != NULL)
{ for (i = 0; mode[i] != '\0'; i++)
switch (mode[i]) {
case 'a' : modebits |= (O_CREAT|O_APPEND); break;
case 't' : modebits |= O_TRUNC; break;
case 'r' : modebits |= O_RDONLY; break;
case 'w' : modebits |= (O_CREAT|O_WRONLY); break;
case 'x' : modebits |= (O_CREAT|O_RDWR); break;
default:
}
}
fd = open(fn,modebits,S_IRUSR|S_IWUSR);
if (fd < 0)
{ perror("SYSLOG");
}
return fd;
}
/*****************************************************************************
** closeMudSyslog : Closes an syslog file
**
** Input Parameters:
** none
** Output Parameters:
** none
***************************************************************************/
void closeMudSyslog(void)
{ if (syslogfd > 0)
{ close(syslogfd);
syslogfd = -1;
}
}
/****************************************************************************
** mudSyslog : Writes a log message to the syslog file
**
** input parameters:
** int which : which logfile.
** char *pattern : normal printf pattern with %s/%d/%f notations.
** ... : a number of parameters to fill in the required fields
** in the pattern format.
** output parameters:
** none
**************************************************************************/
void mudSyslog(int which, char *pattern,...)
{ va_list pvar;
char format[512];
char line[1024];
time_t thistime = time(NULL);
char *z = ctime(&thistime);
int fd;
va_start (pvar,pattern);
z += 4;
z[15] = '\0';
sprintf(format,"%s| %s\n\r",z,pattern);
vsprintf(line,format,pvar);
switch (which) {
case BUGLOG:
case TYPOLOG:
fd = openMudSyslog(BUGSYSLOG,"wa",which);
if (fd >= 0)
{ write(fd,line,strlen(line+1));
close(fd);
}
else
write(syslogfd,line,strlen(line+1));
break;
case SUGGLOG:
fd = openMudSyslog(SUGGESTLOG,"wa",which);
if (fd >= 0)
{ write(fd,line,strlen(line+1));
close(fd);
}
else
write(syslogfd,line,strlen(line+1));
break;
case ABERLOG:
fd = openMudSyslog(ABERCHATLOG,"wa",which);
if (fd >= 0)
{ write(fd,line,strlen(line+1));
close(fd);
}
else
write(syslogfd,line,strlen(line+1));
break;
default:
if (syslogfd >= 0)
write(syslogfd,line,strlen(line+1));
/* else
fprintf(stderr,"%s",line);*/
break;
}
va_end(pvar);
}
void fwerror(char *fn)
{ mudSyslog(MUDLOG,"FILE: Could not open %s for writing.",fn);
bprintf("Could not open temporary file.\n");
}
int open_logfile(char *fn,Boolean clear_log)
{
if (clear_log)
syslogfd = openMudSyslog(fn,"wt",MUDLOG);
else
syslogfd = openMudSyslog(fn,"wa",MUDLOG);
if (syslogfd >= 0)
{ dup2(syslogfd,fileno(stderr));
close(syslogfd);
syslogfd = fileno(stderr);
}
return (syslogfd >= 0) ? 0 : -1;
}
void close_logfile()
{ closeMudSyslog();
}
void progerror(char *name)
{ mudSyslog(MUDLOG,"PERROR %s: [%d] %s",name, errno, strerror(errno));
}
void mudlog(char *format,...)
{ char buff[4096];
va_list pvar;
va_start (pvar,format);
vsprintf(buff,format,pvar);
mudSyslog(MUDLOG,buff);
va_end(pvar);
}
void buglog(char *format,...)
{ char buff[4096];
char pattern[512];
va_list pvar;
va_start (pvar,format);
sprintf(pattern,"%s",format);
vsprintf(buff,pattern,pvar);
mudSyslog(BUGLOG,buff);
va_end(pvar);
}
void suggestlog(char *format,...)
{ char buff[4096];
char pattern[512];
va_list pvar;
va_start (pvar,format);
sprintf(pattern,"%s",format);
vsprintf(buff,pattern,pvar);
mudSyslog(SUGGLOG,buff);
va_end(pvar);
}
void aberlog(char *format,...)
{ char buff[4096];
char pattern[512];
va_list pvar;
va_start (pvar,format);
sprintf(pattern,"%s",format);
vsprintf(buff,pattern,pvar);
mudSyslog(ABERLOG,buff);
va_end(pvar);
}