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 | |
|
|
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 | * |
20 | * |
35 | * See include/logger.h for possible logLevels. Messages with llevInfo |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
36 | * and llevError are always printed, regardless of debug mode. |
|
|
37 | */ |
22 | */ |
38 | |
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 | |
39 | void |
135 | void |
40 | LOG (LogLevel logLevel, const char *format, ...) |
136 | log_cleanup () |
41 | { |
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 | |
42 | if (logLevel <= settings.debug) |
176 | if (level > settings.debug) |
43 | { |
177 | 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 tbuf[20480]; |
|
|
49 | |
178 | |
50 | time_t curtime = time (NULL); |
179 | logline line; |
51 | struct tm *ptime = localtime (&curtime); |
180 | gettimeofday (&line.tv, 0); |
52 | |
181 | |
53 | strftime (tbuf, 256, "%Y-%m-%d %H:%M:%S ", ptime); |
182 | static dynbuf_text buf; |
54 | |
183 | |
55 | va_list ap; |
184 | va_list ap; |
56 | |
|
|
57 | va_start (ap, format); |
185 | va_start (ap, format); |
58 | |
186 | buf.vprintf (format, ap); |
59 | vsnprintf (buf, sizeof (buf), format, ap); |
|
|
60 | strncat (tbuf, buf, 20460); |
|
|
61 | fputs (tbuf, logfile); |
|
|
62 | fputs (tbuf, stderr); |
|
|
63 | |
|
|
64 | 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) |
65 | } |
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 | } |
66 | } |
222 | } |
|
|
223 | |