ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/logger.C
(Generate patch)

Comparing deliantra/server/common/logger.C (file contents):
Revision 1.16 by root, Thu Nov 8 19:43:23 2007 UTC vs.
Revision 1.17 by root, Tue Apr 15 03:16:02 2008 UTC

20 * 20 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24#include <cstdarg> 24#include <cstdarg>
25#include <cstring>
26
27#include <vector>
28
29#include <sys/uio.h>
30#include <pthread.h>
31
25#include <global.h> 32#include "global.h"
26#include <funcpoint.h> 33#include "dynbuf.h"
34#include "util.h"
35
36struct logline
37{
38 struct timeval tv;
39 char *buf; // includes PREFIX_LEN garbage bytes
40 int len;
41 int flags;
42};
43
44static SMUTEX(mutex);
45static SCOND(cond);
46static int logfd = STDERR_FILENO;
47static int logsync = 1;
48static std::vector<logline, slice_allocator<logline> > queue;
49
50void set_logfd (int fd)
51{
52 SMUTEX_LOCK (mutex);
53 logfd = fd < 0 ? STDERR_FILENO : fd;
54 SMUTEX_UNLOCK (mutex);
55}
56
57#define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1
58
59static void
60log_sync (logline &line)
61{
62 struct tm *lt = localtime (&line.tv.tv_sec);
63 char pfx [PREFIX_LEN];
64
65 sprintf (pfx, "%04d-%02d-%02d %02d:%02d:%02d.%04d",
66 lt->tm_year + 1900,
67 lt->tm_mon + 1,
68 lt->tm_mday,
69 lt->tm_hour,
70 lt->tm_min,
71 lt->tm_sec,
72 (int)(line.tv.tv_usec / 100)
73 );
74
75 pfx [PREFIX_LEN - 1] = line.flags & logSync ? '=' : ' ';
76
77 struct iovec iov [2];
78
79 iov [0].iov_base = pfx;
80 iov [0].iov_len = PREFIX_LEN;
81
82 char *buf = line.buf;
83
84 while (char *end = strchr (buf, '\n'))
85 {
86 iov [1].iov_base = buf;
87 iov [1].iov_len = end - buf + 1;
88
89 writev (STDERR_FILENO, iov, 2);
90 if (logfd != STDERR_FILENO)
91 writev (logfd, iov, 2);
92
93 buf = end + 1;
94
95 if (buf == line.buf + line.len)
96 break;
97
98 pfx [PREFIX_LEN - 1] = '+';
99 }
100
101 sfree (line.buf, line.len);
102}
103
104static void *
105logthread_proc (void *arg)
106{
107 int idx = 0;
108
109 for (;;)
110 {
111 logline line;
112
113 SMUTEX_LOCK (mutex);
114
115 while (queue.empty ())
116 SCOND_WAIT (cond, mutex);
117
118 line = queue [idx++];
119
120 // this algorithm could result in an ever-increasing vector
121 // size if we log faster than we can write, but if that happens
122 // we have bigger problems.
123 if (idx == queue.size ())
124 {
125 queue.clear ();
126 idx = 0;
127 }
128
129 SMUTEX_UNLOCK (mutex);
130
131 log_sync (line);
132 }
133}
134
135void
136log_cleanup ()
137{
138 logsync = 1;
139
140 for (;;)
141 {
142 int done;
143
144 SMUTEX_LOCK (mutex);
145 done = queue.empty ();
146 SMUTEX_UNLOCK (mutex);
147
148 if (done)
149 break;
150
151 usleep (10000);
152 }
153}
154
155static void
156af_child ()
157{
158 logsync = 1;
159}
160
161static struct logthread : thread
162{
163 logthread ()
164 {
165 pthread_atfork (0, 0, af_child);
166 start (logthread_proc);
167 logsync = 0;
168 }
169} logthread;
27 170
28void 171void
29LOG (int flags, const char *format, ...) 172LOG (int flags, const char *format, ...)
30{ 173{
31 int level = flags & 15; 174 int level = flags & 15;
32 175
33 if (level > settings.debug) 176 if (level > settings.debug)
34 return; 177 return;
35 178
36 char buf[20480]; 179 logline line;
37
38 struct timeval tv;
39 gettimeofday (&tv, 0); 180 gettimeofday (&line.tv, 0);
40 struct tm *ptime = localtime (&tv.tv_sec);
41 181
42 int len = strftime (buf, sizeof (buf) - 7, "%Y-%m-%d %H:%M:%S", ptime); 182 static dynbuf_text buf;
43 len += sprintf (buf + len, ".%04d ", (int)(tv.tv_usec / 100));
44 183
45 va_list ap; 184 va_list ap;
46 va_start (ap, format); 185 va_start (ap, format);
47 len += vsnprintf (buf + len, sizeof (buf) - len, format, ap); 186 buf.vprintf (format, ap);
48 va_end (ap); 187 va_end (ap);
49 188
50 len = min (sizeof (buf) - 1, len); 189 buf << '\n';
51 190
191 line.buf = buf.linearise ();
192 line.len = buf.size ();
193
52 if (buf [len - 1] != '\n') 194 if (line.buf [line.len - 2] == '\n')
53 buf [len++] = '\n'; // not 0-terminated 195 --line.len;
54 196
55 write (STDERR_FILENO, buf, len); 197 line.buf = salloc<char> (line.len, line.buf);
56 if (logfile != stderr) 198
57 fwrite (buf, 1, len, logfile); 199 buf.clear ();
200
201 if (logsync)
202 flags |= logSync;
58 203
59 if (flags & logBacktrace) 204 if (flags & logBacktrace)
205 {
206 line.buf [line.len - 1] = 0;
60 log_backtrace (buf); 207 log_backtrace (line.buf);
208 line.buf [line.len - 1] = '\n';
209 }
210
211 line.flags = flags;
212
213 if (line.flags & logSync)
214 log_sync (line);
215 else
216 {
217 SMUTEX_LOCK (mutex);
218 queue.push_back (line);
219 SMUTEX_UNLOCK (mutex);
220 SCOND_SIGNAL (cond);
221 }
61} 222}
223

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines