WvStreams
wvsyslog.cc
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * WvSyslog is a descendant of WvLogRcv that sends messages to the syslogd
00006  * daemon.
00007  */
00008 #include "wvsyslog.h"
00009 #include "strutils.h"
00010 #include <time.h>
00011 #include <syslog.h>
00012 
00013 WvSyslog::WvSyslog(WvStringParm _prefix, bool _include_appname,
00014                    WvLog::LogLevel _first_debug,
00015                    WvLog::LogLevel _max_level)
00016         : WvLogRcv(_max_level), syslog_prefix(_prefix)
00017 {
00018     first_debug = _first_debug;
00019     include_appname = _include_appname;
00020     openlog(syslog_prefix, 0, LOG_DAEMON);
00021 }
00022 
00023 
00024 WvSyslog::~WvSyslog()
00025 {
00026     end_line();
00027     closelog();
00028 }
00029 
00030 
00031 void WvSyslog::_begin_line()
00032 {
00033     if (include_appname)
00034         current.put(prefix, prelen);
00035 }
00036 
00037 
00038 void WvSyslog::_mid_line(const char *str, size_t len)
00039 {
00040     current.put(str, len);
00041 }
00042 
00043 
00044 void WvSyslog::_end_line()
00045 {
00046     int lev, count;
00047     struct LevMap
00048     {
00049         int wvlog_lvl;
00050         int syslog_lvl;
00051     };
00052 
00053     static LevMap levmap[] = {
00054         {WvLog::Critical,       LOG_CRIT},
00055         {WvLog::Error,          LOG_ERR},
00056         {WvLog::Warning,        LOG_WARNING},
00057         {WvLog::Notice,         LOG_NOTICE},
00058         {WvLog::Info,           LOG_INFO},
00059         {WvLog::Debug,          LOG_DEBUG},
00060         {WvLog::Debug2,         -1},
00061         {-1, -1}
00062     };
00063     
00064     if (current.used())
00065     {
00066         lev = -1;
00067         
00068         for (count = 0; levmap[count].wvlog_lvl >= 0; count++)
00069         {
00070             if (last_level >= levmap[count].wvlog_lvl)
00071                 lev = levmap[count].syslog_lvl;
00072         }
00073         
00074         if (last_level < first_debug && lev == LOG_DEBUG)
00075             lev = LOG_INFO;
00076         
00077         if (lev >= 0)
00078         {
00079             current.put("", 1); // null-terminate
00080             syslog(lev, "%s", current.get(current.used()));
00081         }
00082         else
00083             current.zap(); // not important enough to print this message
00084     }
00085 }
00086