C ++事件库 [英] C++ event library

查看:85
本文介绍了C ++事件库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否推荐具有以下功能的轻量级跨平台事件记录/日志库:

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

  • 简单的界面
  • 增量事件记录(即event ++)
  • 快速更新
  • 输出的可自定义报告(例如iostream)
  • 时间戳或操作系统集成不重要

原则上,使用带字符串/整数键值的map来使自己变得不难,但我宁愿使用已经编写的map.我已经看过log4cxx,但这似乎有点过分.

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.

谢谢

推荐答案

这是原型,最终版本是:

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

这篇关于C ++事件库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆