| 1 |
elmex |
1.1 |
/* |
| 2 |
root |
1.16 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
| 3 |
pippijn |
1.11 |
* |
| 4 |
root |
1.27 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
| 5 |
pippijn |
1.11 |
* |
| 6 |
root |
1.20 |
* Deliantra is free software: you can redistribute it and/or modify it under |
| 7 |
|
|
* the terms of the Affero GNU General Public License as published by the |
| 8 |
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
| 9 |
|
|
* option) any later version. |
| 10 |
pippijn |
1.11 |
* |
| 11 |
root |
1.15 |
* This program is distributed in the hope that it will be useful, |
| 12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
|
|
* GNU General Public License for more details. |
| 15 |
pippijn |
1.11 |
* |
| 16 |
root |
1.20 |
* You should have received a copy of the Affero GNU General Public License |
| 17 |
|
|
* and the GNU General Public License along with this program. If not, see |
| 18 |
|
|
* <http://www.gnu.org/licenses/>. |
| 19 |
root |
1.13 |
* |
| 20 |
root |
1.16 |
* The authors can be reached via e-mail to <support@deliantra.net> |
| 21 |
pippijn |
1.11 |
*/ |
| 22 |
elmex |
1.1 |
|
| 23 |
root |
1.12 |
#include <cstdarg> |
| 24 |
root |
1.17 |
#include <cstring> |
| 25 |
|
|
|
| 26 |
|
|
#include <vector> |
| 27 |
|
|
|
| 28 |
|
|
#include <sys/uio.h> |
| 29 |
|
|
#include <pthread.h> |
| 30 |
|
|
|
| 31 |
|
|
#include "global.h" |
| 32 |
|
|
#include "dynbuf.h" |
| 33 |
|
|
#include "util.h" |
| 34 |
|
|
|
| 35 |
|
|
struct logline |
| 36 |
|
|
{ |
| 37 |
|
|
struct timeval tv; |
| 38 |
|
|
char *buf; // includes PREFIX_LEN garbage bytes |
| 39 |
|
|
int len; |
| 40 |
|
|
int flags; |
| 41 |
|
|
}; |
| 42 |
|
|
|
| 43 |
root |
1.25 |
typedef std::vector<logline, slice_allocator<logline> > logvector; |
| 44 |
|
|
|
| 45 |
root |
1.17 |
static SMUTEX(mutex); |
| 46 |
root |
1.24 |
static SMUTEX(fdlock); |
| 47 |
root |
1.17 |
static SCOND(cond); |
| 48 |
|
|
static int logfd = STDERR_FILENO; |
| 49 |
|
|
static int logsync = 1; |
| 50 |
root |
1.25 |
static logvector queue; |
| 51 |
root |
1.17 |
|
| 52 |
root |
1.24 |
int log_setfd (int fd) |
| 53 |
root |
1.17 |
{ |
| 54 |
root |
1.24 |
SMUTEX_LOCK (fdlock); |
| 55 |
|
|
int old = logfd; |
| 56 |
root |
1.17 |
logfd = fd < 0 ? STDERR_FILENO : fd; |
| 57 |
root |
1.24 |
SMUTEX_UNLOCK (fdlock); |
| 58 |
|
|
|
| 59 |
|
|
return old; |
| 60 |
root |
1.17 |
} |
| 61 |
|
|
|
| 62 |
root |
1.26 |
#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000 L+") - 1 |
| 63 |
root |
1.17 |
|
| 64 |
|
|
static void |
| 65 |
|
|
log_sync (logline &line) |
| 66 |
|
|
{ |
| 67 |
root |
1.26 |
static const char levelchar [16+1] = "EWIDt???????????"; |
| 68 |
root |
1.18 |
struct tm lt; |
| 69 |
root |
1.17 |
char pfx [PREFIX_LEN]; |
| 70 |
|
|
|
| 71 |
root |
1.18 |
localtime_r (&line.tv.tv_sec, <); |
| 72 |
|
|
|
| 73 |
root |
1.26 |
sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d %c", |
| 74 |
root |
1.18 |
lt.tm_year + 1900, |
| 75 |
|
|
lt.tm_mon + 1, |
| 76 |
|
|
lt.tm_mday, |
| 77 |
|
|
lt.tm_hour, |
| 78 |
|
|
lt.tm_min, |
| 79 |
|
|
lt.tm_sec, |
| 80 |
root |
1.26 |
(int)(line.tv.tv_usec / 100), |
| 81 |
|
|
levelchar [line.flags & 15] |
| 82 |
root |
1.17 |
); |
| 83 |
|
|
|
| 84 |
|
|
pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' '; |
| 85 |
|
|
|
| 86 |
|
|
struct iovec iov [2]; |
| 87 |
|
|
|
| 88 |
|
|
iov [0].iov_base = pfx; |
| 89 |
|
|
iov [0].iov_len = PREFIX_LEN; |
| 90 |
|
|
|
| 91 |
|
|
char *buf = line.buf; |
| 92 |
|
|
|
| 93 |
root |
1.24 |
if (logsync != 2) |
| 94 |
|
|
SMUTEX_LOCK (fdlock); |
| 95 |
|
|
|
| 96 |
root |
1.17 |
while (char *end = strchr (buf, '\n')) |
| 97 |
|
|
{ |
| 98 |
|
|
iov [1].iov_base = buf; |
| 99 |
|
|
iov [1].iov_len = end - buf + 1; |
| 100 |
|
|
|
| 101 |
|
|
writev (STDERR_FILENO, iov, 2); |
| 102 |
|
|
if (logfd != STDERR_FILENO) |
| 103 |
|
|
writev (logfd, iov, 2); |
| 104 |
|
|
|
| 105 |
|
|
buf = end + 1; |
| 106 |
|
|
|
| 107 |
|
|
if (buf == line.buf + line.len) |
| 108 |
|
|
break; |
| 109 |
|
|
|
| 110 |
|
|
pfx [PREFIX_LEN - 1] = '+'; |
| 111 |
|
|
} |
| 112 |
|
|
|
| 113 |
root |
1.24 |
if (logsync != 2) |
| 114 |
|
|
SMUTEX_UNLOCK (fdlock); |
| 115 |
|
|
|
| 116 |
root |
1.17 |
sfree (line.buf, line.len); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
static void * |
| 120 |
|
|
logthread_proc (void *arg) |
| 121 |
|
|
{ |
| 122 |
|
|
int idx = 0; |
| 123 |
|
|
|
| 124 |
|
|
for (;;) |
| 125 |
|
|
{ |
| 126 |
|
|
logline line; |
| 127 |
|
|
|
| 128 |
|
|
SMUTEX_LOCK (mutex); |
| 129 |
|
|
|
| 130 |
|
|
while (queue.empty ()) |
| 131 |
|
|
SCOND_WAIT (cond, mutex); |
| 132 |
|
|
|
| 133 |
|
|
line = queue [idx++]; |
| 134 |
|
|
|
| 135 |
|
|
// this algorithm could result in an ever-increasing vector |
| 136 |
|
|
// size if we log faster than we can write, but if that happens |
| 137 |
|
|
// we have bigger problems. |
| 138 |
|
|
if (idx == queue.size ()) |
| 139 |
|
|
{ |
| 140 |
root |
1.25 |
if (idx < 32) |
| 141 |
|
|
queue.clear (); |
| 142 |
|
|
else |
| 143 |
|
|
logvector ().swap (queue); // free memory, hopefully |
| 144 |
|
|
|
| 145 |
root |
1.17 |
idx = 0; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
|
SMUTEX_UNLOCK (mutex); |
| 149 |
|
|
|
| 150 |
|
|
log_sync (line); |
| 151 |
|
|
} |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
void |
| 155 |
|
|
log_cleanup () |
| 156 |
|
|
{ |
| 157 |
|
|
logsync = 1; |
| 158 |
root |
1.25 |
SMUTEX_UNLOCK (fdlock); |
| 159 |
root |
1.17 |
|
| 160 |
|
|
for (;;) |
| 161 |
|
|
{ |
| 162 |
|
|
int done; |
| 163 |
|
|
|
| 164 |
|
|
SMUTEX_LOCK (mutex); |
| 165 |
|
|
done = queue.empty (); |
| 166 |
|
|
SMUTEX_UNLOCK (mutex); |
| 167 |
|
|
|
| 168 |
|
|
if (done) |
| 169 |
|
|
break; |
| 170 |
|
|
|
| 171 |
|
|
usleep (10000); |
| 172 |
root |
1.24 |
SMUTEX_LOCK (fdlock); |
| 173 |
|
|
SMUTEX_UNLOCK (fdlock); |
| 174 |
root |
1.17 |
} |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
static void |
| 178 |
|
|
af_child () |
| 179 |
|
|
{ |
| 180 |
root |
1.24 |
logsync = 2; |
| 181 |
root |
1.17 |
} |
| 182 |
|
|
|
| 183 |
|
|
static struct logthread : thread |
| 184 |
|
|
{ |
| 185 |
|
|
logthread () |
| 186 |
|
|
{ |
| 187 |
|
|
pthread_atfork (0, 0, af_child); |
| 188 |
|
|
start (logthread_proc); |
| 189 |
|
|
logsync = 0; |
| 190 |
|
|
} |
| 191 |
|
|
} logthread; |
| 192 |
elmex |
1.1 |
|
| 193 |
root |
1.4 |
void |
| 194 |
root |
1.22 |
LOG (int flags, const_utf8_string format, ...) |
| 195 |
elmex |
1.1 |
{ |
| 196 |
root |
1.12 |
int level = flags & 15; |
| 197 |
|
|
|
| 198 |
|
|
if (level > settings.debug) |
| 199 |
|
|
return; |
| 200 |
|
|
|
| 201 |
root |
1.17 |
logline line; |
| 202 |
|
|
gettimeofday (&line.tv, 0); |
| 203 |
root |
1.12 |
|
| 204 |
root |
1.17 |
static dynbuf_text buf; |
| 205 |
root |
1.12 |
|
| 206 |
|
|
va_list ap; |
| 207 |
|
|
va_start (ap, format); |
| 208 |
root |
1.17 |
buf.vprintf (format, ap); |
| 209 |
root |
1.12 |
va_end (ap); |
| 210 |
|
|
|
| 211 |
root |
1.17 |
buf << '\n'; |
| 212 |
|
|
|
| 213 |
|
|
line.buf = buf.linearise (); |
| 214 |
|
|
line.len = buf.size (); |
| 215 |
root |
1.12 |
|
| 216 |
root |
1.17 |
if (line.buf [line.len - 2] == '\n') |
| 217 |
|
|
--line.len; |
| 218 |
root |
1.14 |
|
| 219 |
root |
1.17 |
line.buf = salloc<char> (line.len, line.buf); |
| 220 |
|
|
|
| 221 |
|
|
buf.clear (); |
| 222 |
|
|
|
| 223 |
|
|
if (logsync) |
| 224 |
|
|
flags |= logSync; |
| 225 |
root |
1.12 |
|
| 226 |
|
|
if (flags & logBacktrace) |
| 227 |
root |
1.17 |
{ |
| 228 |
|
|
line.buf [line.len - 1] = 0; |
| 229 |
|
|
log_backtrace (line.buf); |
| 230 |
|
|
line.buf [line.len - 1] = '\n'; |
| 231 |
|
|
} |
| 232 |
|
|
|
| 233 |
|
|
line.flags = flags; |
| 234 |
|
|
|
| 235 |
|
|
if (line.flags & logSync) |
| 236 |
|
|
log_sync (line); |
| 237 |
|
|
else |
| 238 |
|
|
{ |
| 239 |
|
|
SMUTEX_LOCK (mutex); |
| 240 |
|
|
queue.push_back (line); |
| 241 |
|
|
SMUTEX_UNLOCK (mutex); |
| 242 |
|
|
SCOND_SIGNAL (cond); |
| 243 |
|
|
} |
| 244 |
elmex |
1.1 |
} |
| 245 |
root |
1.17 |
|
| 246 |
root |
1.24 |
static int suspended; |
| 247 |
|
|
|
| 248 |
|
|
void |
| 249 |
|
|
log_suspend () |
| 250 |
|
|
{ |
| 251 |
|
|
if (!suspended++) |
| 252 |
|
|
{ |
| 253 |
|
|
LOG (llevDebug, "logging suspended."); |
| 254 |
|
|
SMUTEX_LOCK (fdlock); |
| 255 |
|
|
} |
| 256 |
|
|
} |
| 257 |
|
|
|
| 258 |
|
|
void |
| 259 |
|
|
log_resume () |
| 260 |
|
|
{ |
| 261 |
|
|
if (!--suspended) |
| 262 |
|
|
{ |
| 263 |
|
|
SMUTEX_UNLOCK (fdlock); |
| 264 |
|
|
LOG (llevDebug, "logging resumed."); |
| 265 |
|
|
} |
| 266 |
|
|
} |
| 267 |
|
|
|