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