ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/logger.C
(Generate patch)

Comparing deliantra/server/common/logger.C (file contents):
Revision 1.14 by root, Sat Jun 9 23:18:17 2007 UTC vs.
Revision 1.27 by root, Sat Apr 23 04:56:46 2011 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 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 * 5 *
8 * Crossfire TRT is free software; you can redistribute it and/or modify it 6 * Deliantra is free software: you can redistribute it and/or modify it under
9 * under the terms of the GNU General Public License as published by the Free 7 * the terms of the Affero GNU General Public License as published by the
10 * Software Foundation; either version 2 of the License, or (at your option) 8 * Free Software Foundation, either version 3 of the License, or (at your
11 * any later version. 9 * option) any later version.
12 * 10 *
13 * This program is distributed in the hope that it will be useful, but 11 * This program is distributed in the hope that it will be useful,
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * for more details. 14 * GNU General Public License for more details.
17 * 15 *
18 * You should have received a copy of the GNU General Public License along 16 * You should have received a copy of the Affero GNU General Public License
19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 17 * and the GNU General Public License along with this program. If not, see
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 * <http://www.gnu.org/licenses/>.
21 * 19 *
22 * The authors can be reached via e-mail to <crossfire@schmorp.de> 20 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 21 */
24 22
25#include <cstdarg> 23#include <cstdarg>
24#include <cstring>
25
26#include <vector>
27
28#include <sys/uio.h>
29#include <pthread.h>
30
26#include <global.h> 31#include "global.h"
27#include <funcpoint.h> 32#include "dynbuf.h"
33#include "util.h"
28 34
35struct logline
36{
37 struct timeval tv;
38 char *buf; // includes PREFIX_LEN garbage bytes
39 int len;
40 int flags;
41};
42
43typedef std::vector<logline, slice_allocator<logline> > logvector;
44
45static SMUTEX(mutex);
46static SMUTEX(fdlock);
47static SCOND(cond);
48static int logfd = STDERR_FILENO;
49static int logsync = 1;
50static logvector queue;
51
52int log_setfd (int fd)
53{
54 SMUTEX_LOCK (fdlock);
55 int old = logfd;
56 logfd = fd < 0 ? STDERR_FILENO : fd;
57 SMUTEX_UNLOCK (fdlock);
58
59 return old;
60}
61
62#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000 L+") - 1
63
64static void
65log_sync (logline &line)
66{
67 static const char levelchar [16+1] = "EWIDt???????????";
68 struct tm lt;
69 char pfx [PREFIX_LEN];
70
71 localtime_r (&line.tv.tv_sec, &lt);
72
73 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d %c",
74 lt.tm_year + 1900,
75 lt.tm_mon + 1,
76 lt.tm_mday,
77 lt.tm_hour,
78 lt.tm_min,
79 lt.tm_sec,
80 (int)(line.tv.tv_usec / 100),
81 levelchar [line.flags & 15]
82 );
83
84 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' ';
85
86 struct iovec iov [2];
87
88 iov [0].iov_base = pfx;
89 iov [0].iov_len = PREFIX_LEN;
90
91 char *buf = line.buf;
92
93 if (logsync != 2)
94 SMUTEX_LOCK (fdlock);
95
96 while (char *end = strchr (buf, '\n'))
97 {
98 iov [1].iov_base = buf;
99 iov [1].iov_len = end - buf + 1;
100
101 writev (STDERR_FILENO, iov, 2);
102 if (logfd != STDERR_FILENO)
103 writev (logfd, iov, 2);
104
105 buf = end + 1;
106
107 if (buf == line.buf + line.len)
108 break;
109
110 pfx [PREFIX_LEN - 1] = '+';
111 }
112
113 if (logsync != 2)
114 SMUTEX_UNLOCK (fdlock);
115
116 sfree (line.buf, line.len);
117}
118
119static void *
120logthread_proc (void *arg)
121{
122 int idx = 0;
123
124 for (;;)
125 {
126 logline line;
127
128 SMUTEX_LOCK (mutex);
129
130 while (queue.empty ())
131 SCOND_WAIT (cond, mutex);
132
133 line = queue [idx++];
134
135 // this algorithm could result in an ever-increasing vector
136 // size if we log faster than we can write, but if that happens
137 // we have bigger problems.
138 if (idx == queue.size ())
139 {
140 if (idx < 32)
141 queue.clear ();
142 else
143 logvector ().swap (queue); // free memory, hopefully
144
145 idx = 0;
146 }
147
148 SMUTEX_UNLOCK (mutex);
149
150 log_sync (line);
151 }
152}
153
29void 154void
155log_cleanup ()
156{
157 logsync = 1;
158 SMUTEX_UNLOCK (fdlock);
159
160 for (;;)
161 {
162 int done;
163
164 SMUTEX_LOCK (mutex);
165 done = queue.empty ();
166 SMUTEX_UNLOCK (mutex);
167
168 if (done)
169 break;
170
171 usleep (10000);
172 SMUTEX_LOCK (fdlock);
173 SMUTEX_UNLOCK (fdlock);
174 }
175}
176
177static void
178af_child ()
179{
180 logsync = 2;
181}
182
183static struct logthread : thread
184{
185 logthread ()
186 {
187 pthread_atfork (0, 0, af_child);
188 start (logthread_proc);
189 logsync = 0;
190 }
191} logthread;
192
193void
30LOG (int flags, const char *format, ...) 194LOG (int flags, const_utf8_string format, ...)
31{ 195{
32 int level = flags & 15; 196 int level = flags & 15;
33 197
34 if (level > settings.debug) 198 if (level > settings.debug)
35 return; 199 return;
36 200
37 char buf[20480]; 201 logline line;
38
39 struct timeval tv;
40 gettimeofday (&tv, 0); 202 gettimeofday (&line.tv, 0);
41 struct tm *ptime = localtime (&tv.tv_sec);
42 203
43 int len = strftime (buf, sizeof (buf) - 7, "%Y-%m-%d %H:%M:%S", ptime); 204 static dynbuf_text buf;
44 len += sprintf (buf + len, ".%04d ", (int)(tv.tv_usec / 100));
45 205
46 va_list ap; 206 va_list ap;
47 va_start (ap, format); 207 va_start (ap, format);
48 len += vsnprintf (buf + len, sizeof (buf) - len, format, ap); 208 buf.vprintf (format, ap);
49 va_end (ap); 209 va_end (ap);
50 210
51 len = min (sizeof (buf) - 1, len); 211 buf << '\n';
52 212
213 line.buf = buf.linearise ();
214 line.len = buf.size ();
215
53 if (buf [len - 1] != '\n') 216 if (line.buf [line.len - 2] == '\n')
54 buf [len++] = '\n'; // not 0-terminated 217 --line.len;
55 218
56 write (STDERR_FILENO, buf, len); 219 line.buf = salloc<char> (line.len, line.buf);
57 if (logfile != stderr) 220
58 fwrite (buf, 1, len, logfile); 221 buf.clear ();
222
223 if (logsync)
224 flags |= logSync;
59 225
60 if (flags & logBacktrace) 226 if (flags & logBacktrace)
227 {
228 line.buf [line.len - 1] = 0;
61 log_backtrace (buf); 229 log_backtrace (line.buf);
230 line.buf [line.len - 1] = '\n';
231 }
232
233 line.flags = flags;
234
235 if (line.flags & logSync)
236 log_sync (line);
237 else
238 {
239 SMUTEX_LOCK (mutex);
240 queue.push_back (line);
241 SMUTEX_UNLOCK (mutex);
242 SCOND_SIGNAL (cond);
243 }
62} 244}
245
246static int suspended;
247
248void
249log_suspend ()
250{
251 if (!suspended++)
252 {
253 LOG (llevDebug, "logging suspended.");
254 SMUTEX_LOCK (fdlock);
255 }
256}
257
258void
259log_resume ()
260{
261 if (!--suspended)
262 {
263 SMUTEX_UNLOCK (fdlock);
264 LOG (llevDebug, "logging resumed.");
265 }
266}
267

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines