1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* Functions needed to implement general WvLog class.
*
* See wvlog.h for more information.
*/
#include "wvlogrcv.h"
#include "wvstringlist.h"
#include "strutils.h"
#include "wvfork.h"
#include <ctype.h>
#ifdef _WIN32
#include <io.h>
#define snprintf _snprintf
#endif
WvLogRcvBaseList WvLog::receivers;
int WvLog::num_receivers = 0, WvLog::num_logs = 0;
WvLogRcvBase *WvLog::default_receiver = NULL;
char *WvLogRcv::loglevels[WvLog::NUM_LOGLEVELS] = {
"Crit",
"Err",
"Warn",
"Notice",
"Info",
"*1",
"*2",
"*3",
"*4",
"*5",
};
/////////////////////////////////////// WvLog
WvLog::WvLog(WvStringParm _app, LogLevel _loglevel, const WvLog *par)
: app(_app)
{
parent = par;
loglevel = _loglevel;
num_logs++;
}
WvLog::WvLog(const WvLog &l)
{
parent = l.parent ? l.parent : &l;
app = parent->app;
loglevel = parent->loglevel;
num_logs++;
}
WvLog::~WvLog()
{
num_logs--;
if (!num_logs && default_receiver)
{
num_receivers++; // deleting default does not really reduce
delete default_receiver;
default_receiver = NULL;
}
}
bool WvLog::isok() const
{
return true;
}
bool WvLog::pre_select(SelectInfo &si)
{
// a wvlog is always writable...
if (si.wants.writable)
return true;
else
return WvStream::pre_select(si);
}
size_t WvLog::uwrite(const void *_buf, size_t len)
{
if (!num_receivers)
{
if (!default_receiver)
{
// nobody's listening -- create a receiver on the console
default_receiver = new WvLogConsole(dup(2));
num_receivers--; // default does not qualify!
}
default_receiver->log(parent ? parent : this, loglevel,
(const char *)_buf, len);
return len;
}
else if (default_receiver)
{
// no longer empty list -- delete our default to stderr
num_receivers++; // deleting default does not really reduce
delete default_receiver;
default_receiver = NULL;
}
WvLogRcvBaseList::Iter i(receivers);
for (i.rewind(); i.next(); )
{
WvLogRcvBase &rc = *i;
rc.log(parent ? parent : this, loglevel, (const char *)_buf, len);
}
return len;
}
///////////////////////////////////// WvLogRcvBase
WvLogRcvBase::WvLogRcvBase()
{
static_init();
WvLogRcvBase::force_new_line = false;
WvLog::receivers.append(this, false);
WvLog::num_receivers++;
}
WvLogRcvBase::~WvLogRcvBase()
{
WvLog::receivers.unlink(this);
WvLog::num_receivers--;
}
const char *WvLogRcvBase::appname(const WvLog *log) const
{
if (log)
return log->app;
else
return WvString("unknown");
}
void WvLogRcvBase::static_init()
{
static bool init = false;
if (!init)
{
#ifndef _WIN32
add_wvfork_callback(WvLogRcvBase::cleanup_on_fork);
#endif
init = true;
}
}
void WvLogRcvBase::cleanup_on_fork(pid_t p)
{
if (p) return; // parent: do nothing
WvLog::receivers.zap();
delete WvLog::default_receiver;
WvLog::default_receiver = NULL;
WvLog::num_receivers = 0;
}
//////////////////////////////////// WvLogRcv
WvLogRcv::WvLogRcv(WvLog::LogLevel _max_level) : custom_levels(5)
{
last_source = NULL;
last_level = WvLog::NUM_LOGLEVELS;
max_level = _max_level;
at_newline = true;
}
WvLogRcv::~WvLogRcv()
{
}
void WvLogRcv::_make_prefix()
{
prefix = WvString("%s<%s>: ",
appname(last_source), loglevels[last_level]);
prelen = prefix.len();
}
void WvLogRcv::_begin_line()
{
mid_line(prefix, prelen);
}
void WvLogRcv::_end_line()
{
// do nothing
}
// like isprint(), but always treats chars >128 as printable, because they
// always are (even if they're meaningless)
static bool my_isprint(char _c)
{
unsigned char c = _c;
if (isprint(c) || c >= 128)
return true;
else
return false;
}
void WvLogRcv::log(const WvLog *source, int _loglevel,
const char *_buf, size_t len)
{
WvLog::LogLevel loglevel = (WvLog::LogLevel)_loglevel;
char hex[5];
WvLog::LogLevel threshold = max_level;
WvString srcname = source->app;
strlwr(srcname.edit());
Src_LvlDict::Iter i(custom_levels);
i.rewind();
// Check if the debug level for the source has been overridden
while (i.next())
{
if (strstr(srcname, i->src))
{
threshold = i->lvl;
break;
}
}
if (loglevel > threshold)
return;
// only need to start a new line with new headers if they headers have
// changed. if the source and level are the same as before, just continue
// the previous log entry.
if (source != last_source || loglevel != last_level || WvLogRcvBase::force_new_line)
{
end_line();
last_source = source;
last_level = loglevel;
_make_prefix();
}
const char *buf = (const char *)_buf, *bufend = buf + len, *cptr;
// loop through the buffer, printing each character or its [hex] equivalent
// if it is unprintable. Also eat newlines unless they are appropriate.
while (buf < bufend)
{
if (buf[0] == '\n' || buf[0] == '\r')
{
end_line();
buf++;
continue;
}
begin_line();
if (buf[0] == '\t')
{
mid_line(" ", 1);
buf++;
continue;
}
else if (!my_isprint(buf[0]))
{
snprintf(hex, 5, "[%02x]", buf[0]);
mid_line(hex, 4);
buf++;
continue;
}
// like strchr, but size-limited instead of null-terminated
for (cptr = buf; cptr < bufend; cptr++)
{
if (*cptr == '\n' || !my_isprint(*cptr))
break;
}
if (cptr >= bufend) // end of buffer
{
mid_line(buf, bufend - buf);
buf = bufend;
}
else if (*cptr == '\n') // end of line
{
mid_line((const char *)buf, cptr - buf);
buf = cptr;
}
else // therefore (!my_isprint(*cptr))
{
mid_line(buf, cptr - buf);
buf = cptr;
}
}
}
// input format: name=number, name=number, name=number, etc.
// 'name' is the name of a log service
// 'number' is the number of the log level to use.
bool WvLogRcv::set_custom_levels(WvString descr)
{
custom_levels.zap();
// Parse the filter line into individual rules
WvStringList lst;
WvStringList::Iter i(lst);
lst.split(descr, ",= ");
if (!lst.count())
return true;
WvString src("");
for (i.rewind(); i.next(); )
{
if (src != "")
{
if (atoi(*i) > 0 && atoi(*i) <= WvLog::NUM_LOGLEVELS)
{
custom_levels.add(new Src_Lvl(src, atoi(*i)), true);
src = "";
}
else
return false;
}
else
{
src = *i;
strlwr(trim_string(src.edit()));
}
}
if (src != "")
return false;
return true;
}
///////////////////////////////////// WvLogConsole
WvLogConsole::WvLogConsole(int _fd, WvLog::LogLevel _max_level) :
WvFDStream(_fd), WvLogRcv(_max_level)
{
}
WvLogConsole::~WvLogConsole()
{
end_line();
}
void WvLogConsole::_mid_line(const char *str, size_t len)
{
uwrite(str, len);
}
|