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.23 by root, Fri Mar 26 01:04:44 2010 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,2008,2009,2010 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 * 5 *
6 * Deliantra is free software: you can redistribute it and/or modify it under 6 * Deliantra is free software: you can redistribute it and/or modify it under
7 * the terms of the Affero GNU General Public License as published by the 7 * the terms of the Affero GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your 8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version. 9 * option) any later version.
10 * 10 *
11 * 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,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the Affero GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
17 * and the GNU General Public License along with this program. If not, see 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>. 18 * <http://www.gnu.org/licenses/>.
19 * 19 *
20 * 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>
21 */ 21 */
22 22
23#include <cstdarg> 23#include <cstdarg>
24#include <cstring> 24#include <cstring>
38 char *buf; // includes PREFIX_LEN garbage bytes 38 char *buf; // includes PREFIX_LEN garbage bytes
39 int len; 39 int len;
40 int flags; 40 int flags;
41}; 41};
42 42
43typedef std::vector<logline, slice_allocator<logline> > logvector;
44
43static SMUTEX(mutex); 45static SMUTEX(mutex);
46static SMUTEX(fdlock);
44static SCOND(cond); 47static SCOND(cond);
45static int logfd = STDERR_FILENO; 48static int logfd = STDERR_FILENO;
46static int logsync = 1; 49static int logsync = 1;
47static std::vector<logline, slice_allocator<logline> > queue; 50static logvector queue;
48 51
49void set_logfd (int fd) 52int log_setfd (int fd)
50{ 53{
51 SMUTEX_LOCK (mutex); 54 SMUTEX_LOCK (fdlock);
55 int old = logfd;
52 logfd = fd < 0 ? STDERR_FILENO : fd; 56 logfd = fd < 0 ? STDERR_FILENO : fd;
53 SMUTEX_UNLOCK (mutex); 57 SMUTEX_UNLOCK (fdlock);
54}
55 58
59 return old;
60}
61
56#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
57 63
58static void 64static void
59log_sync (logline &line) 65log_sync (logline &line)
60{ 66{
67 static const char levelchar [16+1] = "EWIDt???????????";
61 struct tm lt; 68 struct tm lt;
62 char pfx [PREFIX_LEN]; 69 char pfx [PREFIX_LEN];
63 70
64 localtime_r (&line.tv.tv_sec, &lt); 71 localtime_r (&line.tv.tv_sec, &lt);
65 72
66 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d", 73 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d %c",
67 lt.tm_year + 1900, 74 lt.tm_year + 1900,
68 lt.tm_mon + 1, 75 lt.tm_mon + 1,
69 lt.tm_mday, 76 lt.tm_mday,
70 lt.tm_hour, 77 lt.tm_hour,
71 lt.tm_min, 78 lt.tm_min,
72 lt.tm_sec, 79 lt.tm_sec,
73 (int)(line.tv.tv_usec / 100) 80 (int)(line.tv.tv_usec / 100),
81 levelchar [line.flags & 15]
74 ); 82 );
75 83
76 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' '; 84 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' ';
77 85
78 struct iovec iov [2]; 86 struct iovec iov [2];
79 87
80 iov [0].iov_base = pfx; 88 iov [0].iov_base = pfx;
81 iov [0].iov_len = PREFIX_LEN; 89 iov [0].iov_len = PREFIX_LEN;
82 90
83 char *buf = line.buf; 91 char *buf = line.buf;
92
93 if (logsync != 2)
94 SMUTEX_LOCK (fdlock);
84 95
85 while (char *end = strchr (buf, '\n')) 96 while (char *end = strchr (buf, '\n'))
86 { 97 {
87 iov [1].iov_base = buf; 98 iov [1].iov_base = buf;
88 iov [1].iov_len = end - buf + 1; 99 iov [1].iov_len = end - buf + 1;
96 if (buf == line.buf + line.len) 107 if (buf == line.buf + line.len)
97 break; 108 break;
98 109
99 pfx [PREFIX_LEN - 1] = '+'; 110 pfx [PREFIX_LEN - 1] = '+';
100 } 111 }
112
113 if (logsync != 2)
114 SMUTEX_UNLOCK (fdlock);
101 115
102 sfree (line.buf, line.len); 116 sfree (line.buf, line.len);
103} 117}
104 118
105static void * 119static void *
121 // this algorithm could result in an ever-increasing vector 135 // this algorithm could result in an ever-increasing vector
122 // 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
123 // we have bigger problems. 137 // we have bigger problems.
124 if (idx == queue.size ()) 138 if (idx == queue.size ())
125 { 139 {
140 if (idx < 32)
126 queue.clear (); 141 queue.clear ();
142 else
143 logvector ().swap (queue); // free memory, hopefully
144
127 idx = 0; 145 idx = 0;
128 } 146 }
129 147
130 SMUTEX_UNLOCK (mutex); 148 SMUTEX_UNLOCK (mutex);
131 149
135 153
136void 154void
137log_cleanup () 155log_cleanup ()
138{ 156{
139 logsync = 1; 157 logsync = 1;
158 SMUTEX_UNLOCK (fdlock);
140 159
141 for (;;) 160 for (;;)
142 { 161 {
143 int done; 162 int done;
144 163
148 167
149 if (done) 168 if (done)
150 break; 169 break;
151 170
152 usleep (10000); 171 usleep (10000);
172 SMUTEX_LOCK (fdlock);
173 SMUTEX_UNLOCK (fdlock);
153 } 174 }
154} 175}
155 176
156static void 177static void
157af_child () 178af_child ()
158{ 179{
159 logsync = 1; 180 logsync = 2;
160} 181}
161 182
162static struct logthread : thread 183static struct logthread : thread
163{ 184{
164 logthread () 185 logthread ()
220 SMUTEX_UNLOCK (mutex); 241 SMUTEX_UNLOCK (mutex);
221 SCOND_SIGNAL (cond); 242 SCOND_SIGNAL (cond);
222 } 243 }
223} 244}
224 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