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.4 by root, Sun Sep 10 16:00:23 2006 UTC vs.
Revision 1.31 by root, Sat Nov 17 23:40:00 2018 UTC

1
2/* 1/*
3 * static char *rcsid_loger_c = 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
4 * "$Id: logger.C,v 1.4 2006/09/10 16:00:23 root Exp $ "; 3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 *
7 * Deliantra is free software: you can redistribute it and/or modify it under
8 * the terms of the Affero GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the Affero GNU General Public License
18 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
5 */ 22 */
6 23
7/*
8 CrossFire, A Multiplayer game for X-windows
9
10 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
11 Copyright (C) 1992 Frank Tore Johansen
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 The authors can be reached via e-mail at crossfire-devel@real-time.com
28*/
29
30
31#include <stdarg.h> 24#include <cstdarg>
25#include <cstring>
26
27#include <vector>
28
29#include <sys/uio.h>
30#include <pthread.h>
31
32#include <global.h> 32#include "global.h"
33#include <funcpoint.h> 33#include "dynbuf.h"
34#include "util.h"
34 35
35/* 36struct logline
36 * Logs a message to stderr, or to file, and/or even to socket.
37 * Or discards the message if it is of no importanse, and none have
38 * asked to hear messages of that logLevel.
39 *
40 * See include/logger.h for possible logLevels. Messages with llevInfo
41 * and llevError are always printed, regardless of debug mode.
42 */
43
44void
45LOG (LogLevel logLevel, const char *format, ...)
46{ 37{
47 char buf[20480]; /* This needs to be really really big - larger 38 struct timeval tv;
48 * than any other buffer, since that buffer may 39 char *buf; // includes PREFIX_LEN garbage bytes
49 * need to be put in this one. 40 int len;
50 */ 41 int flags;
51 char tbuf[20480]; 42};
52 43
53 time_t curtime = time (NULL); 44typedef std::vector<logline, slice_allocator<logline> > logvector;
54 struct tm *ptime = localtime (&curtime);
55 45
56 strftime (tbuf, 256, "%Y-%m-%d %H:%M:%S ", ptime); 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
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
195LOG (int flags, const_utf8_string format, ...)
196{
197 int level = flags & 15;
198
199 if (level > settings.debug)
200 return;
201
202 logline line;
203 gettimeofday (&line.tv, 0);
204
205 static dynbuf_text buf;
57 206
58 va_list ap; 207 va_list ap;
59
60 va_start (ap, format); 208 va_start (ap, format);
61 209 buf.vprintf (format, ap);
62 buf[0] = '\0';
63 if (logLevel <= settings.debug)
64 {
65 vsnprintf (buf, sizeof (buf), format, ap);
66 strncat (tbuf, buf, 20460);
67#ifdef WIN32 /* ---win32 change log handling for win32 */
68 fputs (tbuf, logfile); /* wrote to file or stdout */
69# ifdef DEBUG /* if we have a debug version, we want see ALL output */
70 fflush (logfile); /* so flush this! */
71# endif
72 if (logfile != stderr) /* if was it a logfile wrote it to screen too */
73 fputs (tbuf, stderr);
74#else
75 fputs (tbuf, logfile);
76#endif
77 }
78 if (!exiting && !trying_emergency_save && logLevel == llevError && ++nroferrors > MAX_ERRORS)
79 {
80 exiting = 1;
81 if (!trying_emergency_save)
82 emergency_save (0);
83 }
84 va_end (ap); 210 va_end (ap);
211
212 buf << '\n';
213
214 line.buf = buf.linearise ();
215 line.len = buf.size ();
216
217 if (line.buf [line.len - 2] == '\n')
218 --line.len;
219
220 line.buf = salloc<char> (line.len, line.buf);
221
222 buf.clear ();
223
224 if (logsync)
225 flags |= logSync;
226
227 if (flags & logBacktrace)
228 {
229 line.buf [line.len - 1] = 0;
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 }
85} 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