File: wvsyslog.cc

package info (click to toggle)
wvstreams 4.6.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,820 kB
  • ctags: 7,837
  • sloc: cpp: 64,203; ansic: 4,154; sh: 4,094; makefile: 549; perl: 402
file content (86 lines) | stat: -rw-r--r-- 1,642 bytes parent folder | download | duplicates (11)
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
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 * 
 * WvSyslog is a descendant of WvLogRcv that sends messages to the syslogd
 * daemon.
 */
#include "wvsyslog.h"
#include "strutils.h"
#include <time.h>
#include <syslog.h>

WvSyslog::WvSyslog(WvStringParm _prefix, bool _include_appname,
		   WvLog::LogLevel _first_debug,
		   WvLog::LogLevel _max_level)
	: WvLogRcv(_max_level), syslog_prefix(_prefix)
{
    first_debug = _first_debug;
    include_appname = _include_appname;
    openlog(syslog_prefix, 0, LOG_DAEMON);
}


WvSyslog::~WvSyslog()
{
    end_line();
    closelog();
}


void WvSyslog::_begin_line()
{
    if (include_appname)
	current.put(prefix, prelen);
}


void WvSyslog::_mid_line(const char *str, size_t len)
{
    current.put(str, len);
}


void WvSyslog::_end_line()
{
    int lev, count;
    struct LevMap
    {
	int wvlog_lvl;
	int syslog_lvl;
    };

    static LevMap levmap[] = {
	{WvLog::Critical,	LOG_CRIT},
	{WvLog::Error,		LOG_ERR},
	{WvLog::Warning,	LOG_WARNING},
	{WvLog::Notice,		LOG_NOTICE},
	{WvLog::Info,		LOG_INFO},
	{WvLog::Debug,		LOG_DEBUG},
	{WvLog::Debug2,		-1},
	{-1, -1}
    };
    
    if (current.used())
    {
	lev = -1;
	
	for (count = 0; levmap[count].wvlog_lvl >= 0; count++)
	{
	    if (last_level >= levmap[count].wvlog_lvl)
		lev = levmap[count].syslog_lvl;
	}
	
	if (last_level < first_debug && lev == LOG_DEBUG)
	    lev = LOG_INFO;
	
	if (lev >= 0)
	{
	    current.put("", 1); // null-terminate
	    syslog(lev, "%s", current.get(current.used()));
	}
	else
	    current.zap(); // not important enough to print this message
    }
}