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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines