#pragma once #include #include //streamsize #include //basic_streambuf #include //char_traits, basic_string #include //char_traits, basic_string #include #include #include #include #define STDX_DSTREAM_BUFFERING using namespace std; namespace Jenga{ namespace Common{ class LoggerSetting : public BoostSerializationSupport { public: int stopStep; LoggerSetting() : stopStep( -1 ) { } // XMLシリアライズ用 private: virtual const char *RootTagName() const { return "loggerSetting"; } friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP( stopStep ); } }; // VC++ で STLport だと using std::char_traits; みたいなのが必要かも template > class basic_dbg_streambuf: public std::basic_stringbuf { protected: std::string saveFilePath; int count; LoggerSetting setting; public: basic_dbg_streambuf( const std::string &saveFilePath, bool isOptionEnabled ) : saveFilePath( saveFilePath ) , count( 0 ) { #ifndef STDX_DSTREAM_BUFFERING setbuf(0,0); #endif if( isOptionEnabled ) { // 設定ファイルを読み込む char temporary[MAX_PATH]; char temp2[MAX_PATH]; char temp3[MAX_PATH]; _splitpath(saveFilePath.c_str(),temporary,temp2,temp3,NULL); std::string settingXmlPath = (std::string)temporary + temp2 + temp3 + ".setting.xml"; if( Jenga::Common::Path( settingXmlPath ).IsExistFile() ) { setting.ReadXml( settingXmlPath, false ); } } } virtual ~basic_dbg_streambuf() { sync(); } protected: int sync(void) { dbg_out(str().c_str()); pbump(static_cast(pbase() - pptr())); return 0; } void dbg_out(const Ch_T*); }; template <> inline void basic_dbg_streambuf::dbg_out(const char *str) { ofstream ofs( ( saveFilePath ).c_str(), ios_base::app ); ofs << "[" << (count++) << "] " << str ; ofs.close(); OutputDebugString( str ); if( (count-1) == setting.stopStep ) { DebugBreak(); } } template > class basic_dbg_ostream: public std::basic_ostream { public: basic_dbg_ostream( const string &saveFilePath, bool isOptionEnabled ) : std::basic_ostream(new basic_dbg_streambuf(saveFilePath,isOptionEnabled)) { ofstream ofs( ( saveFilePath ).c_str(), ios_base::trunc ); ofs.close(); } virtual ~basic_dbg_ostream() { // flush(); // 不要らしい.http://www.tietew.jp/cppll/archive/607 delete rdbuf(); } }; typedef basic_dbg_ostream<_TCHAR> Logger; }} BOOST_CLASS_IMPLEMENTATION(Jenga::Common::LoggerSetting, boost::serialization::object_serializable);