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.17 by root, Tue Apr 15 03:16:02 2008 UTC vs.
Revision 1.29 by root, Mon Oct 29 23:55:52 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 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>
25#include <cstring> 24#include <cstring>
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{
62 struct tm *lt = localtime (&line.tv.tv_sec); 67 static const char levelchar [16+1] = "EWIDt???????????";
68 struct tm lt;
63 char pfx [PREFIX_LEN]; 69 char pfx [PREFIX_LEN];
64 70
71 localtime_r (&line.tv.tv_sec, &lt);
72
65 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d", 73 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d %c",
66 lt->tm_year + 1900, 74 lt.tm_year + 1900,
67 lt->tm_mon + 1, 75 lt.tm_mon + 1,
68 lt->tm_mday, 76 lt.tm_mday,
69 lt->tm_hour, 77 lt.tm_hour,
70 lt->tm_min, 78 lt.tm_min,
71 lt->tm_sec, 79 lt.tm_sec,
72 (int)(line.tv.tv_usec / 100) 80 (int)(line.tv.tv_usec / 100),
81 levelchar [line.flags & 15]
73 ); 82 );
74 83
75 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' '; 84 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' ';
76 85
77 struct iovec iov [2]; 86 struct iovec iov [2];
78 87
79 iov [0].iov_base = pfx; 88 iov [0].iov_base = pfx;
80 iov [0].iov_len = PREFIX_LEN; 89 iov [0].iov_len = PREFIX_LEN;
81 90
82 char *buf = line.buf; 91 char *buf = line.buf;
92
93 if (logsync != 2)
94 SMUTEX_LOCK (fdlock);
83 95
84 while (char *end = strchr (buf, '\n')) 96 while (char *end = strchr (buf, '\n'))
85 { 97 {
86 iov [1].iov_base = buf; 98 iov [1].iov_base = buf;
87 iov [1].iov_len = end - buf + 1; 99 iov [1].iov_len = end - buf + 1;
95 if (buf == line.buf + line.len) 107 if (buf == line.buf + line.len)
96 break; 108 break;
97 109
98 pfx [PREFIX_LEN - 1] = '+'; 110 pfx [PREFIX_LEN - 1] = '+';
99 } 111 }
112
113 if (logsync != 2)
114 SMUTEX_UNLOCK (fdlock);
100 115
101 sfree (line.buf, line.len); 116 sfree (line.buf, line.len);
102} 117}
103 118
104static void * 119static void *
120 // this algorithm could result in an ever-increasing vector 135 // this algorithm could result in an ever-increasing vector
121 // 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
122 // we have bigger problems. 137 // we have bigger problems.
123 if (idx == queue.size ()) 138 if (idx == queue.size ())
124 { 139 {
140 if (idx < 32)
125 queue.clear (); 141 queue.clear ();
142 else
143 logvector ().swap (queue); // free memory, hopefully
144
126 idx = 0; 145 idx = 0;
127 } 146 }
128 147
129 SMUTEX_UNLOCK (mutex); 148 SMUTEX_UNLOCK (mutex);
130 149
134 153
135void 154void
136log_cleanup () 155log_cleanup ()
137{ 156{
138 logsync = 1; 157 logsync = 1;
158 SMUTEX_UNLOCK (fdlock);
139 159
140 for (;;) 160 for (;;)
141 { 161 {
142 int done; 162 int done;
143 163
147 167
148 if (done) 168 if (done)
149 break; 169 break;
150 170
151 usleep (10000); 171 usleep (10000);
172 SMUTEX_LOCK (fdlock);
173 SMUTEX_UNLOCK (fdlock);
152 } 174 }
153} 175}
154 176
155static void 177static void
156af_child () 178af_child ()
157{ 179{
158 logsync = 1; 180 logsync = 2;
159} 181}
160 182
161static struct logthread : thread 183static struct logthread : thread
162{ 184{
163 logthread () 185 logthread ()
167 logsync = 0; 189 logsync = 0;
168 } 190 }
169} logthread; 191} logthread;
170 192
171void 193void
172LOG (int flags, const char *format, ...) 194LOG (int flags, const_utf8_string format, ...)
173{ 195{
174 int level = flags & 15; 196 int level = flags & 15;
175 197
176 if (level > settings.debug) 198 if (level > settings.debug)
177 return; 199 return;
219 SMUTEX_UNLOCK (mutex); 241 SMUTEX_UNLOCK (mutex);
220 SCOND_SIGNAL (cond); 242 SCOND_SIGNAL (cond);
221 } 243 }
222} 244}
223 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