log/syslog.cpp

/* This example shows how to extend toast::log with an output policy that
   sends messages to syslog.  Something similar would probably end up being
   useful.  However something like this doesn't belong in toast
   because it would add a dependency other than boost or the standard C++
   library.
*/

#include <syslog.h>
#include <toast/log.hpp>

using namespace toast::log;

struct to_syslog {
  typedef char char_type;

  static void message(char_type const *message, int severity)
  {
    switch(severity) {
    case DEFAULT:  severity = LOG_ERR;     break;
    case FATAL:    severity = LOG_EMERG;   break;
    case ALERT:    severity = LOG_ALERT;   break;
    case CRITICAL: severity = LOG_CRIT;    break;
    case ERROR:    severity = LOG_ERR;     break;
    case WARNING:  severity = LOG_WARNING; break;
    case NOTICE:   severity = LOG_NOTICE;  break;
    case INFO:     severity = LOG_INFO;    break;
    case DEBUG:    severity = LOG_DEBUG;   break;
    default:       severity = LOG_ERR;     break;
    }

    syslog(severity, "%s", message);
  }
};

typedef logger<always_output, to_syslog> Log;

void system_log_example()
{
  openlog("toast::log system logging example", LOG_PID, LOG_USER);
  Log s;
  
  s.message("Let's annoy the sysadmins with toast::log!", INFO);
  s.message("Let's annoy the sysadmins with toast::log!", INFO);
}

SourceForge.net Logo