#ifndef __LOGGER_H__ #define __LOGGER_H__ /** * ログを簡単にとるための道具 * */ #include #include #include #include class Loggable{ public: virtual void inspect(std::ostream &out) const = 0; }; class Logger{ protected: typedef std::ostream ostream; typedef std::fstream fstream; typedef std::string string; typedef std::vector storage_t; std::ostream &m_out; storage_t m_targets; bool do_make_stream; public: Logger() : m_out(std::cout), m_targets(), do_make_stream(false) {} Logger(ostream &out) : m_out(out), m_targets(), do_make_stream(false) {} Logger(string fname) : m_out(*(new fstream(fname.c_str(), std::ios::out | std::ios::app))), m_targets(), do_make_stream(true) {} ~Logger(){ if(do_make_stream){ (static_cast(&m_out))->close(); delete(&m_out); } } std::ostream &out() const {return m_out;} void flush() const{ for(storage_t::const_iterator it = m_targets.begin(); it < m_targets.end(); it++){ (*it)->inspect(m_out); } } void add(const Loggable &target){ m_targets.push_back(&target); } void remove(const Loggable &target){ m_targets.erase(std::remove(m_targets.begin(), m_targets.end(), &target), m_targets.end()); } void clear(){m_targets.clear();} }; #endif /* __LOGGER_H__ */