Skip to content

yurial/libsyslogpp

Repository files navigation

NAME
    libsyslogpp - C++ wrapper for standard syslog interface

SYNOPSIS
    #include <syslogpp.h>
    Link with -lsyslogpp or with -lsyslogpp_r -pthread if you build library with multi-threading support.
    libsyslogpp_r is a thread-safe library which provides a 'syslog' object for each thread. These messages with guarantee won't be mixed in proceeding sequence.

    For including standard syslog mechanism define HAVE_SYSLOG_H keyword (else you should provide yours syslog functions similar to openlog, closelog, syslog, setlogmask ).
    SYSLOGPP_DEFAULT_PRIO define a default messages priority, if HAVE_SYSLOG_H defined set LOG_ALERT else 0.

    class syslog_t:
        protected syslogbuf,
        public std::ostream
    {
    public:
    inline                  syslog_t(int prio = SYSLOGPP_DEFAULT_PRIO);
    inline                  ~syslog_t();

    //operator of priority modification
    inline  std::ostream&   operator () (int prio);

    //standard interface
    inline  void            open(const char* procname, int option, int facility);
    inline  void            close();
    inline  int             setmask(int mask);
    inline  void            operator () (int prio, const char* fmt, ...) const;
    };

STANDARD INTERFACE
    Standard interface contains overriding of the standard syslog definition and preserves the compatibility with standard interface:

    openlog( "name", LOG_PID, LOG_DAEMON );
    setlogmask( LOG_UPTO( LOG_DEBUG ) );
    syslog( LOG_DEBUG, "%s", "test message" );
    closelog();

STREAM INTERFACE
    syslog class derived from std::ostream and provides a standard stream interface:
    syslog.open( "name", LOG_PID, LOG_DAEMON );
    syslog.setmask( LOG_UPTO( LOG_DEBUG ) ); 
    syslog << "test message" << std::endl;
    syslog.close();

    Data which has been redirected to syslog remains in buffer till synchronization. Synchronization is called automatically by any standard handler for which it's typical(std::endl, std::flush) or by direct call of method flush();

    You should call operator () for change of a message's priority:
    syslog( LOG_DEBUG );

    All messages will be send to syslog with current priority until the last one won't be changed.

    You can mix changing of messages priority and data redirection: 
    syslog( LOG_DEBUG ) << "test message" << std::endl;

    The operator of priority modification transfers data from internal buffer to syslog with the previous priority before setting a new priority value.
    syslog( LOG_DEBUG ) << "message1";
    syslog( LOG_NOTICE ) << "message2" << std::endl; //send "message1" with priority LOG_DEBUG and "message2" with priority LOG_NOTICE.

    You can substitute standard behavior of the predetermined stream output object like for std::cout, std::cerr and std::clog:
    #include <stdlib.h>
    #include <iostream>
    #include <syslogpp.h>

    int main(int argc, char* argv[])
    {
    syslog.open( NULL, LOG_PID, LOG_USER );
    syslog( LOG_ERR );
    std::clog.rdbuf( syslog.rdbuf() );
    std::clog << "test message" << std::endl;
    syslog.close();
    return EXIT_SUCCESS;
    }

    The substitution of std::cerr requires the inclusion of buffering. Use it with std::nounitbuf manipulator else std::cerr will send each message separately.

AUTHORS
    Document written by:
    Yuri Dyatchenko [email protected]
    Roman Dolgov [email protected]