【问题标题】:C++ event library [closed]C ++事件库[关闭]
【发布时间】:2010-08-29 03:10:03
【问题描述】:

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

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

原则上,使用带有字符串/整数键值的映射并不难,但我宁愿使用已经编写好的映射。我看过 log4cxx 但这似乎有点过头了。

谢谢

【问题讨论】:

  • "时间戳或操作系统集成不重要"??然后我为您提供 0% 开销、100% 无膨胀选项:#include <iostream>; #include <iomanip>; std::cerr << "This is a log message!!!" << std::endl;

标签: c++ logging


【解决方案1】:

这是原型,最终版本是: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

【讨论】:

    【解决方案2】:

    良好的旧系统日志(或 syslog-ng)似乎是一个不错的起点......

    【讨论】:

      【解决方案3】:
      • http://pantheios.sourceforge.net/:Pantheios 是一个开源 C/C++ 诊断日志 API 库,提供 100% 类型安全、效率、通用性和可扩展性的最佳组合。它易于使用和扩展,高度可移植(独立于平台和编译器),而且最重要的是,它坚持 C 语言的传统,即您只需为使用的内容付费。

      • http://www.arg0.net/rlog:RLog 是用于 C++ 程序和库的灵活的消息记录工具。它针对不输出日志消息的情况进行了高度优化,因此可以留在生产代码中并按需启用

      【讨论】:

        【解决方案4】:

        您想要的是 boost 日志库,它简单、快速、可配置。

        我不确定 event++ 选项,但不会很难实现。它有你需要的一切,还有更多,请查看http://torjo.com/log2/doc/html/main_intro.html#main_motivation

        【讨论】:

          猜你喜欢
          • 2014-06-17
          • 1970-01-01
          • 1970-01-01
          • 2021-06-13
          • 2012-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多