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.20 by root, Mon Oct 12 14:00:57 2009 UTC vs.
Revision 1.25 by root, Sun Apr 11 17:54:13 2010 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 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 it under 6 * Deliantra is free software: you can redistribute it and/or modify it under
9 * 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
10 * 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
11 * option) any later version. 9 * option) any later version.
40 char *buf; // includes PREFIX_LEN garbage bytes 38 char *buf; // includes PREFIX_LEN garbage bytes
41 int len; 39 int len;
42 int flags; 40 int flags;
43}; 41};
44 42
43typedef std::vector<logline, slice_allocator<logline> > logvector;
44
45static SMUTEX(mutex); 45static SMUTEX(mutex);
46static SMUTEX(fdlock);
46static SCOND(cond); 47static SCOND(cond);
47static int logfd = STDERR_FILENO; 48static int logfd = STDERR_FILENO;
48static int logsync = 1; 49static int logsync = 1;
49static std::vector<logline, slice_allocator<logline> > queue; 50static logvector queue;
50 51
51void set_logfd (int fd) 52int log_setfd (int fd)
52{ 53{
53 SMUTEX_LOCK (mutex); 54 SMUTEX_LOCK (fdlock);
55 int old = logfd;
54 logfd = fd < 0 ? STDERR_FILENO : fd; 56 logfd = fd < 0 ? STDERR_FILENO : fd;
55 SMUTEX_UNLOCK (mutex); 57 SMUTEX_UNLOCK (fdlock);
58
59 return old;
56} 60}
57 61
58#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 62#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1
59 63
60static void 64static void
82 iov [0].iov_base = pfx; 86 iov [0].iov_base = pfx;
83 iov [0].iov_len = PREFIX_LEN; 87 iov [0].iov_len = PREFIX_LEN;
84 88
85 char *buf = line.buf; 89 char *buf = line.buf;
86 90
91 if (logsync != 2)
92 SMUTEX_LOCK (fdlock);
93
87 while (char *end = strchr (buf, '\n')) 94 while (char *end = strchr (buf, '\n'))
88 { 95 {
89 iov [1].iov_base = buf; 96 iov [1].iov_base = buf;
90 iov [1].iov_len = end - buf + 1; 97 iov [1].iov_len = end - buf + 1;
91 98
98 if (buf == line.buf + line.len) 105 if (buf == line.buf + line.len)
99 break; 106 break;
100 107
101 pfx [PREFIX_LEN - 1] = '+'; 108 pfx [PREFIX_LEN - 1] = '+';
102 } 109 }
110
111 if (logsync != 2)
112 SMUTEX_UNLOCK (fdlock);
103 113
104 sfree (line.buf, line.len); 114 sfree (line.buf, line.len);
105} 115}
106 116
107static void * 117static void *
123 // this algorithm could result in an ever-increasing vector 133 // this algorithm could result in an ever-increasing vector
124 // size if we log faster than we can write, but if that happens 134 // size if we log faster than we can write, but if that happens
125 // we have bigger problems. 135 // we have bigger problems.
126 if (idx == queue.size ()) 136 if (idx == queue.size ())
127 { 137 {
138 if (idx < 32)
128 queue.clear (); 139 queue.clear ();
140 else
141 logvector ().swap (queue); // free memory, hopefully
142
129 idx = 0; 143 idx = 0;
130 } 144 }
131 145
132 SMUTEX_UNLOCK (mutex); 146 SMUTEX_UNLOCK (mutex);
133 147
137 151
138void 152void
139log_cleanup () 153log_cleanup ()
140{ 154{
141 logsync = 1; 155 logsync = 1;
156 SMUTEX_UNLOCK (fdlock);
142 157
143 for (;;) 158 for (;;)
144 { 159 {
145 int done; 160 int done;
146 161
150 165
151 if (done) 166 if (done)
152 break; 167 break;
153 168
154 usleep (10000); 169 usleep (10000);
170 SMUTEX_LOCK (fdlock);
171 SMUTEX_UNLOCK (fdlock);
155 } 172 }
156} 173}
157 174
158static void 175static void
159af_child () 176af_child ()
160{ 177{
161 logsync = 1; 178 logsync = 2;
162} 179}
163 180
164static struct logthread : thread 181static struct logthread : thread
165{ 182{
166 logthread () 183 logthread ()
170 logsync = 0; 187 logsync = 0;
171 } 188 }
172} logthread; 189} logthread;
173 190
174void 191void
175LOG (int flags, const char *format, ...) 192LOG (int flags, const_utf8_string format, ...)
176{ 193{
177 int level = flags & 15; 194 int level = flags & 15;
178 195
179 if (level > settings.debug) 196 if (level > settings.debug)
180 return; 197 return;
222 SMUTEX_UNLOCK (mutex); 239 SMUTEX_UNLOCK (mutex);
223 SCOND_SIGNAL (cond); 240 SCOND_SIGNAL (cond);
224 } 241 }
225} 242}
226 243
244static int suspended;
245
246void
247log_suspend ()
248{
249 if (!suspended++)
250 {
251 LOG (llevDebug, "logging suspended.");
252 SMUTEX_LOCK (fdlock);
253 }
254}
255
256void
257log_resume ()
258{
259 if (!--suspended)
260 {
261 SMUTEX_UNLOCK (fdlock);
262 LOG (llevDebug, "logging resumed.");
263 }
264}
265

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines