C++ C++;事件库
您是否可以推荐具有以下功能的轻量级跨平台事件记录/日志库:C++ C++;事件库,c++,logging,C++,Logging,您是否可以推荐具有以下功能的轻量级跨平台事件记录/日志库: 简单接口 增量事件记录(即事件++) 快速更新 可定制的报告输出(例如iostream) 时间戳或操作系统集成并不重要 原则上,使用带有字符串/整型键值的map制作一个并不困难,但我更愿意使用已经编写好的map。我已经看过log4cxx了,但这似乎有点过头了 谢谢好的旧syslog(或syslog ng)似乎是一个很好的起点… :Pantheios是一个开源C/C++诊断日志API库,提供100%类型安全性、效率、通用性和可扩展性的
- 简单接口
- 增量事件记录(即事件++)
- 快速更新
- 可定制的报告输出(例如iostream)
- 时间戳或操作系统集成并不重要
- :Pantheios是一个开源C/C++诊断日志API库,提供100%类型安全性、效率、通用性和可扩展性的最佳组合。它易于使用和扩展,高度可移植(独立于平台和编译器),最重要的是,它坚持了C语言的传统,即只需为使用的东西付费 < L>是C++程序和库的灵活消息记录工具。它针对不输出日志消息的情况进行了高度优化,因此可以将其保留在生产代码中并按需启用
- 这是原型,最终版本是:
#定义公用设施事件(HPP)
#包括“实用程序/计时器.hpp”
#包括
#包括
#包括
#包括
#定义PROFILE_函数(…)\
实用工具::分析器::事件\
事件(实用工具::探查器::全局[\
实用工具::详细信息::探查器事件(uuu漂亮函数)(uu VA_ARGS_uuu)])
命名空间实用程序{
名称空间详细信息{
结构探查器事件{
探查器_事件(const std::string和key):数据_u(key){}
运算符const std::string&()const{返回数据}
探查器_事件和运算符()(常量std::字符串和键){
数据+=(“:”+键);
归还*这个;
}
探查器_事件和运算符(){return*this;}
私人:
std::字符串数据;
};
}
结构分析器{
typedef std::字符串事件\ u键;
结构事件数据{
事件\数据():大小\ 0,值\ 0{}
事件数据(常量事件数据和e)
:size_u(e.size_u),value_u(e.value_u){}
事件数据和运算符+=(双t){
boost::lock\u guard lock(互斥锁);
++大小;
值u+=t;
归还*这个;
}
事件_数据和运算符++(){return(*this+=1);}
std::ostream&to_流(std::ostream&ostream)常数{
boost::lock\u guard lock(互斥锁);
ostream您需要的是boost日志库,它简单、快速、可配置
我不确定事件++选项,但实现起来并不困难。它有您需要的一切,还有更多,请查看它“时间戳或操作系统集成不重要”?然后我为您提供了0%开销、100%无膨胀的选项:#include
#include
std::cerr
#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