利用Asio搭建日誌系統

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   sp   for   

  Asio(http://think-async.com)官方樣本中給出了一個比較初步的Log Service,主要代碼在basic_logger.hpp、logger_service.hpp、logger_service.cpp這3個檔案。稍作改進(比如建立單獨目錄存放Log檔案、格式化Log檔案名稱以及每一行日誌、定時建立新的記錄檔)就可以搭建起一個可用的日誌系統。

  新增一個logger類繼承自basic_logger<logger_service>,在logger中建立定時器和定時事件,每隔規定時間發出請求重建一個記錄檔。

  對basic_logger類中的use_file方法稍作修改,在此方法中建立存放Log檔案的目錄,並響應產生新Log檔案的請求,Log檔案可以利用產生時間命名以便進行區分。

  //basic_logger.hpp  void use_file(const std::string& file)  {
// create new dir for log files // create new log file‘s name depends on time
... ... service_.use_file(impl_, file); }
void log(const std::string& message) { service_.log(impl_, message); }

  從basic_logger.hpp的代碼可以看出,真正的IO操作,無論是Log檔案的建立還是Log記錄的寫入,都是在logger_service中進行的。寫入Log時,不妨加上時間首碼,以便今後的日誌分析。

  //logger_service.hpp  void use_file(impl_type& /*impl*/, const std::string& file)  {       work_io_service_.post(boost::bind(          &logger_service::use_file_impl, this, file));  }  void log(impl_type& impl, const std::string& message)  {     // add time prefix     std::ostringstream os;     os << ... ... << ": " << message;     work_io_service_.post(boost::bind(          &logger_service::log_impl, this, os.str()));   }  void use_file_impl(const std::string& file)  {     ofstream_.close();     ofstream_.clear();     ofstream_.open(file.c_str());   }   void log_impl(const std::string& text)   {     ofstream_ << text << std::endl;   }

  在logger_service中,use_file和log都調用post方法把回調放入內部io對象的事件隊列。use_file_impl負責建立新的Log檔案,log_impl負責寫入Log檔案,所有的回呼函數在單線程內被調用,這樣即便它們共用ofstream_,也不需要加鎖保護。

  在虛擬機器測試一下效能,CentOS 6.5、單核、1G記憶體,寫入100萬條日誌需15秒。

利用Asio搭建日誌系統

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.