ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/logger.C
Revision: 1.17
Committed: Tue Apr 15 03:16:02 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_5, rel-2_52
Changes since 1.16: +179 -17 lines
Log Message:
better logging, remove cruft

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
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>
22 */
23
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"
33 #include "dynbuf.h"
34 #include "util.h"
35
36 struct logline
37 {
38 struct timeval tv;
39 char *buf; // includes PREFIX_LEN garbage bytes
40 int len;
41 int flags;
42 };
43
44 static SMUTEX(mutex);
45 static SCOND(cond);
46 static int logfd = STDERR_FILENO;
47 static int logsync = 1;
48 static std::vector<logline, slice_allocator<logline> > queue;
49
50 void 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
59 static void
60 log_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
104 static void *
105 logthread_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
135 void
136 log_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
155 static void
156 af_child ()
157 {
158 logsync = 1;
159 }
160
161 static 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
171 void
172 LOG (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;
183
184 va_list ap;
185 va_start (ap, format);
186 buf.vprintf (format, 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 }
222 }
223