WvStreams
wvsyslog.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * WvSyslog is a descendant of WvLogRcv that sends messages to the syslogd
6  * daemon.
7  */
8 #include "wvsyslog.h"
9 #include "strutils.h"
10 #include <time.h>
11 #include <syslog.h>
12 
13 WvSyslog::WvSyslog(WvStringParm _prefix, bool _include_appname,
14  WvLog::LogLevel _first_debug,
15  WvLog::LogLevel _max_level)
16  : WvLogRcv(_max_level), syslog_prefix(_prefix)
17 {
18  first_debug = _first_debug;
19  include_appname = _include_appname;
20  openlog(syslog_prefix, 0, LOG_DAEMON);
21 }
22 
23 
24 WvSyslog::~WvSyslog()
25 {
26  end_line();
27  closelog();
28 }
29 
30 
32 {
33  if (include_appname)
34  current.put(prefix, prelen);
35 }
36 
37 
38 void WvSyslog::_mid_line(const char *str, size_t len)
39 {
40  current.put(str, len);
41 }
42 
43 
45 {
46  int lev, count;
47  struct LevMap
48  {
49  int wvlog_lvl;
50  int syslog_lvl;
51  };
52 
53  static LevMap levmap[] = {
54  {WvLog::Critical, LOG_CRIT},
55  {WvLog::Error, LOG_ERR},
56  {WvLog::Warning, LOG_WARNING},
57  {WvLog::Notice, LOG_NOTICE},
58  {WvLog::Info, LOG_INFO},
59  {WvLog::Debug, LOG_DEBUG},
60  {WvLog::Debug2, -1},
61  {-1, -1}
62  };
63 
64  if (current.used())
65  {
66  lev = -1;
67 
68  for (count = 0; levmap[count].wvlog_lvl >= 0; count++)
69  {
70  if (last_level >= levmap[count].wvlog_lvl)
71  lev = levmap[count].syslog_lvl;
72  }
73 
74  if (last_level < first_debug && lev == LOG_DEBUG)
75  lev = LOG_INFO;
76 
77  if (lev >= 0)
78  {
79  current.put("", 1); // null-terminate
80  syslog(lev, "%s", current.get(current.used()));
81  }
82  else
83  current.zap(); // not important enough to print this message
84  }
85 }
86 
WvBufBaseCommonImpl::get
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
Definition: wvbufbase.h:114
WvSyslog::_mid_line
virtual void _mid_line(const char *str, size_t len)
add text to the current log line.
Definition: wvsyslog.cc:38
WvBufBaseCommonImpl::zap
void zap()
Clears the buffer.
Definition: wvbufbase.h:257
WvSyslog::_begin_line
virtual void _begin_line()
Start a new log line (print prefix)
Definition: wvsyslog.cc:31
WvLogRcv
WvLogRcv adds some intelligence to WvLogRcvBase, to keep track of line-prefix-printing and other form...
Definition: wvlogrcv.h:28
WvBufBaseCommonImpl::used
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Definition: wvbufbase.h:92
WvSyslog::_end_line
virtual void _end_line()
End this (Guaranteed NonEmpty) log line.
Definition: wvsyslog.cc:44