Boost logo

Boost :

From: John Torjo (john.groups_at_[hidden])
Date: 2008-02-07 17:04:15


> From what I can see so far, using this library is not going to be a
> compelling alternative to the concise
>
> #include <syslog.h>
> syslog(LOG_WARN,"Blob %d failed to %s",n,x);
>
>
>
Here you go. Note - in order to work, you need to replace
boost/logging/format/destination/convert_destination.hpp - sorry , my bad.
test_now.cpp show you how you can easily write to the syslog.

Best,
John

-- 
http://John.Torjo.com -- C++ expert
http://blog.torjo.com
... call me only if you want things done right

// convert_destination.hpp

// Boost Logging library
//
// Author: John Torjo, www.torjo.com
//
// Copyright (C) 2007 John Torjo (see www.torjo.com for email)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org for updates, documentation, and revision history.
// See http://www.torjo.com/log2/ for more details

#ifndef JT28092007_convert_destination_HPP_DEFINED
#define JT28092007_convert_destination_HPP_DEFINED

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

#include <boost/logging/detail/fwd.hpp>

namespace boost { namespace logging { namespace destination {

template<class t> struct into {};

namespace tag {
    template<
            class string_ ,
            class param1 ,
            class param2 ,
            class param3 ,
            class param4 ,
            class param5 ,
            class param6 ,
            class param7 ,
            class param8 ,
            class param9 ,
            class param10> struct holder ;
}

/**
@brief Allows writing messages to destinations

It has 2 function overloads:
- write(message, output) - writes the given message, to the given output
- do_convert(message, into<other_type>() );

FIXME
*/
namespace convert {
    template<class obj, class char_type, class char_traits> void write(const obj & m, std::basic_ostream<char_type, char_traits> & out) {
        out << m;
    }

    template<class string_, class p1, class p2, class p3, class p4, class p5, class p6, class p7, class p8, class p9, class p10, class char_type, class char_traits> void write(const ::boost::logging::tag::holder<string_,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10> & src, std::basic_ostream<char_type, char_traits> & out ) {
        typedef typename ::boost::logging::tag::holder<string_,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10>::string_type string_type;
        out << (const string_type&)src;
    }

    template<class char_traits, class char_type> void write(const char_type* m, std::basic_ostream<char_type, char_traits> & out) {
        out << m;
    }

    inline const char_type * do_convert(const char_type * c, const into<const char_type*> &) { return c; }
    inline const char_type * do_convert(const std::basic_string<char_type> & s, const into<const char_type* > &) { return s.c_str(); }

    inline const std::basic_string<char_type> & do_convert(const std::basic_string<char_type> & s, const into< std::basic_string<char_type> > &) {
        return s;
    }
}

struct do_convert_destination {
    template<class msg, class dest> static void write(const msg & m, dest & d) {
        convert::write(m, d);
    }

    template<class msg, class dest> static dest do_convert(const msg & m, const into<dest> &) {
        return convert::do_convert(m, into<dest>() );
    }

};

}}}

#endif


// destination/syslog.hpp

// Boost Logging library
//
// Author: John Torjo, www.torjo.com
//
// Copyright (C) 2007 John Torjo (see www.torjo.com for email)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org for updates, documentation, and revision history.
// See http://www.torjo.com/log2/ for more details

#ifndef JT28092007_destination_syslog_HPP_DEFINED
#define JT28092007_destination_syslog_HPP_DEFINED

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

#include <boost/logging/detail/fwd.hpp>
#include <boost/logging/detail/manipulator.hpp>
#include <boost/logging/format/destination/convert_destination.hpp>
#include <boost/logging/format/destination/file.hpp>
#include <boost/logging/detail/level.hpp>
#include <syslog.h>

namespace boost { namespace logging { namespace destination {

/**
    @brief Writes the string to syslog. Note: does not care about levels - always logs as LOG_INFO
*/
template<class convert_dest = do_convert_destination > struct syslog_no_levels_t : is_generic, boost::logging::op_equal::always_equal {

    template<class msg_type> void operator()(const msg_type & msg) const {
        syslog( LOG_INFO, msg.c_str() );
    }
};

/** @brief syslog_no_levels_t with default values. See syslog_no_levels_t

@copydoc syslog_no_levels_t
*/
typedef syslog_no_levels_t<> syslog_no_levels;

/** @brief Writes the string to syslog. It cares about levels

See @ref boost::logging::tag "how to use tags".
*/
template<class convert = do_convert_format::prepend> struct syslog_t : is_generic, uses_tag< level_t<convert>, ::boost::logging::tag::level >, boost::logging::op_equal::always_equal {
    typedef convert convert_type;
    template<class msg_type, class tag_type> void write_tag(msg_type & str, const tag_type & tag) const {
        syslog( level_to_syslog_level(tag.val) , as_string(str).c_str() );
    }

private:
    const hold_string_type& as_string(const hold_string_type & str) { return str; }

    int level_to_syslog_level( level::type level) {
        if ( level <= level::debug)
            return LOG_DEBUG;
        if ( level <= level::info)
            return LOG_INFO;
        if ( level <= level::warning)
            return LOG_WARN;
        if ( level <= level::error)
            return LOG_ERR;
        if ( level <= level::fatal)
            return LOG_CRIT;

        return LOG_EMERG;
    }
};

/** @brief syslog_t with default values. See syslog_t

@copydoc syslog_t
*/
typedef syslog_t<> syslog;

}}}

#endif


#include <boost/logging/format.hpp>
#include <boost/logging/format/formatter/tags.hpp>
#include <boost/logging/format/formatter/syslog.hpp>

namespace bl = boost::logging;
typedef bl::tag::holder<bl::default_, bl::tag::level> tag_holder;
BOOST_LOG_FORMAT_MSG(tag_holder)
BOOST_LOG_DESTINATION_MSG(tag_holder)

typedef bl::logger_format_write< > logger_type;

#define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_filter(), lvl ) .set_tag( BOOST_LOG_TAG_LEVEL(lvl) )

BOOST_DEFINE_LOG_FILTER(g_log_filter, bl::level::holder )
BOOST_DEFINE_LOG(g_l, logger_type)

int main() {
    g_l()->writer().add_formatter( bl::formatter::idx(), "[%] " );
    g_l()->writer().add_formatter( bl::formatter::append_newline() );
    // write to cout and syslog
    g_l()->writer().add_destination( bl::destination::cout() );
    g_l()->writer().add_destination( bl::destination::syslog() );
    g_l()->mark_as_initialized();

    int i = 1;
    L_(debug) << "this is so cool " << i++;
    L_(error) << "first error " << i++;
}


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