1 | /* |
1 | /* |
2 | * static char *rcsid_loger_c = |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * "$Id: logger.C,v 1.3 2006/08/29 08:01:35 root Exp $ "; |
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
|
|
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
|
|
7 | * |
|
|
8 | * Deliantra is free software: you can redistribute it and/or modify |
|
|
9 | * it under the terms of the GNU General Public License as published by |
|
|
10 | * the Free Software Foundation, either version 3 of the License, or |
|
|
11 | * (at your option) any later version. |
|
|
12 | * |
|
|
13 | * This program is distributed in the hope that it will be useful, |
|
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
16 | * GNU General Public License for more details. |
|
|
17 | * |
|
|
18 | * You should have received a copy of the GNU General Public License |
|
|
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
4 | */ |
22 | */ |
5 | |
23 | |
6 | /* |
|
|
7 | CrossFire, A Multiplayer game for X-windows |
|
|
8 | |
|
|
9 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
|
|
10 | Copyright (C) 1992 Frank Tore Johansen |
|
|
11 | |
|
|
12 | This program is free software; you can redistribute it and/or modify |
|
|
13 | it under the terms of the GNU General Public License as published by |
|
|
14 | the Free Software Foundation; either version 2 of the License, or |
|
|
15 | (at your option) any later version. |
|
|
16 | |
|
|
17 | This program is distributed in the hope that it will be useful, |
|
|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
20 | GNU General Public License for more details. |
|
|
21 | |
|
|
22 | You should have received a copy of the GNU General Public License |
|
|
23 | along with this program; if not, write to the Free Software |
|
|
24 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
25 | |
|
|
26 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
|
|
27 | */ |
|
|
28 | |
|
|
29 | |
|
|
30 | #include <stdarg.h> |
24 | #include <cstdarg> |
|
|
25 | #include <cstring> |
|
|
26 | |
|
|
27 | #include <vector> |
|
|
28 | |
|
|
29 | #include <sys/uio.h> |
|
|
30 | #include <pthread.h> |
|
|
31 | |
31 | #include <global.h> |
32 | #include "global.h" |
32 | #include <funcpoint.h> |
33 | #include "dynbuf.h" |
|
|
34 | #include "util.h" |
33 | |
35 | |
34 | /* |
36 | struct logline |
35 | * Logs a message to stderr, or to file, and/or even to socket. |
|
|
36 | * Or discards the message if it is of no importanse, and none have |
|
|
37 | * asked to hear messages of that logLevel. |
|
|
38 | * |
|
|
39 | * See include/logger.h for possible logLevels. Messages with llevInfo |
|
|
40 | * and llevError are always printed, regardless of debug mode. |
|
|
41 | */ |
|
|
42 | |
|
|
43 | void LOG (LogLevel logLevel, const char *format, ...) |
|
|
44 | { |
37 | { |
45 | char buf[20480]; /* This needs to be really really big - larger |
38 | struct timeval tv; |
46 | * than any other buffer, since that buffer may |
39 | char *buf; // includes PREFIX_LEN garbage bytes |
47 | * need to be put in this one. |
40 | int len; |
48 | */ |
41 | int flags; |
49 | char tbuf[20480]; |
42 | }; |
50 | |
43 | |
51 | time_t curtime = time (NULL); |
44 | static SMUTEX(mutex); |
52 | struct tm *ptime = localtime (&curtime); |
45 | static SCOND(cond); |
|
|
46 | static int logfd = STDERR_FILENO; |
|
|
47 | static int logsync = 1; |
|
|
48 | static std::vector<logline, slice_allocator<logline> > queue; |
53 | |
49 | |
54 | strftime (tbuf, 256, "%Y-%m-%d %H:%M:%S ", ptime); |
50 | void set_logfd (int fd) |
|
|
51 | { |
|
|
52 | SMUTEX_LOCK (mutex); |
|
|
53 | logfd = fd < 0 ? STDERR_FILENO : fd; |
|
|
54 | SMUTEX_UNLOCK (mutex); |
|
|
55 | } |
|
|
56 | |
|
|
57 | #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 |
|
|
58 | |
|
|
59 | static void |
|
|
60 | log_sync (logline &line) |
|
|
61 | { |
|
|
62 | struct tm lt; |
|
|
63 | char pfx [PREFIX_LEN]; |
|
|
64 | |
|
|
65 | localtime_r (&line.tv.tv_sec, <); |
|
|
66 | |
|
|
67 | sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d", |
|
|
68 | lt.tm_year + 1900, |
|
|
69 | lt.tm_mon + 1, |
|
|
70 | lt.tm_mday, |
|
|
71 | lt.tm_hour, |
|
|
72 | lt.tm_min, |
|
|
73 | lt.tm_sec, |
|
|
74 | (int)(line.tv.tv_usec / 100) |
|
|
75 | ); |
|
|
76 | |
|
|
77 | pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' '; |
|
|
78 | |
|
|
79 | struct iovec iov [2]; |
|
|
80 | |
|
|
81 | iov [0].iov_base = pfx; |
|
|
82 | iov [0].iov_len = PREFIX_LEN; |
|
|
83 | |
|
|
84 | char *buf = line.buf; |
|
|
85 | |
|
|
86 | while (char *end = strchr (buf, '\n')) |
|
|
87 | { |
|
|
88 | iov [1].iov_base = buf; |
|
|
89 | iov [1].iov_len = end - buf + 1; |
|
|
90 | |
|
|
91 | writev (STDERR_FILENO, iov, 2); |
|
|
92 | if (logfd != STDERR_FILENO) |
|
|
93 | writev (logfd, iov, 2); |
|
|
94 | |
|
|
95 | buf = end + 1; |
|
|
96 | |
|
|
97 | if (buf == line.buf + line.len) |
|
|
98 | break; |
|
|
99 | |
|
|
100 | pfx [PREFIX_LEN - 1] = '+'; |
|
|
101 | } |
|
|
102 | |
|
|
103 | sfree (line.buf, line.len); |
|
|
104 | } |
|
|
105 | |
|
|
106 | static void * |
|
|
107 | logthread_proc (void *arg) |
|
|
108 | { |
|
|
109 | int idx = 0; |
|
|
110 | |
|
|
111 | for (;;) |
|
|
112 | { |
|
|
113 | logline line; |
|
|
114 | |
|
|
115 | SMUTEX_LOCK (mutex); |
|
|
116 | |
|
|
117 | while (queue.empty ()) |
|
|
118 | SCOND_WAIT (cond, mutex); |
|
|
119 | |
|
|
120 | line = queue [idx++]; |
|
|
121 | |
|
|
122 | // this algorithm could result in an ever-increasing vector |
|
|
123 | // size if we log faster than we can write, but if that happens |
|
|
124 | // we have bigger problems. |
|
|
125 | if (idx == queue.size ()) |
|
|
126 | { |
|
|
127 | queue.clear (); |
|
|
128 | idx = 0; |
|
|
129 | } |
|
|
130 | |
|
|
131 | SMUTEX_UNLOCK (mutex); |
|
|
132 | |
|
|
133 | log_sync (line); |
|
|
134 | } |
|
|
135 | } |
|
|
136 | |
|
|
137 | void |
|
|
138 | log_cleanup () |
|
|
139 | { |
|
|
140 | logsync = 1; |
|
|
141 | |
|
|
142 | for (;;) |
|
|
143 | { |
|
|
144 | int done; |
|
|
145 | |
|
|
146 | SMUTEX_LOCK (mutex); |
|
|
147 | done = queue.empty (); |
|
|
148 | SMUTEX_UNLOCK (mutex); |
|
|
149 | |
|
|
150 | if (done) |
|
|
151 | break; |
|
|
152 | |
|
|
153 | usleep (10000); |
|
|
154 | } |
|
|
155 | } |
|
|
156 | |
|
|
157 | static void |
|
|
158 | af_child () |
|
|
159 | { |
|
|
160 | logsync = 1; |
|
|
161 | } |
|
|
162 | |
|
|
163 | static struct logthread : thread |
|
|
164 | { |
|
|
165 | logthread () |
|
|
166 | { |
|
|
167 | pthread_atfork (0, 0, af_child); |
|
|
168 | start (logthread_proc); |
|
|
169 | logsync = 0; |
|
|
170 | } |
|
|
171 | } logthread; |
|
|
172 | |
|
|
173 | void |
|
|
174 | LOG (int flags, const char *format, ...) |
|
|
175 | { |
|
|
176 | int level = flags & 15; |
|
|
177 | |
|
|
178 | if (level > settings.debug) |
|
|
179 | return; |
|
|
180 | |
|
|
181 | logline line; |
|
|
182 | gettimeofday (&line.tv, 0); |
|
|
183 | |
|
|
184 | static dynbuf_text buf; |
55 | |
185 | |
56 | va_list ap; |
186 | va_list ap; |
57 | va_start(ap, format); |
187 | va_start (ap, format); |
58 | |
188 | buf.vprintf (format, ap); |
59 | buf[0] = '\0'; |
|
|
60 | if (logLevel <= settings.debug) |
|
|
61 | { |
|
|
62 | vsnprintf(buf, sizeof (buf), format, ap); |
|
|
63 | strncat(tbuf, buf, 20460); |
|
|
64 | #ifdef WIN32 /* ---win32 change log handling for win32 */ |
|
|
65 | fputs(tbuf, logfile); /* wrote to file or stdout */ |
|
|
66 | #ifdef DEBUG /* if we have a debug version, we want see ALL output */ |
|
|
67 | fflush(logfile); /* so flush this! */ |
|
|
68 | #endif |
|
|
69 | if(logfile != stderr) /* if was it a logfile wrote it to screen too */ |
|
|
70 | fputs(tbuf, stderr); |
|
|
71 | #else |
|
|
72 | fputs(tbuf, logfile); |
|
|
73 | #endif |
|
|
74 | } |
|
|
75 | if (!exiting && !trying_emergency_save && |
|
|
76 | logLevel == llevError && ++nroferrors > MAX_ERRORS) { |
|
|
77 | exiting = 1; |
|
|
78 | if (!trying_emergency_save) |
|
|
79 | emergency_save(0); |
|
|
80 | } |
|
|
81 | va_end(ap); |
189 | va_end (ap); |
|
|
190 | |
|
|
191 | buf << '\n'; |
|
|
192 | |
|
|
193 | line.buf = buf.linearise (); |
|
|
194 | line.len = buf.size (); |
|
|
195 | |
|
|
196 | if (line.buf [line.len - 2] == '\n') |
|
|
197 | --line.len; |
|
|
198 | |
|
|
199 | line.buf = salloc<char> (line.len, line.buf); |
|
|
200 | |
|
|
201 | buf.clear (); |
|
|
202 | |
|
|
203 | if (logsync) |
|
|
204 | flags |= logSync; |
|
|
205 | |
|
|
206 | if (flags & logBacktrace) |
|
|
207 | { |
|
|
208 | line.buf [line.len - 1] = 0; |
|
|
209 | log_backtrace (line.buf); |
|
|
210 | line.buf [line.len - 1] = '\n'; |
|
|
211 | } |
|
|
212 | |
|
|
213 | line.flags = flags; |
|
|
214 | |
|
|
215 | if (line.flags & logSync) |
|
|
216 | log_sync (line); |
|
|
217 | else |
|
|
218 | { |
|
|
219 | SMUTEX_LOCK (mutex); |
|
|
220 | queue.push_back (line); |
|
|
221 | SMUTEX_UNLOCK (mutex); |
|
|
222 | SCOND_SIGNAL (cond); |
|
|
223 | } |
82 | } |
224 | } |
|
|
225 | |