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