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.16 by root, Thu Nov 8 19:43:23 2007 UTC vs.
Revision 1.31 by root, Sat Nov 17 23:40:00 2018 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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 6 *
8 * Deliantra is free software: you can redistribute it and/or modify 7 * 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 8 * 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 9 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 10 * option) any later version.
12 * 11 *
13 * This program is distributed in the hope that it will be useful, 12 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 15 * GNU General Public License for more details.
17 * 16 *
18 * You should have received a copy of the GNU General Public License 17 * 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/>. 18 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 * 20 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24#include <cstdarg> 24#include <cstdarg>
25#include <cstring>
26
27#include <vector>
28
29#include <sys/uio.h>
30#include <pthread.h>
31
25#include <global.h> 32#include "global.h"
26#include <funcpoint.h> 33#include "dynbuf.h"
34#include "util.h"
27 35
36struct logline
37{
38 struct timeval tv;
39 char *buf; // includes PREFIX_LEN garbage bytes
40 int len;
41 int flags;
42};
43
44typedef std::vector<logline, slice_allocator<logline> > logvector;
45
46static SMUTEX(mutex);
47static SMUTEX(fdlock);
48static SCOND(cond);
49static int logfd = STDERR_FILENO;
50static int logsync = 1;
51static logvector queue;
52
53int log_setfd (int fd)
54{
55 SMUTEX_LOCK (fdlock);
56 int old = logfd;
57 logfd = fd < 0 ? STDERR_FILENO : fd;
58 SMUTEX_UNLOCK (fdlock);
59
60 return old;
61}
62
63#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000 L+") - 1
64
65static void
66log_sync (logline &line)
67{
68 static const char levelchar [16+1] = "EWIDt???????????";
69 struct tm lt;
70 char pfx [PREFIX_LEN];
71
72 localtime_r (&line.tv.tv_sec, &lt);
73
74 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d %c",
75 lt.tm_year + 1900,
76 lt.tm_mon + 1,
77 lt.tm_mday,
78 lt.tm_hour,
79 lt.tm_min,
80 lt.tm_sec,
81 (int)(line.tv.tv_usec / 100),
82 levelchar [line.flags & 15]
83 );
84
85 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' ';
86
87 struct iovec iov [2];
88
89 iov [0].iov_base = pfx;
90 iov [0].iov_len = PREFIX_LEN;
91
92 char *buf = line.buf;
93
94 if (logsync != 2)
95 SMUTEX_LOCK (fdlock);
96
97 while (char *end = strchr (buf, '\n'))
98 {
99 iov [1].iov_base = buf;
100 iov [1].iov_len = end - buf + 1;
101
102 writev (STDERR_FILENO, iov, 2);
103 if (logfd != STDERR_FILENO)
104 writev (logfd, iov, 2);
105
106 buf = end + 1;
107
108 if (buf == line.buf + line.len)
109 break;
110
111 pfx [PREFIX_LEN - 1] = '+';
112 }
113
114 if (logsync != 2)
115 SMUTEX_UNLOCK (fdlock);
116
117 sfree (line.buf, line.len);
118}
119
120static void *
121logthread_proc (void *arg)
122{
123 int idx = 0;
124
125 for (;;)
126 {
127 logline line;
128
129 SMUTEX_LOCK (mutex);
130
131 while (queue.empty ())
132 SCOND_WAIT (cond, mutex);
133
134 line = queue [idx++];
135
136 // this algorithm could result in an ever-increasing vector
137 // size if we log faster than we can write, but if that happens
138 // we have bigger problems.
139 if (idx == queue.size ())
140 {
141 if (idx < 32)
142 queue.clear ();
143 else
144 logvector ().swap (queue); // free memory, hopefully
145
146 idx = 0;
147 }
148
149 SMUTEX_UNLOCK (mutex);
150
151 log_sync (line);
152 }
153}
154
28void 155void
156log_cleanup ()
157{
158 logsync = 1;
159 SMUTEX_UNLOCK (fdlock);
160
161 for (;;)
162 {
163 int done;
164
165 SMUTEX_LOCK (mutex);
166 done = queue.empty ();
167 SMUTEX_UNLOCK (mutex);
168
169 if (done)
170 break;
171
172 usleep (10000);
173 SMUTEX_LOCK (fdlock);
174 SMUTEX_UNLOCK (fdlock);
175 }
176}
177
178static void
179af_child ()
180{
181 logsync = 2;
182}
183
184static struct logthread : thread
185{
186 logthread ()
187 {
188 pthread_atfork (0, 0, af_child);
189 start (logthread_proc);
190 logsync = 0;
191 }
192} logthread;
193
194void
29LOG (int flags, const char *format, ...) 195LOG (int flags, const_utf8_string format, ...)
30{ 196{
31 int level = flags & 15; 197 int level = flags & 15;
32 198
33 if (level > settings.debug) 199 if (level > settings.debug)
34 return; 200 return;
35 201
36 char buf[20480]; 202 logline line;
37
38 struct timeval tv;
39 gettimeofday (&tv, 0); 203 gettimeofday (&line.tv, 0);
40 struct tm *ptime = localtime (&tv.tv_sec);
41 204
42 int len = strftime (buf, sizeof (buf) - 7, "%Y-%m-%d %H:%M:%S", ptime); 205 static dynbuf_text buf;
43 len += sprintf (buf + len, ".%04d ", (int)(tv.tv_usec / 100));
44 206
45 va_list ap; 207 va_list ap;
46 va_start (ap, format); 208 va_start (ap, format);
47 len += vsnprintf (buf + len, sizeof (buf) - len, format, ap); 209 buf.vprintf (format, ap);
48 va_end (ap); 210 va_end (ap);
49 211
50 len = min (sizeof (buf) - 1, len); 212 buf << '\n';
51 213
214 line.buf = buf.linearise ();
215 line.len = buf.size ();
216
52 if (buf [len - 1] != '\n') 217 if (line.buf [line.len - 2] == '\n')
53 buf [len++] = '\n'; // not 0-terminated 218 --line.len;
54 219
55 write (STDERR_FILENO, buf, len); 220 line.buf = salloc<char> (line.len, line.buf);
56 if (logfile != stderr) 221
57 fwrite (buf, 1, len, logfile); 222 buf.clear ();
223
224 if (logsync)
225 flags |= logSync;
58 226
59 if (flags & logBacktrace) 227 if (flags & logBacktrace)
228 {
229 line.buf [line.len - 1] = 0;
60 log_backtrace (buf); 230 log_backtrace (line.buf);
231 line.buf [line.len - 1] = '\n';
232 }
233
234 line.flags = flags;
235
236 if (line.flags & logSync)
237 log_sync (line);
238 else
239 {
240 SMUTEX_LOCK (mutex);
241 queue.push_back (line);
242 SMUTEX_UNLOCK (mutex);
243 SCOND_SIGNAL (cond);
244 }
61} 245}
246
247static int suspended;
248
249void
250log_suspend ()
251{
252 if (!suspended++)
253 {
254 LOG (llevDebug, "logging suspended.");
255 SMUTEX_LOCK (fdlock);
256 }
257}
258
259void
260log_resume ()
261{
262 if (!--suspended)
263 {
264 SMUTEX_UNLOCK (fdlock);
265 LOG (llevDebug, "logging resumed.");
266 }
267}
268

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines