00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #include <cstring>
00019 #include <syslog.h>
00020 
00021 #include <libplumbing/logging/syslog.h>
00022 #include <libplumbing/progname.h>
00023 
00024 
00025 plumbing::logging_syslog::~logging_syslog()
00026 {
00027     if (is_open)
00028     {
00029         closelog();
00030         is_open = false;
00031     }
00032 }
00033 
00034 
00035 plumbing::logging_syslog::logging_syslog() :
00036     logging(),
00037     is_open(false)
00038 {
00039 }
00040 
00041 
00042 static int
00043 determine_level_from_text(const char *txt)
00044 {
00045     struct table_t
00046     {
00047         const char *name;
00048         int level;
00049     };
00050 
00051     static const table_t table[] =
00052     {
00053         { "emerg", LOG_EMERG },
00054         { "emergency", LOG_EMERG },
00055         { "alert", LOG_ALERT },
00056         { "crit", LOG_CRIT },
00057         { "critical", LOG_CRIT },
00058         { "err", LOG_ERR },
00059         { "error", LOG_ERR },
00060         { "warn", LOG_WARNING },
00061         { "warning", LOG_WARNING },
00062         { "notice", LOG_NOTICE },
00063         { "info", LOG_INFO },
00064         { "information", LOG_INFO },
00065         { "debug", LOG_DEBUG },
00066         { 0, 0 }
00067     };
00068 
00069     for (const table_t *tp = table; tp->name; ++tp)
00070     {
00071         size_t len = strlen(tp->name);
00072         if (strncasecmp(tp->name, txt, len) == 0 && txt[len] == ':')
00073             return tp->level;
00074     }
00075     return LOG_ERR;
00076 }
00077 
00078 
00079 void
00080 plumbing::logging_syslog::deliver_message_text(const char *txt)
00081 {
00082     int facility = LOG_USER;
00083     if (!is_open)
00084     {
00085         int option = LOG_PID;
00086         openlog(progname_get().c_str(), option, facility);
00087     }
00088     int level = determine_level_from_text(txt);
00089     int priority = facility | level;
00090     syslog(priority, "%s\n", txt);
00091 }
00092 
00093 
00094