2010-08-29 43 views
3

你可以推薦輕量級跨平臺的事件記錄/日誌庫,具有以下特點:C++事件庫

  • 簡單的界面
  • 增量事件記錄(即事件++)
  • 快速更新
  • 定製報表輸出(例如iostream)
  • 時間戳或操作系統集成不重要

原則上,使用帶有字符串/整數鍵值的映射使自己變得不難,但我寧願使用已經寫好的映射。我已經看過log4cxx,但這似乎是一個矯枉過正的問題。

感謝

+1

「時間戳或OS集成並不重要」?然後我有0%的開銷,100%的無膨脹選項給你:'#include '; '#include '; 'std :: cerr <<「這是一條日誌消息!!!」 << std :: endl;' – Dummy00001 2010-08-29 15:33:12

回答

0

這是原型啓用,最後的版本是: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 
+0

這是「代碼*是*文檔」的情況嗎? – 2010-09-24 21:18:39

+0

@這是一個粗糙的原型。最終版本是谷歌 – Anycorn 2010-09-24 21:27:03

+0

@「aaa鯉魚」:這是一個笑話,但你不必笑。 – 2010-09-24 21:37:51

0

好舊系統日誌(或syslog-ng的)似乎是一個很好的開始...

0
  • http://pantheios.sourceforge.net/:Pantheios是一個開源的C/C++診斷日誌API庫,提供100%類型安全性,效率,通用性和可擴展性的最佳組合。它的使用和擴展非常簡單,具有高度可移植性(平臺和編譯器無關),並且最重要的是,它支持C的傳統,只支付您使用的費用。

  • http://www.arg0.net/rlog:RLog是一個靈活的C++程序和庫的消息日誌工具。強烈的情況下,優化在沒有日誌消息輸出,以便它可以在生產代碼中留和點播