1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 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 | * |
5 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * it under the terms of the GNU General Public License as published by |
7 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation, either version 3 of the License, or |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
9 | * option) any later version. |
12 | * |
10 | * |
13 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
17 | * |
15 | * |
18 | * You should have received a copy of the GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | * and the GNU General Public License along with this program. If not, see |
|
|
18 | * <http://www.gnu.org/licenses/>. |
20 | * |
19 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
21 | */ |
23 | |
22 | |
24 | #include <cstdarg> |
23 | #include <cstdarg> |
… | |
… | |
40 | int len; |
39 | int len; |
41 | int flags; |
40 | int flags; |
42 | }; |
41 | }; |
43 | |
42 | |
44 | static SMUTEX(mutex); |
43 | static SMUTEX(mutex); |
|
|
44 | static SMUTEX(fdlock); |
45 | static SCOND(cond); |
45 | static SCOND(cond); |
46 | static int logfd = STDERR_FILENO; |
46 | static int logfd = STDERR_FILENO; |
47 | static int logsync = 1; |
47 | static int logsync = 1; |
48 | static std::vector<logline, slice_allocator<logline> > queue; |
48 | static std::vector<logline, slice_allocator<logline> > queue; |
49 | |
49 | |
50 | void set_logfd (int fd) |
50 | int log_setfd (int fd) |
51 | { |
51 | { |
52 | SMUTEX_LOCK (mutex); |
52 | SMUTEX_LOCK (fdlock); |
|
|
53 | int old = logfd; |
53 | logfd = fd < 0 ? STDERR_FILENO : fd; |
54 | logfd = fd < 0 ? STDERR_FILENO : fd; |
54 | SMUTEX_UNLOCK (mutex); |
55 | SMUTEX_UNLOCK (fdlock); |
|
|
56 | |
|
|
57 | return old; |
55 | } |
58 | } |
56 | |
59 | |
57 | #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 |
60 | #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 |
58 | |
61 | |
59 | static void |
62 | static void |
… | |
… | |
81 | iov [0].iov_base = pfx; |
84 | iov [0].iov_base = pfx; |
82 | iov [0].iov_len = PREFIX_LEN; |
85 | iov [0].iov_len = PREFIX_LEN; |
83 | |
86 | |
84 | char *buf = line.buf; |
87 | char *buf = line.buf; |
85 | |
88 | |
|
|
89 | if (logsync != 2) |
|
|
90 | SMUTEX_LOCK (fdlock); |
|
|
91 | |
86 | while (char *end = strchr (buf, '\n')) |
92 | while (char *end = strchr (buf, '\n')) |
87 | { |
93 | { |
88 | iov [1].iov_base = buf; |
94 | iov [1].iov_base = buf; |
89 | iov [1].iov_len = end - buf + 1; |
95 | iov [1].iov_len = end - buf + 1; |
90 | |
96 | |
… | |
… | |
97 | if (buf == line.buf + line.len) |
103 | if (buf == line.buf + line.len) |
98 | break; |
104 | break; |
99 | |
105 | |
100 | pfx [PREFIX_LEN - 1] = '+'; |
106 | pfx [PREFIX_LEN - 1] = '+'; |
101 | } |
107 | } |
|
|
108 | |
|
|
109 | if (logsync != 2) |
|
|
110 | SMUTEX_UNLOCK (fdlock); |
102 | |
111 | |
103 | sfree (line.buf, line.len); |
112 | sfree (line.buf, line.len); |
104 | } |
113 | } |
105 | |
114 | |
106 | static void * |
115 | static void * |
… | |
… | |
149 | |
158 | |
150 | if (done) |
159 | if (done) |
151 | break; |
160 | break; |
152 | |
161 | |
153 | usleep (10000); |
162 | usleep (10000); |
|
|
163 | SMUTEX_LOCK (fdlock); |
|
|
164 | SMUTEX_UNLOCK (fdlock); |
154 | } |
165 | } |
155 | } |
166 | } |
156 | |
167 | |
157 | static void |
168 | static void |
158 | af_child () |
169 | af_child () |
159 | { |
170 | { |
160 | logsync = 1; |
171 | logsync = 2; |
161 | } |
172 | } |
162 | |
173 | |
163 | static struct logthread : thread |
174 | static struct logthread : thread |
164 | { |
175 | { |
165 | logthread () |
176 | logthread () |
… | |
… | |
169 | logsync = 0; |
180 | logsync = 0; |
170 | } |
181 | } |
171 | } logthread; |
182 | } logthread; |
172 | |
183 | |
173 | void |
184 | void |
174 | LOG (int flags, const char *format, ...) |
185 | LOG (int flags, const_utf8_string format, ...) |
175 | { |
186 | { |
176 | int level = flags & 15; |
187 | int level = flags & 15; |
177 | |
188 | |
178 | if (level > settings.debug) |
189 | if (level > settings.debug) |
179 | return; |
190 | return; |
… | |
… | |
221 | SMUTEX_UNLOCK (mutex); |
232 | SMUTEX_UNLOCK (mutex); |
222 | SCOND_SIGNAL (cond); |
233 | SCOND_SIGNAL (cond); |
223 | } |
234 | } |
224 | } |
235 | } |
225 | |
236 | |
|
|
237 | static int suspended; |
|
|
238 | |
|
|
239 | void |
|
|
240 | log_suspend () |
|
|
241 | { |
|
|
242 | if (!suspended++) |
|
|
243 | { |
|
|
244 | LOG (llevDebug, "logging suspended."); |
|
|
245 | SMUTEX_LOCK (fdlock); |
|
|
246 | } |
|
|
247 | } |
|
|
248 | |
|
|
249 | void |
|
|
250 | log_resume () |
|
|
251 | { |
|
|
252 | if (!--suspended) |
|
|
253 | { |
|
|
254 | SMUTEX_UNLOCK (fdlock); |
|
|
255 | LOG (llevDebug, "logging resumed."); |
|
|
256 | } |
|
|
257 | } |
|
|
258 | |