views:

163

answers:

4
+3  Q: 

C++ event library

can you recommend lightweight cross-platform event recording/log library with the following features:

  • simple interface
  • Incremental event recording (i.e. event++)
  • fast update
  • customizable report out put (for example iostream)
  • timestamps or os integration is not important

in principle it is not hard to make yourself one using map with string/integer keyvalue, but I would rather use one already written. I have looked at log4cxx but that seems like an overkill.

Thanks

A: 

Good old syslog (or syslog-ng) seems like a good place to start...

Uncle Mikey
A: 
  • http://pantheios.sourceforge.net/: Pantheios is an Open Source C/C++ Diagnostic Logging API library, offering an optimal combination of 100% type-safety, efficiency, genericity and extensibility. It is simple to use and extend, highly-portable (platform and compiler-independent) and, best of all, it upholds the C tradition of you only pay for what you use.

  • http://www.arg0.net/rlog: RLog is a flexible message logging facility for C++ programs and libraries. It is highly optimized for the case where no log messages are output, so that it can be left in production code and enabled on-demand

Ricardo
A: 

this is prototype, final version is: http://code.google.com/p/asadchev/source/browse/trunk/projects/boost/utility/profiler.hpp

#define UTILITY_EVENT_HPP

#include "utility/timer.hpp"

#include <string>
#include <map>
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>

#define PROFILE_FUNCTION(...)                                   \
    utility::profiler::event                                    \
    event__(utility::profiler::global[                          \
        utility::detail::profiler_event(__PRETTY_FUNCTION__)(__VA_ARGS__)])



namespace utility {

    namespace detail {
        struct profiler_event {
            profiler_event(const std::string &key) : data_(key) {}
            operator const std::string&() const { return data_; }
            profiler_event& operator()(const std::string &key) {
                data_ += (":" + key);
                return *this;
            }
            profiler_event& operator()() { return *this; }
        private:
            std::string data_;
        };
    }


    struct profiler {

        typedef std::string event_key;

        struct event_data {
            event_data(): size_(0), value_(0) {}
            event_data(const event_data &e)
                : size_(e.size_), value_(e.value_) {}
            event_data& operator+=(double t) {
                boost::lock_guard<boost::mutex> lock(mutex_);
                 ++size_;
                 value_ += t;
                return *this;
            }
            event_data& operator++() { return (*this += 1); }
            std::ostream& to_stream(std::ostream &ostream) const {
                boost::lock_guard<boost::mutex> lock(mutex_);
                ostream << value_ << "/" << size_;
                return ostream;
            }
        private:
            typedef boost::tuple<profiler&, const event_key&> constructor;
            size_t size_;
            double value_;
            mutable boost::mutex mutex_;
        };

        struct event {
            event(event_data &data) : data_(data) {}
            ~event() {
                // std::cout << timer_ << std::endl;
                data_ += double(timer_);
            }
            event_data &data_;
            utility::timer timer_;
        };

        event_data& operator[](const event_key &key) {
            boost::lock_guard<boost::mutex> lock(mutex_);
            return events_[key];
        }
        std::ostream& to_stream(std::ostream &ostream) const {
            boost::lock_guard<boost::mutex> lock(mutex_);
            std::map<event_key, event_data>::const_iterator it = events_.begin();
            while (it != events_.end()) {
                ostream << it->first << ": ";
                it->second.to_stream(ostream);
                ostream << std::endl;
                ++it;
            }
            return ostream;
        }
        static profiler global;
    private:
        std::map<event_key, event_data> events_;
        mutable boost::mutex mutex_;
    };

    inline std::ostream& operator<<(std::ostream &ostream, const profiler &p) {
        return p.to_stream(ostream);
    }

}


#endif // UTILITY_EVENT_HPP
aaa
Is this a case of "the code *is* the documentation"?
André Caron
@Andr it is a rough prototype. final version is on google
aaa
@"aaa carp": It was a joke, but you don't have to laugh.
André Caron
A: 

What you want is boost logging library, its simple, fast, configurable.

I am not sure about event++ option but will not be very hard to implement. It has all that you need and much more, check it out http://torjo.com/log2/doc/html/main_intro.html#main_motivation

harry