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