syslog.c
pdirt/drv/src/syslog.c · 7.4 KB
From the archive pDirt-1_tar.gz
Download the archive « Back to the archive contents
/****************************************************************************
** pDirt syslog.
** Original idea and code by Illusion . Slightly modified and rewritten by
** Marty 1996
***************************************************************************/
#include "kernel.h"
#include "utils.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include "pflags.h"
#include "parse.h"
#include "log.h"
#include "bprintf.h"
#define A_DAY 24*60*60
char *days[] = {
"today", "yesterday", "daybefore", "week",
"fortnight", "month", TABLE_END
};
time_t sectime[] = {
0, A_DAY, 2*A_DAY, 6*A_DAY,
13*A_DAY, 30*A_DAY, 0
};
/* Tables to look up a special color by a special log entry */
char *idtable[] = {
"RECOVER", "SYSTEM", "UPDATE", "SIGNAL",
"GAME", "FILE", "ERROR", "STEAL",
"FROB", "WARN", "BACK", "PARSE",
"QUEST", "CONN", "JAIL", "SOCKET",
"SLAIN", "ZAP", "HEAL", "DEBUG",
"TYPO", "BUG", "LOG", "PUNT",
"SUMMON", "BAN", "EXORCISE", "UAF",
"LFLAGS", "PFLAGS", "MFLAGS", "RESET",
"PK", "ALIAS", "TOUT", "GDBM",
"BOOTSTRAP", "SUGGESTION", "BECOME", "MUDDNS",
"TESTRUN",
TABLE_END
};
char *idcolors[] = {
"&+w", "&+w", "&+w", "&=YB",
"&+w", "&+w", "&=LR", "&+w",
"&+w", "&=WB", "&+w", "&+w",
"&+w", "&+w", "&=MB", "&+w",
"&+w", "&+w", "&+w", "&=RB",
"&=WC", "&=WC", "&=WC", "&+w",
"&+w", "&=YR", "&+w", "&=WG",
"&=LW", "&=BW", "&=MW", "&+w",
"&-R", "&-B", "&=LY", "&-C",
"&+L", "&=WC", "&+w", "&=WB",
"&=YR",
TABLE_END /* Not really needed, but what the heck.. */
};
void seek_line(FILE *fp,int line);
int count_lines(char *fn);
void find_first(FILE *fp, char *pattern);
void beautify_log(FILE *dest, char *buffer);
void lookup_time_index(FILE *logfp, FILE *tempfp, time_t start_date);
void showlog(char *fn);
A_COMMAND(syslogcom)
{ showlog(LOG_FILE);
}
void showlog(char *logfn)
{ char *timestr;
FILE *fp, *logfp;
char tfn[100];
char searchpat[100];
char buffer[400];
char cmpbuf[400];
int i,max_line;
time_t t;
if (!ptstflg(mynum,PFL_SYSLOG))
{ erreval();
return;
}
sprintf(tfn,"%s/TMP/Syslog.%s",data_dir,pname(mynum));
if ((fp = fopen(tfn,"w")) == NULL)
{ fwerror(tfn);
bprintf("Could not open temporary file.\n");
return;
}
if ((logfp = fopen(logfn,"rt")) == NULL)
{ mudlog("FILE: Error opening syslog reader.");
bprintf("Could not find syslog.\n");
fclose(fp);
unlink(tfn);
return;
}
/** Default is: show all entries of today
**/
if (EMPTY(txt1))
{ timestr = ctime(&global_clock);
timestr += 4;
timestr[8] = '\0';
strcpy(searchpat,timestr);
}
else
{ getreinput(globalbuf);
brkword();
i = tlookup(wordbuf, days);
if (i >= 0)
{ t = global_clock - sectime[i];
if (i > 2)
{ lookup_time_index(logfp,fp,t);
strcpy(searchpat," ");
}
else
{ timestr = ctime(&t);
timestr += 4;
timestr[6] = '\0';
strcpy(searchpat,timestr);
find_first(logfp,searchpat);
}
/*
timestr = ctime(&t);
timestr += 4;
timestr[6] = '\0';
strcpy(searchpat,timestr);
if (i > 2)
{ find_first(logfp,searchpat);
while (feof(logfp) && t <= global_clock)
{ fprintf(fp,"==SYSLOG== No log entries on %s\n",timestr);
t += A_DAY;
timestr = ctime(&t);
timestr +=4;
timestr[6] = '\0';
strcpy(searchpat,timestr);
find_first(logfp,searchpat);
}
strcpy(searchpat," ");
}
else
find_first(logfp,searchpat);
*/
}
else if (wordbuf[0] == '*')
{ seek_line(logfp,0);
strcpy(searchpat," ");
}
else if ((i = atoi(wordbuf)) > 0)
{ max_line = count_lines(logfn);
if (i > max_line)
{ bprintf("invalid size, Narrowing field to last 10\n");
seek_line(logfp,max_line - 10);
}
else
seek_line(logfp,max_line - i);
strcpy(searchpat," ");
}
else if (wordbuf[0] == '-' && (wordbuf[1] == 'd' || wordbuf[1] == 'D'))
{ int days;
days = atoi(wordbuf+2);
t = global_clock - (A_DAY *days);
lookup_time_index(logfp,fp,t);
i = (-2);
strcpy(searchpat," ");
}
else
strcpy(searchpat,globalbuf);
}
if (feof(logfp))
{ fprintf(fp,"==SYSLOG== No entries for %s\n",wordbuf);
}
uppercase(searchpat);
while (!feof(logfp))
{ fgets(buffer,sizeof(buffer),logfp);
strcpy(cmpbuf,buffer);
if (strstr(uppercase(cmpbuf),searchpat))
{ if (!feof(logfp))
beautify_log(fp,buffer);
}
}
fclose(fp);
fclose(logfp);
read_file(tfn,NULL,True,NULL);
unlink(tfn);
}
/** Seeks the line, counting from the start
**/
void seek_line(FILE *fp,int line)
{ int i;
char tmp[400];
fseek(fp,0L,SEEK_SET);
for (i=0;i < line; i++)
fgets(tmp,sizeof(tmp),fp);
}
/** Count total number of lines in a file. -1 if the file could not be opened.
**/
int count_lines(char *fn)
{ FILE *fp;
int i = 0;
char tmp[400];
fp = fopen(fn,"rt");
if (fp == NULL)
return -1;
while (fgets(tmp,sizeof(tmp),fp))
i++;
fclose(fp);
return i;
}
/** Find first occurance of a pattern
** Or EOF if no pattern match was found.
**/
void find_first(FILE *fp, char *pattern)
{ char buffer[200], cmpbuf[200];
uppercase(pattern);
fseek(fp,0L,SEEK_SET);
while (!feof(fp))
{ fgets(buffer,sizeof(buffer),fp);
strcpy(cmpbuf,buffer);
if (strstr(uppercase(cmpbuf),pattern))
{ /* beautify_log(fp,buffer);*/
return;
}
}
}
/** Checks the first word after the timestamp with the table, if it isn't in
** there, then no colorcoding is applied.
**/
void beautify_log(FILE *dest, char *buffer)
{ char *s = buffer;
char id[20];
int i,n;
if (strchr(buffer,'|') == NULL)
return;
s+=17;
for (i = 0; isalpha(s[i]); i++)
id[i] = s[i];
id[i] = 0;
n = tlookup(id,idtable);
if (n >= 0)
{
for (i = 0; i < 17; i++)
fprintf(dest,"%c",buffer[i]);
fprintf(dest,"%s",idcolors[n]);
for (; isalpha(buffer[i]) ; i++)
fprintf(dest,"%c",buffer[i]);
fprintf(dest,"&+w");
fprintf(dest,"%s",buffer+i);
}
else
fputs(buffer,dest);
}
void lookup_time_index(FILE *logfp, FILE *tempfp, time_t start_date)
{ time_t t;
char *timestr;
char searchpat[30];
t = start_date;
timestr = ctime(&t);
timestr += 4;
timestr[6] = '\0';
strcpy(searchpat,timestr);
find_first(logfp,searchpat);
while (feof(logfp) && t <= global_clock)
{ fprintf(tempfp,"==SYSLOG== No log entries on %s\n",timestr);
t += A_DAY;
timestr = ctime(&t);
timestr +=4;
timestr[6] = '\0';
strcpy(searchpat,timestr);
find_first(logfp,searchpat);
}
}
A_COMMAND(buglogcom)
{ showlog(BUGSYSLOG);
}
A_COMMAND(suggestlogcom)
{ showlog(SUGGESTLOG);
}
A_COMMAND(aberchatlogcom)
{ showlog(ABERCHATLOG);
}