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