… | |
… | |
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; |
… | |
… | |
131 | // this algorithm could result in an ever-increasing vector |
133 | // this algorithm could result in an ever-increasing vector |
132 | // 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 |
133 | // we have bigger problems. |
135 | // we have bigger problems. |
134 | if (idx == queue.size ()) |
136 | if (idx == queue.size ()) |
135 | { |
137 | { |
|
|
138 | if (idx < 32) |
136 | queue.clear (); |
139 | queue.clear (); |
|
|
140 | else |
|
|
141 | logvector ().swap (queue); // free memory, hopefully |
|
|
142 | |
137 | idx = 0; |
143 | idx = 0; |
138 | } |
144 | } |
139 | |
145 | |
140 | SMUTEX_UNLOCK (mutex); |
146 | SMUTEX_UNLOCK (mutex); |
141 | |
147 | |
… | |
… | |
145 | |
151 | |
146 | void |
152 | void |
147 | log_cleanup () |
153 | log_cleanup () |
148 | { |
154 | { |
149 | logsync = 1; |
155 | logsync = 1; |
|
|
156 | SMUTEX_UNLOCK (fdlock); |
150 | |
157 | |
151 | for (;;) |
158 | for (;;) |
152 | { |
159 | { |
153 | int done; |
160 | int done; |
154 | |
161 | |