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 it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
9 | * option) any later version. |
… | |
… | |
40 | char *buf; // includes PREFIX_LEN garbage bytes |
38 | char *buf; // includes PREFIX_LEN garbage bytes |
41 | int len; |
39 | int len; |
42 | int flags; |
40 | int flags; |
43 | }; |
41 | }; |
44 | |
42 | |
|
|
43 | typedef std::vector<logline, slice_allocator<logline> > logvector; |
|
|
44 | |
45 | static SMUTEX(mutex); |
45 | static SMUTEX(mutex); |
|
|
46 | static SMUTEX(fdlock); |
46 | static SCOND(cond); |
47 | static SCOND(cond); |
47 | static int logfd = STDERR_FILENO; |
48 | static int logfd = STDERR_FILENO; |
48 | static int logsync = 1; |
49 | static int logsync = 1; |
49 | static std::vector<logline, slice_allocator<logline> > queue; |
50 | static logvector queue; |
50 | |
51 | |
51 | void set_logfd (int fd) |
52 | int log_setfd (int fd) |
52 | { |
53 | { |
53 | SMUTEX_LOCK (mutex); |
54 | SMUTEX_LOCK (fdlock); |
|
|
55 | int old = logfd; |
54 | logfd = fd < 0 ? STDERR_FILENO : fd; |
56 | logfd = fd < 0 ? STDERR_FILENO : fd; |
55 | SMUTEX_UNLOCK (mutex); |
57 | SMUTEX_UNLOCK (fdlock); |
|
|
58 | |
|
|
59 | return old; |
56 | } |
60 | } |
57 | |
61 | |
58 | #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 |
62 | #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 |
59 | |
63 | |
60 | static void |
64 | static void |
… | |
… | |
82 | iov [0].iov_base = pfx; |
86 | iov [0].iov_base = pfx; |
83 | iov [0].iov_len = PREFIX_LEN; |
87 | iov [0].iov_len = PREFIX_LEN; |
84 | |
88 | |
85 | char *buf = line.buf; |
89 | char *buf = line.buf; |
86 | |
90 | |
|
|
91 | if (logsync != 2) |
|
|
92 | SMUTEX_LOCK (fdlock); |
|
|
93 | |
87 | while (char *end = strchr (buf, '\n')) |
94 | while (char *end = strchr (buf, '\n')) |
88 | { |
95 | { |
89 | iov [1].iov_base = buf; |
96 | iov [1].iov_base = buf; |
90 | iov [1].iov_len = end - buf + 1; |
97 | iov [1].iov_len = end - buf + 1; |
91 | |
98 | |
… | |
… | |
98 | if (buf == line.buf + line.len) |
105 | if (buf == line.buf + line.len) |
99 | break; |
106 | break; |
100 | |
107 | |
101 | pfx [PREFIX_LEN - 1] = '+'; |
108 | pfx [PREFIX_LEN - 1] = '+'; |
102 | } |
109 | } |
|
|
110 | |
|
|
111 | if (logsync != 2) |
|
|
112 | SMUTEX_UNLOCK (fdlock); |
103 | |
113 | |
104 | sfree (line.buf, line.len); |
114 | sfree (line.buf, line.len); |
105 | } |
115 | } |
106 | |
116 | |
107 | static void * |
117 | static void * |
… | |
… | |
123 | // this algorithm could result in an ever-increasing vector |
133 | // this algorithm could result in an ever-increasing vector |
124 | // size if we log faster than we can write, but if that happens |
134 | // size if we log faster than we can write, but if that happens |
125 | // we have bigger problems. |
135 | // we have bigger problems. |
126 | if (idx == queue.size ()) |
136 | if (idx == queue.size ()) |
127 | { |
137 | { |
|
|
138 | if (idx < 32) |
128 | queue.clear (); |
139 | queue.clear (); |
|
|
140 | else |
|
|
141 | logvector ().swap (queue); // free memory, hopefully |
|
|
142 | |
129 | idx = 0; |
143 | idx = 0; |
130 | } |
144 | } |
131 | |
145 | |
132 | SMUTEX_UNLOCK (mutex); |
146 | SMUTEX_UNLOCK (mutex); |
133 | |
147 | |
… | |
… | |
137 | |
151 | |
138 | void |
152 | void |
139 | log_cleanup () |
153 | log_cleanup () |
140 | { |
154 | { |
141 | logsync = 1; |
155 | logsync = 1; |
|
|
156 | SMUTEX_UNLOCK (fdlock); |
142 | |
157 | |
143 | for (;;) |
158 | for (;;) |
144 | { |
159 | { |
145 | int done; |
160 | int done; |
146 | |
161 | |
… | |
… | |
150 | |
165 | |
151 | if (done) |
166 | if (done) |
152 | break; |
167 | break; |
153 | |
168 | |
154 | usleep (10000); |
169 | usleep (10000); |
|
|
170 | SMUTEX_LOCK (fdlock); |
|
|
171 | SMUTEX_UNLOCK (fdlock); |
155 | } |
172 | } |
156 | } |
173 | } |
157 | |
174 | |
158 | static void |
175 | static void |
159 | af_child () |
176 | af_child () |
160 | { |
177 | { |
161 | logsync = 1; |
178 | logsync = 2; |
162 | } |
179 | } |
163 | |
180 | |
164 | static struct logthread : thread |
181 | static struct logthread : thread |
165 | { |
182 | { |
166 | logthread () |
183 | logthread () |
… | |
… | |
170 | logsync = 0; |
187 | logsync = 0; |
171 | } |
188 | } |
172 | } logthread; |
189 | } logthread; |
173 | |
190 | |
174 | void |
191 | void |
175 | LOG (int flags, const char *format, ...) |
192 | LOG (int flags, const_utf8_string format, ...) |
176 | { |
193 | { |
177 | int level = flags & 15; |
194 | int level = flags & 15; |
178 | |
195 | |
179 | if (level > settings.debug) |
196 | if (level > settings.debug) |
180 | return; |
197 | return; |
… | |
… | |
222 | SMUTEX_UNLOCK (mutex); |
239 | SMUTEX_UNLOCK (mutex); |
223 | SCOND_SIGNAL (cond); |
240 | SCOND_SIGNAL (cond); |
224 | } |
241 | } |
225 | } |
242 | } |
226 | |
243 | |
|
|
244 | static int suspended; |
|
|
245 | |
|
|
246 | void |
|
|
247 | log_suspend () |
|
|
248 | { |
|
|
249 | if (!suspended++) |
|
|
250 | { |
|
|
251 | LOG (llevDebug, "logging suspended."); |
|
|
252 | SMUTEX_LOCK (fdlock); |
|
|
253 | } |
|
|
254 | } |
|
|
255 | |
|
|
256 | void |
|
|
257 | log_resume () |
|
|
258 | { |
|
|
259 | if (!--suspended) |
|
|
260 | { |
|
|
261 | SMUTEX_UNLOCK (fdlock); |
|
|
262 | LOG (llevDebug, "logging resumed."); |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |