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.9 by root, Sun Jan 7 23:10:42 2007 UTC vs.
Revision 1.22 by root, Sat Jan 16 13:41:37 2010 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 3 *
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 *
6 Copyright (C) 1992 Frank Tore Johansen 6 * Deliantra is free software: you can redistribute it and/or modify it under
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
10 the Free Software Foundation; either version 2 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
11 (at your option) any later version. 9 * option) any later version.
12 10 *
13 This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 14 * GNU General Public License for more details.
17 15 *
18 You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
19 along with this program; if not, write to the Free Software 17 * and the GNU General Public License along with this program. If not, see
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 * <http://www.gnu.org/licenses/>.
21
22 The authors can be reached via e-mail at <crossfire@schmorp.de>
23*/
24
25
26#include <stdarg.h>
27#include <global.h>
28#include <funcpoint.h>
29
30/*
31 * Logs a message to stderr, or to file, and/or even to socket.
32 * Or discards the message if it is of no importanse, and none have
33 * asked to hear messages of that logLevel.
34 * 19 *
35 * See include/logger.h for possible logLevels. Messages with llevInfo 20 * The authors can be reached via e-mail to <support@deliantra.net>
36 * and llevError are always printed, regardless of debug mode.
37 */ 21 */
38 22
23#include <cstdarg>
24#include <cstring>
25
26#include <vector>
27
28#include <sys/uio.h>
29#include <pthread.h>
30
31#include "global.h"
32#include "dynbuf.h"
33#include "util.h"
34
35struct logline
36{
37 struct timeval tv;
38 char *buf; // includes PREFIX_LEN garbage bytes
39 int len;
40 int flags;
41};
42
43static SMUTEX(mutex);
44static SCOND(cond);
45static int logfd = STDERR_FILENO;
46static int logsync = 1;
47static std::vector<logline, slice_allocator<logline> > queue;
48
49void set_logfd (int fd)
50{
51 SMUTEX_LOCK (mutex);
52 logfd = fd < 0 ? STDERR_FILENO : fd;
53 SMUTEX_UNLOCK (mutex);
54}
55
56#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1
57
58static void
59log_sync (logline &line)
60{
61 struct tm lt;
62 char pfx [PREFIX_LEN];
63
64 localtime_r (&line.tv.tv_sec, &lt);
65
66 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d",
67 lt.tm_year + 1900,
68 lt.tm_mon + 1,
69 lt.tm_mday,
70 lt.tm_hour,
71 lt.tm_min,
72 lt.tm_sec,
73 (int)(line.tv.tv_usec / 100)
74 );
75
76 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' ';
77
78 struct iovec iov [2];
79
80 iov [0].iov_base = pfx;
81 iov [0].iov_len = PREFIX_LEN;
82
83 char *buf = line.buf;
84
85 while (char *end = strchr (buf, '\n'))
86 {
87 iov [1].iov_base = buf;
88 iov [1].iov_len = end - buf + 1;
89
90 writev (STDERR_FILENO, iov, 2);
91 if (logfd != STDERR_FILENO)
92 writev (logfd, iov, 2);
93
94 buf = end + 1;
95
96 if (buf == line.buf + line.len)
97 break;
98
99 pfx [PREFIX_LEN - 1] = '+';
100 }
101
102 sfree (line.buf, line.len);
103}
104
105static void *
106logthread_proc (void *arg)
107{
108 int idx = 0;
109
110 for (;;)
111 {
112 logline line;
113
114 SMUTEX_LOCK (mutex);
115
116 while (queue.empty ())
117 SCOND_WAIT (cond, mutex);
118
119 line = queue [idx++];
120
121 // this algorithm could result in an ever-increasing vector
122 // size if we log faster than we can write, but if that happens
123 // we have bigger problems.
124 if (idx == queue.size ())
125 {
126 queue.clear ();
127 idx = 0;
128 }
129
130 SMUTEX_UNLOCK (mutex);
131
132 log_sync (line);
133 }
134}
135
39void 136void
40LOG (LogLevel logLevel, const char *format, ...) 137log_cleanup ()
41{ 138{
139 logsync = 1;
140
141 for (;;)
142 {
143 int done;
144
145 SMUTEX_LOCK (mutex);
146 done = queue.empty ();
147 SMUTEX_UNLOCK (mutex);
148
149 if (done)
150 break;
151
152 usleep (10000);
153 }
154}
155
156static void
157af_child ()
158{
159 logsync = 1;
160}
161
162static struct logthread : thread
163{
164 logthread ()
165 {
166 pthread_atfork (0, 0, af_child);
167 start (logthread_proc);
168 logsync = 0;
169 }
170} logthread;
171
172void
173LOG (int flags, const_utf8_string format, ...)
174{
175 int level = flags & 15;
176
42 if (logLevel <= settings.debug) 177 if (level > settings.debug)
43 { 178 return;
44 char buf[20480]; /* This needs to be really really big - larger
45 * than any other buffer, since that buffer may
46 * need to be put in this one.
47 */
48 char msec[20];
49 char tbuf[80];
50 179
51 struct timeval tv; 180 logline line;
52 gettimeofday (&tv, 0); 181 gettimeofday (&line.tv, 0);
53 struct tm *ptime = localtime (&tv.tv_sec);
54 182
55 strftime (tbuf, sizeof (tbuf), "%Y-%m-%d %H:%M:%S", ptime); 183 static dynbuf_text buf;
56 sprintf (msec, ".%03d ", (int)(tv.tv_usec / 1000));
57 184
58 va_list ap; 185 va_list ap;
59 va_start (ap, format); 186 va_start (ap, format);
60 vsnprintf (buf, sizeof (buf), format, ap); 187 buf.vprintf (format, ap);
61 va_end (ap); 188 va_end (ap);
62 189
63 strncat (tbuf, msec, sizeof (buf)); 190 buf << '\n';
64 strncat (tbuf, buf, sizeof (buf));
65 191
66 fputs (tbuf, logfile); 192 line.buf = buf.linearise ();
67 fputs (tbuf, stderr); 193 line.len = buf.size ();
194
195 if (line.buf [line.len - 2] == '\n')
196 --line.len;
197
198 line.buf = salloc<char> (line.len, line.buf);
199
200 buf.clear ();
201
202 if (logsync)
203 flags |= logSync;
204
205 if (flags & logBacktrace)
68 } 206 {
207 line.buf [line.len - 1] = 0;
208 log_backtrace (line.buf);
209 line.buf [line.len - 1] = '\n';
210 }
211
212 line.flags = flags;
213
214 if (line.flags & logSync)
215 log_sync (line);
216 else
217 {
218 SMUTEX_LOCK (mutex);
219 queue.push_back (line);
220 SMUTEX_UNLOCK (mutex);
221 SCOND_SIGNAL (cond);
222 }
69} 223}
224

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines