]>
Commit | Line | Data |
---|---|---|
c62d75be | 1 | #include "extern.h" |
2 | #include <cctype> | |
3 | #include <stdio.h> | |
fb37ecc7 | 4 | #include <fstream> |
7c2ebc31 | 5 | #include <iostream> |
fb37ecc7 | 6 | |
667813c2 | 7 | using namespace std; |
c62d75be | 8 | |
9 | void log(const char *fmt, ...) | |
10 | { | |
5de7a0b0 | 11 | if (fmt[0] == '\0') |
c62d75be | 12 | return; |
5de7a0b0 | 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 | delete []ts; | |
26 | delete []output; | |
27 | cerr << "Error opening gameserv.log" << endl; | |
28 | return; | |
29 | } | |
30 | ||
31 | struct tm *tm; | |
32 | time_t ti; | |
33 | time(&ti); | |
34 | tm = localtime(&ti); | |
35 | strftime(ts, 64, "%m/%d/%Y %H:%M:%S", tm); | |
36 | ||
37 | sprintf(output, "[%s]: ", ts); | |
38 | ||
39 | va_list args; | |
40 | va_start(args, fmt); | |
41 | ||
42 | for (; *t; t++) | |
c62d75be | 43 | { |
5de7a0b0 | 44 | if (*t == '%') |
c62d75be | 45 | { |
5de7a0b0 | 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 'c': sprintf(output, "%s%c", output, va_arg(args, int)); break; | |
51 | case 'l': | |
52 | if (*++t == 'd') | |
53 | sprintf(output, "%s%ld", output, va_arg(args, long int)); break; | |
54 | } | |
c62d75be | 55 | } |
5de7a0b0 | 56 | else |
c62d75be | 57 | { |
5de7a0b0 | 58 | sprintf(output, "%s%c", output, *t); |
c62d75be | 59 | } |
5de7a0b0 | 60 | |
c62d75be | 61 | } |
5de7a0b0 | 62 | |
63 | outfile << output << endl; | |
64 | ||
65 | outfile.close(); | |
66 | va_end(args); | |
67 | ||
68 | delete [] ts; | |
69 | delete [] output; | |
c62d75be | 70 | } |