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.24 by root, Sun Apr 11 17:27:51 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.
41 int len; 39 int len;
42 int flags; 40 int flags;
43}; 41};
44 42
45static SMUTEX(mutex); 43static SMUTEX(mutex);
44static SMUTEX(fdlock);
46static SCOND(cond); 45static SCOND(cond);
47static int logfd = STDERR_FILENO; 46static int logfd = STDERR_FILENO;
48static int logsync = 1; 47static int logsync = 1;
49static std::vector<logline, slice_allocator<logline> > queue; 48static std::vector<logline, slice_allocator<logline> > queue;
50 49
51void set_logfd (int fd) 50int log_setfd (int fd)
52{ 51{
53 SMUTEX_LOCK (mutex); 52 SMUTEX_LOCK (fdlock);
53 int old = logfd;
54 logfd = fd < 0 ? STDERR_FILENO : fd; 54 logfd = fd < 0 ? STDERR_FILENO : fd;
55 SMUTEX_UNLOCK (mutex); 55 SMUTEX_UNLOCK (fdlock);
56
57 return old;
56} 58}
57 59
58#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 60#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1
59 61
60static void 62static void
82 iov [0].iov_base = pfx; 84 iov [0].iov_base = pfx;
83 iov [0].iov_len = PREFIX_LEN; 85 iov [0].iov_len = PREFIX_LEN;
84 86
85 char *buf = line.buf; 87 char *buf = line.buf;
86 88
89 if (logsync != 2)
90 SMUTEX_LOCK (fdlock);
91
87 while (char *end = strchr (buf, '\n')) 92 while (char *end = strchr (buf, '\n'))
88 { 93 {
89 iov [1].iov_base = buf; 94 iov [1].iov_base = buf;
90 iov [1].iov_len = end - buf + 1; 95 iov [1].iov_len = end - buf + 1;
91 96
98 if (buf == line.buf + line.len) 103 if (buf == line.buf + line.len)
99 break; 104 break;
100 105
101 pfx [PREFIX_LEN - 1] = '+'; 106 pfx [PREFIX_LEN - 1] = '+';
102 } 107 }
108
109 if (logsync != 2)
110 SMUTEX_UNLOCK (fdlock);
103 111
104 sfree (line.buf, line.len); 112 sfree (line.buf, line.len);
105} 113}
106 114
107static void * 115static void *
150 158
151 if (done) 159 if (done)
152 break; 160 break;
153 161
154 usleep (10000); 162 usleep (10000);
163 SMUTEX_LOCK (fdlock);
164 SMUTEX_UNLOCK (fdlock);
155 } 165 }
156} 166}
157 167
158static void 168static void
159af_child () 169af_child ()
160{ 170{
161 logsync = 1; 171 logsync = 2;
162} 172}
163 173
164static struct logthread : thread 174static struct logthread : thread
165{ 175{
166 logthread () 176 logthread ()
170 logsync = 0; 180 logsync = 0;
171 } 181 }
172} logthread; 182} logthread;
173 183
174void 184void
175LOG (int flags, const char *format, ...) 185LOG (int flags, const_utf8_string format, ...)
176{ 186{
177 int level = flags & 15; 187 int level = flags & 15;
178 188
179 if (level > settings.debug) 189 if (level > settings.debug)
180 return; 190 return;
222 SMUTEX_UNLOCK (mutex); 232 SMUTEX_UNLOCK (mutex);
223 SCOND_SIGNAL (cond); 233 SCOND_SIGNAL (cond);
224 } 234 }
225} 235}
226 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