]>
Commit | Line | Data |
---|---|---|
c62d75be | 1 | #include "extern.h" |
2 | #include <cctype> | |
3 | #include <stdio.h> | |
fb37ecc7 | 4 | #include <fstream> |
5 | ||
6 | using std::endl; | |
7 | using std::cerr; | |
8 | using std::ofstream; | |
9 | using std::ios; | |
c62d75be | 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 | ||
03a4bdbb | 20 | ts = new char[64]; |
c62d75be | 21 | output = new char[4096]; |
22 | ||
9f8c2acc | 23 | outfile.open("gameserv.log", ios::out | ios::app); |
c62d75be | 24 | |
25 | if (outfile.fail()) | |
26 | { | |
9f8c2acc | 27 | cerr << "Error opening gameserv.log" << endl; |
c62d75be | 28 | return; |
29 | } | |
30 | ||
31 | struct tm *tm; | |
32 | time_t ti; | |
33 | time(&ti); | |
34 | tm = localtime(&ti); | |
03a4bdbb | 35 | strftime(ts, 64, "%m/%d/%Y %H:%M:%S", tm); |
c62d75be | 36 | |
37 | sprintf(output, "[%s]: ", ts); | |
38 | ||
39 | va_list args; | |
40 | va_start(args, fmt); | |
41 | ||
42 | for (; *t; t++) | |
43 | { | |
44 | if (*t == '%') | |
45 | { | |
46 | switch(*++t) { | |
47 | case 'd': sprintf(output, "%s%d", output, va_arg(args, int)); break; | |
48 | case 's': sprintf(output, "%s%s", output, va_arg(args, char *)); break; | |
49 | case 'S': sprintf(output, "%s%s", output, s_GameServ); 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 | } |