]> jfr.im git - irc/gameservirc.git/blob - gameserv/log.cpp
Implemented a log command, and changed all output to go to a log file. Currently...
[irc/gameservirc.git] / gameserv / log.cpp
1 #include "extern.h"
2 #include <cctype>
3 #include <stdio.h>
4 #include <fstream.h>
5
6 void log(const char *fmt, ...)
7 {
8 if (fmt[0] == '\0')
9 return;
10
11 ofstream outfile;
12 char *ts, *output;
13 const char *t = fmt;
14
15 ts = new char[32];
16 output = new char[4096];
17
18 outfile.open("gameserv.log", ios::out | ios::app);
19
20 if (outfile.fail())
21 {
22 cerr << "Error opening gameserv.log" << endl;
23 return;
24 }
25
26 struct tm *tm;
27 time_t ti;
28 time(&ti);
29 tm = localtime(&ti);
30 strftime(ts, 32, "%Y%m%d", tm);
31
32 sprintf(output, "[%s]: ", ts);
33
34 va_list args;
35 va_start(args, fmt);
36
37 for (; *t; t++)
38 {
39 if (*t == '%')
40 {
41 switch(*++t) {
42 case 'd': sprintf(output, "%s%d", output, va_arg(args, int)); break;
43 case 's': sprintf(output, "%s%s", output, va_arg(args, char *)); break;
44 case 'S': sprintf(output, "%s%s", output, s_GameServ); break;
45 case 'l':
46 if (*++t == 'd')
47 sprintf(output, "%s%ld", output, va_arg(args, long int)); break;
48 }
49 }
50 else
51 {
52 sprintf(output, "%s%c", output, *t);
53 }
54
55 }
56
57 outfile << output << endl;
58
59 outfile.close();
60 va_end(args);
61
62 delete [] ts;
63 delete [] output;
64 }