]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanservlog.c
A4STATS: remove E style escapes and switch to createtable for indices
[irc/quakenet/newserv.git] / chanserv / chanservlog.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include "chanserv.h"
8 #include "../core/hooks.h"
9 #include "../core/error.h"
10
11 int logfd;
12
13 /* When we get a sigusr1, reopen the logfile */
14 void cs_usr1handler(int hooknum, void *arg) {
15 Error("chanserv",ERR_INFO,"Reopening logfile.");
16
17 if (logfd>=0)
18 close(logfd);
19
20 logfd=open("chanservlog",O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR);
21 }
22
23 void cs_initlog() {
24 logfd=open("chanservlog",O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR);
25 registerhook(HOOK_CORE_SIGUSR1, cs_usr1handler);
26 }
27
28 void cs_closelog() {
29 if (logfd>=0)
30 close(logfd);
31 deregisterhook(HOOK_CORE_SIGUSR1, cs_usr1handler);
32 }
33
34 void cs_log(nick *np, char *event, ... ) {
35 char buf[512];
36 char buf2[1024];
37 char userbuf[512];
38 va_list va;
39 char timebuf[TIMELEN];
40 int len;
41 time_t now;
42
43 if (logfd<0)
44 return;
45
46 va_start(va,event);
47 vsnprintf(buf,512,event,va);
48 va_end(va);
49
50 if (np) {
51 snprintf(userbuf,511,"%s!%s@%s [%s%s] ",np->nick,np->ident,np->host->name->content,
52 getreguserfromnick(np)?"auth ":"noauth",getreguserfromnick(np)?getreguserfromnick(np)->username:"");
53 } else {
54 userbuf[0]='\0';
55 }
56
57 now=time(NULL);
58 strftime(timebuf,sizeof(timebuf),Q9_LOG_FORMAT_TIME, gmtime(&now));
59 len=snprintf(buf2,sizeof(buf2),"[%s] %s%s\n",timebuf,userbuf,buf);
60 write(logfd, buf2, len);
61 }