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.15 by root, Sun Jul 1 05:00:17 2007 UTC vs.
Revision 1.24 by root, Sun Apr 11 17:27:51 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines