phreak Posted September 5, 2011 Report Posted September 5, 2011 Exemplu de folosire : xLogger logger; logger.addFile("first.txt",std::ios::app); logger.addFile("second.txt",std::ios::out); logger<<"first post"; logger<<"abcdef"; logger<<"123"; logger<<"456";Output ( in cout si amandoua fisierele ) :##################################################[2011-Sep-05 15:17:40] first post[2011-Sep-05 15:17:40] abcdef[2011-Sep-05 15:17:40] 123[2011-Sep-05 15:17:40] 456Sursa : http://codepad.org/eqzorpcV#ifndef XLOGGER_H#define XLOGGER_H#include <boost/shared_ptr.hpp>#include <boost/enable_shared_from_this.hpp>#include <boost/date_time.hpp>#include <fstream>#include <iostream>class TempLogger : public boost::enable_shared_from_this<TempLogger>{public: TempLogger(std::vector<std::ofstream *> & streams): m_Streams(streams) {} template<typename T> boost::shared_ptr<TempLogger> operator<<(const T & value) { for(int i = 0 ; i < m_Streams.size(); i++) *m_Streams.at(i)<<value; std::cout<<value; return shared_from_this(); } ~TempLogger() { for(int i = 0 ; i < m_Streams.size(); i++) *m_Streams.at(i)<<std::endl; std::cout<<std::endl; }private: std::vector<std::ofstream *> & m_Streams;};template <typename T>boost::shared_ptr<TempLogger> operator<<(boost::shared_ptr<TempLogger> logger, T const & t){ return (*logger) << t;}struct xLogger { xLogger() {} void addFile(std::string filename,std::ios_base::openmode openMode) { m_Streams.push_back(new std::ofstream(filename.c_str(),openMode)); *m_Streams.back()<<"##################################################"<<std::endl; } template<typename T> boost::shared_ptr<TempLogger> operator<<( const T & value){ boost::shared_ptr<TempLogger> temp(new TempLogger(m_Streams)); for(int i = 0 ; i < m_Streams.size() ; i++) *m_Streams.at(i)<<"[" + to_simple_string(boost::posix_time::second_clock::local_time()) + "] "<<value; std::cout<<"[" + to_simple_string(boost::posix_time::second_clock::local_time()) + "] "<<value; return temp; } ~xLogger() { for(int i = 0 ; i < m_Streams.size() ; i++) { m_Streams.at(i)->close(); delete m_Streams.at(i); } }private: std::vector<std::ofstream *> m_Streams;};#endif // XLOGGER_H Quote
Ethereal Posted September 9, 2011 Report Posted September 9, 2011 De ce ai nevoie de TempLogger sau de boost? Din ce inteleg eu, xLogger este un wrapper pt. un vector de ostream-uri. Quote
phreak Posted September 9, 2011 Author Report Posted September 9, 2011 Am nevoie de TempLogger pentru a nu fi nevoit sa adaug timestamp si endl la fiecare apelare a loggerului.Am nevoie de boost pentru timestamp si shared_ptr Quote
cifratorul Posted September 11, 2011 Report Posted September 11, 2011 shared_ptr are si STL-ul nu era nevoie de boost Quote
phreak Posted September 11, 2011 Author Report Posted September 11, 2011 C++11 are si inca nu e complet implementat asa ca.. raman la boost deocamdata Quote