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.3 2006/08/29 08:01:35 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> |
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 | /* |
36 | struct 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 | |
|
|
43 | void 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); |
44 | typedef std::vector<logline, slice_allocator<logline> > logvector; |
52 | struct tm *ptime = localtime (&curtime); |
|
|
53 | |
45 | |
54 | strftime (tbuf, 256, "%Y-%m-%d %H:%M:%S ", ptime); |
46 | static SMUTEX(mutex); |
|
|
47 | static SMUTEX(fdlock); |
|
|
48 | static SCOND(cond); |
|
|
49 | static int logfd = STDERR_FILENO; |
|
|
50 | static int logsync = 1; |
|
|
51 | static logvector queue; |
|
|
52 | |
|
|
53 | int 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 | |
|
|
65 | static void |
|
|
66 | log_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, <); |
|
|
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 | |
|
|
120 | static void * |
|
|
121 | logthread_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 | |
|
|
155 | void |
|
|
156 | log_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 | |
|
|
178 | static void |
|
|
179 | af_child () |
|
|
180 | { |
|
|
181 | logsync = 2; |
|
|
182 | } |
|
|
183 | |
|
|
184 | static 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 | |
|
|
194 | void |
|
|
195 | LOG (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; |
55 | |
206 | |
56 | va_list ap; |
207 | va_list ap; |
57 | va_start(ap, format); |
208 | va_start (ap, format); |
58 | |
209 | 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); |
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 | } |
82 | } |
245 | } |
|
|
246 | |
|
|
247 | static int suspended; |
|
|
248 | |
|
|
249 | void |
|
|
250 | log_suspend () |
|
|
251 | { |
|
|
252 | if (!suspended++) |
|
|
253 | { |
|
|
254 | LOG (llevDebug, "logging suspended."); |
|
|
255 | SMUTEX_LOCK (fdlock); |
|
|
256 | } |
|
|
257 | } |
|
|
258 | |
|
|
259 | void |
|
|
260 | log_resume () |
|
|
261 | { |
|
|
262 | if (!--suspended) |
|
|
263 | { |
|
|
264 | SMUTEX_UNLOCK (fdlock); |
|
|
265 | LOG (llevDebug, "logging resumed."); |
|
|
266 | } |
|
|
267 | } |
|
|
268 | |