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,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * 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 |
7 | * 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 |
8 | * 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 |
9 | * option) any later version. |
9 | * option) any later version. |
… | |
… | |
38 | char *buf; // includes PREFIX_LEN garbage bytes |
38 | char *buf; // includes PREFIX_LEN garbage bytes |
39 | int len; |
39 | int len; |
40 | int flags; |
40 | int flags; |
41 | }; |
41 | }; |
42 | |
42 | |
|
|
43 | typedef std::vector<logline, slice_allocator<logline> > logvector; |
|
|
44 | |
43 | static SMUTEX(mutex); |
45 | static SMUTEX(mutex); |
44 | static SMUTEX(fdlock); |
46 | static SMUTEX(fdlock); |
45 | static SCOND(cond); |
47 | static SCOND(cond); |
46 | static int logfd = STDERR_FILENO; |
48 | static int logfd = STDERR_FILENO; |
47 | static int logsync = 1; |
49 | static int logsync = 1; |
48 | static std::vector<logline, slice_allocator<logline> > queue; |
50 | static logvector queue; |
49 | |
51 | |
50 | int log_setfd (int fd) |
52 | int log_setfd (int fd) |
51 | { |
53 | { |
52 | SMUTEX_LOCK (fdlock); |
54 | SMUTEX_LOCK (fdlock); |
53 | int old = logfd; |
55 | int old = logfd; |
… | |
… | |
55 | SMUTEX_UNLOCK (fdlock); |
57 | SMUTEX_UNLOCK (fdlock); |
56 | |
58 | |
57 | return old; |
59 | return old; |
58 | } |
60 | } |
59 | |
61 | |
60 | #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 |
62 | #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000 L+") - 1 |
61 | |
63 | |
62 | static void |
64 | static void |
63 | log_sync (logline &line) |
65 | log_sync (logline &line) |
64 | { |
66 | { |
|
|
67 | static const char levelchar [16+1] = "EWIDt???????????"; |
65 | struct tm lt; |
68 | struct tm lt; |
66 | char pfx [PREFIX_LEN]; |
69 | char pfx [PREFIX_LEN]; |
67 | |
70 | |
68 | localtime_r (&line.tv.tv_sec, <); |
71 | localtime_r (&line.tv.tv_sec, <); |
69 | |
72 | |
70 | sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d", |
73 | sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d %c", |
71 | lt.tm_year + 1900, |
74 | lt.tm_year + 1900, |
72 | lt.tm_mon + 1, |
75 | lt.tm_mon + 1, |
73 | lt.tm_mday, |
76 | lt.tm_mday, |
74 | lt.tm_hour, |
77 | lt.tm_hour, |
75 | lt.tm_min, |
78 | lt.tm_min, |
76 | lt.tm_sec, |
79 | lt.tm_sec, |
77 | (int)(line.tv.tv_usec / 100) |
80 | (int)(line.tv.tv_usec / 100), |
|
|
81 | levelchar [line.flags & 15] |
78 | ); |
82 | ); |
79 | |
83 | |
80 | pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' '; |
84 | pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' '; |
81 | |
85 | |
82 | struct iovec iov [2]; |
86 | struct iovec iov [2]; |
… | |
… | |
131 | // this algorithm could result in an ever-increasing vector |
135 | // this algorithm could result in an ever-increasing vector |
132 | // size if we log faster than we can write, but if that happens |
136 | // size if we log faster than we can write, but if that happens |
133 | // we have bigger problems. |
137 | // we have bigger problems. |
134 | if (idx == queue.size ()) |
138 | if (idx == queue.size ()) |
135 | { |
139 | { |
|
|
140 | if (idx < 32) |
136 | queue.clear (); |
141 | queue.clear (); |
|
|
142 | else |
|
|
143 | logvector ().swap (queue); // free memory, hopefully |
|
|
144 | |
137 | idx = 0; |
145 | idx = 0; |
138 | } |
146 | } |
139 | |
147 | |
140 | SMUTEX_UNLOCK (mutex); |
148 | SMUTEX_UNLOCK (mutex); |
141 | |
149 | |
… | |
… | |
145 | |
153 | |
146 | void |
154 | void |
147 | log_cleanup () |
155 | log_cleanup () |
148 | { |
156 | { |
149 | logsync = 1; |
157 | logsync = 1; |
|
|
158 | SMUTEX_UNLOCK (fdlock); |
150 | |
159 | |
151 | for (;;) |
160 | for (;;) |
152 | { |
161 | { |
153 | int done; |
162 | int done; |
154 | |
163 | |