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.19 by root, Tue May 6 16:55:25 2008 UTC vs.
Revision 1.28 by root, Tue Jan 3 11:25:31 2012 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 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 * Deliantra 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 <support@deliantra.net> 20 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 21 */
23 22
24#include <cstdarg> 23#include <cstdarg>
39 char *buf; // includes PREFIX_LEN garbage bytes 38 char *buf; // includes PREFIX_LEN garbage bytes
40 int len; 39 int len;
41 int flags; 40 int flags;
42}; 41};
43 42
43typedef std::vector<logline, slice_allocator<logline> > logvector;
44
44static SMUTEX(mutex); 45static SMUTEX(mutex);
46static SMUTEX(fdlock);
45static SCOND(cond); 47static SCOND(cond);
46static int logfd = STDERR_FILENO; 48static int logfd = STDERR_FILENO;
47static int logsync = 1; 49static int logsync = 1;
48static std::vector<logline, slice_allocator<logline> > queue; 50static logvector queue;
49 51
50void set_logfd (int fd) 52int log_setfd (int fd)
51{ 53{
52 SMUTEX_LOCK (mutex); 54 SMUTEX_LOCK (fdlock);
55 int old = logfd;
53 logfd = fd < 0 ? STDERR_FILENO : fd; 56 logfd = fd < 0 ? STDERR_FILENO : fd;
54 SMUTEX_UNLOCK (mutex); 57 SMUTEX_UNLOCK (fdlock);
55}
56 58
59 return old;
60}
61
57#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 62#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000 L+") - 1
58 63
59static void 64static void
60log_sync (logline &line) 65log_sync (logline &line)
61{ 66{
67 static const char levelchar [16+1] = "EWIDt???????????";
62 struct tm lt; 68 struct tm lt;
63 char pfx [PREFIX_LEN]; 69 char pfx [PREFIX_LEN];
64 70
65 localtime_r (&line.tv.tv_sec, &lt); 71 localtime_r (&line.tv.tv_sec, &lt);
66 72
67 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d", 73 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d %c",
68 lt.tm_year + 1900, 74 lt.tm_year + 1900,
69 lt.tm_mon + 1, 75 lt.tm_mon + 1,
70 lt.tm_mday, 76 lt.tm_mday,
71 lt.tm_hour, 77 lt.tm_hour,
72 lt.tm_min, 78 lt.tm_min,
73 lt.tm_sec, 79 lt.tm_sec,
74 (int)(line.tv.tv_usec / 100) 80 (int)(line.tv.tv_usec / 100),
81 levelchar [line.flags & 15]
75 ); 82 );
76 83
77 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' '; 84 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' ';
78 85
79 struct iovec iov [2]; 86 struct iovec iov [2];
80 87
81 iov [0].iov_base = pfx; 88 iov [0].iov_base = pfx;
82 iov [0].iov_len = PREFIX_LEN; 89 iov [0].iov_len = PREFIX_LEN;
83 90
84 char *buf = line.buf; 91 char *buf = line.buf;
92
93 if (logsync != 2)
94 SMUTEX_LOCK (fdlock);
85 95
86 while (char *end = strchr (buf, '\n')) 96 while (char *end = strchr (buf, '\n'))
87 { 97 {
88 iov [1].iov_base = buf; 98 iov [1].iov_base = buf;
89 iov [1].iov_len = end - buf + 1; 99 iov [1].iov_len = end - buf + 1;
97 if (buf == line.buf + line.len) 107 if (buf == line.buf + line.len)
98 break; 108 break;
99 109
100 pfx [PREFIX_LEN - 1] = '+'; 110 pfx [PREFIX_LEN - 1] = '+';
101 } 111 }
112
113 if (logsync != 2)
114 SMUTEX_UNLOCK (fdlock);
102 115
103 sfree (line.buf, line.len); 116 sfree (line.buf, line.len);
104} 117}
105 118
106static void * 119static void *
122 // this algorithm could result in an ever-increasing vector 135 // this algorithm could result in an ever-increasing vector
123 // size if we log faster than we can write, but if that happens 136 // size if we log faster than we can write, but if that happens
124 // we have bigger problems. 137 // we have bigger problems.
125 if (idx == queue.size ()) 138 if (idx == queue.size ())
126 { 139 {
140 if (idx < 32)
127 queue.clear (); 141 queue.clear ();
142 else
143 logvector ().swap (queue); // free memory, hopefully
144
128 idx = 0; 145 idx = 0;
129 } 146 }
130 147
131 SMUTEX_UNLOCK (mutex); 148 SMUTEX_UNLOCK (mutex);
132 149
136 153
137void 154void
138log_cleanup () 155log_cleanup ()
139{ 156{
140 logsync = 1; 157 logsync = 1;
158 SMUTEX_UNLOCK (fdlock);
141 159
142 for (;;) 160 for (;;)
143 { 161 {
144 int done; 162 int done;
145 163
149 167
150 if (done) 168 if (done)
151 break; 169 break;
152 170
153 usleep (10000); 171 usleep (10000);
172 SMUTEX_LOCK (fdlock);
173 SMUTEX_UNLOCK (fdlock);
154 } 174 }
155} 175}
156 176
157static void 177static void
158af_child () 178af_child ()
159{ 179{
160 logsync = 1; 180 logsync = 2;
161} 181}
162 182
163static struct logthread : thread 183static struct logthread : thread
164{ 184{
165 logthread () 185 logthread ()
169 logsync = 0; 189 logsync = 0;
170 } 190 }
171} logthread; 191} logthread;
172 192
173void 193void
174LOG (int flags, const char *format, ...) 194LOG (int flags, const_utf8_string format, ...)
175{ 195{
176 int level = flags & 15; 196 int level = flags & 15;
177 197
178 if (level > settings.debug) 198 if (level > settings.debug)
179 return; 199 return;
221 SMUTEX_UNLOCK (mutex); 241 SMUTEX_UNLOCK (mutex);
222 SCOND_SIGNAL (cond); 242 SCOND_SIGNAL (cond);
223 } 243 }
224} 244}
225 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