1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/*
* HybServ TS Services, Copyright (C) 1998-1999 Patrick Alken
* This program comes with absolutely NO WARRANTY
*
* Should you choose to use and/or modify this source code, please
* do so under the terms of the GNU General Public License under which
* this program is distributed.
*
* $Id: log.c,v 1.3 2001/11/12 09:50:55 asuffield Exp $
*/
#include "defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#endif
#include "config.h"
#include "dcc.h"
#include "hybdefs.h"
#include "log.h"
#include "match.h"
#include "misc.h"
#include "operserv.h"
#include "settings.h"
#include "sprintf_irc.h"
#include "sprintf_irc.h"
/*
putlog()
args: int level, char *fmt, va_alist
purpose: log 'fmt' to log file
return: none
*/
void
putlog(int level, char *format, ...)
{
FILE *fp;
time_t CurrTime;
char buf[MAXLINE];
va_list args;
/*
* Do not log anything if LogLevel equals 0 - also,
* if the given level is greater than LogLevel, do not
* log it
*/
if ((LogLevel == 0) || (LogLevel < level))
return;
if (!LogFile)
return;
if ((fp = fopen(LogFile, "a+")) == NULL)
{
#ifdef DEBUGMODE
printf("Unable to open log file: %s\n", LogFile);
#endif
return;
}
CurrTime = current_ts;
strcpy(buf, ctime(&CurrTime));
/*
* Erase the \n character that ctime() puts on the end, in order
* to concat the string we want to log
*/
buf[strlen(buf) - 1] = ' ';
fprintf(fp, "%s", buf);
va_start(args, format);
/* write the string to the log file */
vfprintf(fp, format, args);
fprintf(fp, "\n");
#ifdef DEBUGMODE
fprintf(stderr, "Logged> ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
#endif
fclose(fp);
va_end(args);
} /* putlog() */
/*
CheckLogs()
Called at midnight each day to rename the current log file
to: LogFile.YYYYMMDD, and also check if we have more than
MaxLogs log files. If so, erase the oldest one, to make
room for the newest one.
*/
void
CheckLogs(time_t unixtime)
{
char tmplog[MAXLINE],
olddate[MAXLINE];
char *currdate;
struct tm *log_tm;
time_t oldts;
DIR *dp;
struct dirent *dirp;
int lmatches,
len;
if (MaxLogs)
{
/*
* We must now check if there are MaxLogs log files
* in HPath/. If so, delete the oldest one to make
* room for the current one.
*/
if (!(dp = opendir(HPath)))
{
putlog(LOG1, "Error reading log directory: %s",
strerror(errno));
return;
}
ircsprintf(tmplog, "%s.", LogFile);
len = strlen(tmplog);
lmatches = 0;
olddate[0] = '\0';
currdate = NULL;
/*
* Go through all the files in the directory and
* pick out the ones that match "LogFile."
*/
while ((dirp = readdir(dp)))
{
if (!ircncmp(dirp->d_name, tmplog, len))
{
++lmatches;
/*
* Now check the date on the log file to see
* if its the oldest.
*/
if (!*olddate)
strcpy(olddate, dirp->d_name + len);
else
{
currdate = dirp->d_name + len;
if (atol(olddate) > atol(currdate))
strcpy(olddate, currdate);
}
}
}
if ((lmatches >= MaxLogs) && *olddate)
{
/*
* There are too many log files in the directory,
* delete the oldest one - it will be: LogFile.olddate
*/
ircsprintf(tmplog, "%s/%s.%s", HPath, LogFile, olddate);
unlink(tmplog);
}
closedir(dp);
} /* if (MaxLogs) */
/*
* Now rename the current log file. Use the TS of one
* second ago since this log file is actually yesterday's,
* because it just turned midnight.
*/
oldts = unixtime - 1;
log_tm = localtime(&oldts);
ircsprintf(tmplog, "%s.%d%02d%02d",
LogFile, 1900 + log_tm->tm_year, log_tm->tm_mon + 1, log_tm->tm_mday);
rename(LogFile, tmplog);
} /* CheckLogs() */
/*
RecordCommand()
Called when someone executes a NickServ/ChanServ/MemoServ/StatServ
command - log it and send it to opers with usermode +s
*/
void
RecordCommand(char *format, ...)
{
va_list args;
char buffer[MAXLINE * 2];
va_start(args, format);
vsprintf_irc(buffer, format, args);
va_end(args);
/* log the command */
stripformatsymbols( buffer );
putlog(LOG2, buffer);
/* send it to opers with usermode +s */
SendUmode(OPERUMODE_S, buffer);
} /* RecordCommand() */
|