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.24 by root, Sun Apr 11 17:27:51 2010 UTC vs.
Revision 1.28 by root, Tue Jan 3 11:25:31 2012 UTC

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
43typedef std::vector<logline, slice_allocator<logline> > logvector;
44
43static SMUTEX(mutex); 45static SMUTEX(mutex);
44static SMUTEX(fdlock); 46static SMUTEX(fdlock);
45static SCOND(cond); 47static SCOND(cond);
46static int logfd = STDERR_FILENO; 48static int logfd = STDERR_FILENO;
47static int logsync = 1; 49static int logsync = 1;
48static std::vector<logline, slice_allocator<logline> > queue; 50static logvector queue;
49 51
50int log_setfd (int fd) 52int 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
62static void 64static void
63log_sync (logline &line) 65log_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, &lt); 71 localtime_r (&line.tv.tv_sec, &lt);
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
146void 154void
147log_cleanup () 155log_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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines