/********************************************************
* File: logwrite.c
* Created at Sun Jan 28 22:10:30 MSK 2001 by raorn // raorn@binec.ru
* Logging stuff
* $Id: logwrite.c,v 1.4 2001/11/26 01:53:06 raorn Exp $
*******************************************************/
#include <machine/defs.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDARG_H
# include <stdarg.h>
#endif
#include <stdlib.h>
#ifdef HAVE_STDARG_H
# include <stdarg.h>
#endif
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#elif HAVE_SYS_TIME
# include <sys/time.h>
#else
# include <time.h>
#endif
#ifdef USE_SYSLOG
#define SYSLOG_NAMES
#include <syslog.h>
bool usesyslog;
int syslogpri[] = {
LOG_INFO, /* SYSTEMINFO */
LOG_ERR, /* SYSTEMERR */
LOG_INFO, /* TOSSINGINFO */
LOG_ERR, /* TOSSINGERR */
LOG_INFO, /* MISCINFO */
LOG_DEBUG, /* DEBUG */
LOG_INFO, /* AREAFIX */
LOG_INFO, /* ACTIONINFO */
LOG_ERR /* USERERR */
};
#endif
#include "logwrite.h"
static FILE *logfh;
static ulong LogLevel;
bool OpenLogfile(uchar *logfile, ulong level)
{
if (!logfile[0])
return FALSE;
LogLevel = level;
#ifdef USE_SYSLOG
if (logfile[0] == '$') {
int i;
for (i=0; facilitynames[i].c_name; i++)
if (!stricmp(logfile+1, facilitynames[i].c_name))
break;
if (facilitynames[i].c_name == NULL) {
fprintf(stderr, "Invalid facility name %s\n", logfile);
return (FALSE);
}
usesyslog = TRUE;
openlog("CrashEcho", 0, facilitynames[i].c_val);
return (TRUE);
}
#endif
if (!(logfh = fopen(logfile, "at"))) {
fprintf(stderr, "Failed to open logfile %s\n", logfile);
fprintf(stderr, "Error: %s\n", strerror(errno));
return (FALSE);
}
return (TRUE);
}
void CloseLogfile(void)
{
#ifdef USE_SYSLOG
if (usesyslog) {
closelog();
usesyslog = FALSE;
return;
}
#endif
fclose(logfh);
}
void LogWrite(ulong level, ulong category, uchar * fmt, ...)
{
va_list args;
time_t t;
struct tm *tp;
uchar str[2048] = {0};
if (level > LogLevel)
return;
if (level == 0)
LogWrite(6, DEBUG, "*** Warning: Loglevel is 0!!! ***");
if (fmt[0] == 0) {
printf("\n");
return;
}
time(&t);
tp = localtime(&t);
strftime(str, 20, "%d %b %y %H:%M:%S ", tp);
va_start(args, fmt);
vsnprintf(str+19, 2048, fmt, args);
va_end(args);
printf(str+19);
printf("\n");
#ifdef USE_SYSLOG
if (usesyslog) {
syslog(syslogpri[category], str+19);
return;
}
#endif
fprintf(logfh, str);
fprintf(logfh, "\n");
}
syntax highlighted by Code2HTML, v. 0.9.1