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.2 by root, Sun Aug 27 16:15:11 2006 UTC vs.
Revision 1.17 by root, Tue Apr 15 03:16:02 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines