Boost logo

Boost :

Subject: Re: [boost] [log] Review-ready version in the Vault
From: Peter Simons (simons_at_[hidden])
Date: 2009-02-19 05:57:56


Andrey Semashev writes:

> Ok, consider me convinced on this part. I will add portable RFC3164
> support to Boost.Log, as it will help to route log records better.

It turned out implementing a simple rfc3164-based logger is not all that
difficult, really. I guess the biggest effort would be to integrate it
meaningfully into Boost.Log. Anyway, here's the source code, in case any
finds it useful.

Take care,
Peter


#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/assert.hpp>
#include <boost/test/prg_exec_monitor.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/array.hpp>
#include <iostream>

namespace rfc3164
{
  namespace type
  {
    enum facility_type
      { kernel_message = 0
      , user_message = 1
      , mail_system = 2
      , system_daemon = 3
      , authorization = 4
      , internal = 5
      , line_printer = 6
      , network_news = 7
      , uucp = 8
      , clock_daemon = 9
      , security = 10
      , ftp_daemon = 11
      , ntp_daemon = 12
      , log_audit = 13
      , log_alert = 14
      , clock_daemon2 = 15
      , local0 = 16
      , local1 = 17
      , local2 = 18
      , local3 = 19
      , local4 = 20
      , local5 = 21
      , local6 = 22
      , local7 = 23
      };

    enum priority_type
      { emergency = 0
      , alert = 1
      , critical = 2
      , error = 3
      , warning = 4
      , notice = 5
      , info = 6
      , debug = 7
      };
  }

  using namespace type;

  class message
  {
  public:
    typedef boost::asio::const_buffer const_buffer;
    typedef boost::array<const_buffer, 2u> iovec;

    message(facility_type facility, priority_type pri, const_buffer msg)
    {
      int const code( static_cast<int>(facility) * 8 + static_cast<int>(pri) );
      _prefix = '<' + boost::lexical_cast<std::string>(code) + '>';
      _iov[0] = boost::asio::buffer(_prefix);
      _iov[1] = msg;
    }

    iovec const & iov() const { return _iov; }

  private:
    std::string _prefix;
    iovec _iov;
  };

  class server
  {
  public:
    typedef boost::asio::ip::udp inet;

    static inet::endpoint endpoint(std::string const & host, unsigned short port = 514u)
    {
      return inet::endpoint(boost::asio::ip::address::from_string(host), port);
    }

    server(inet::endpoint const & destination = endpoint("127.0.0.1", 514u))
    : _s(_ios, inet::endpoint(destination.protocol(), 0))
    {
      _s.connect(destination);
    }

    void send(facility_type facility, priority_type pri, char const * msg)
    {
      _s.send(message(facility, pri, boost::asio::const_buffer(msg, std::strlen(msg))).iov());
    }

  private:
    boost::asio::io_service _ios;
    boost::asio::ip::udp::socket _s;
  };
}

int cpp_main(int argc, char ** argv)
{
  using namespace rfc3164::type;
  rfc3164::server log;
  log.send(user_message, info, "this is a meaningful test message");
  return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk