1 | #pragma once
|
---|
2 |
|
---|
3 | #define STDX_DSTREAM_BUFFERING
|
---|
4 |
|
---|
5 | namespace Jenga{
|
---|
6 | namespace Common{
|
---|
7 |
|
---|
8 |
|
---|
9 | class LoggerSetting : public BoostSerializationSupport<LoggerSetting>
|
---|
10 | {
|
---|
11 | public:
|
---|
12 | int stopStep;
|
---|
13 |
|
---|
14 | LoggerSetting()
|
---|
15 | : stopStep( -1 )
|
---|
16 | {
|
---|
17 | }
|
---|
18 |
|
---|
19 | // XMLシリアライズ用
|
---|
20 | private:
|
---|
21 | virtual const char *RootTagName() const
|
---|
22 | {
|
---|
23 | return "loggerSetting";
|
---|
24 | }
|
---|
25 | friend class boost::serialization::access;
|
---|
26 | template<class Archive> void serialize(Archive& ar, const unsigned int version)
|
---|
27 | {
|
---|
28 | ar & BOOST_SERIALIZATION_NVP( stopStep );
|
---|
29 | }
|
---|
30 | };
|
---|
31 |
|
---|
32 |
|
---|
33 | // VC++ で STLport だと using std::char_traits; みたいなのが必要かも
|
---|
34 | template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
|
---|
35 | class basic_dbg_streambuf: public std::basic_stringbuf<Ch_T, Tr_T>
|
---|
36 | {
|
---|
37 | protected:
|
---|
38 | std::string saveFilePath;
|
---|
39 | int count;
|
---|
40 | LoggerSetting setting;
|
---|
41 |
|
---|
42 | public:
|
---|
43 | basic_dbg_streambuf( const std::string &saveFilePath, bool isOptionEnabled )
|
---|
44 | : saveFilePath( saveFilePath )
|
---|
45 | , count( 0 )
|
---|
46 | {
|
---|
47 | #ifndef STDX_DSTREAM_BUFFERING
|
---|
48 | setbuf(0,0);
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | if( isOptionEnabled )
|
---|
52 | {
|
---|
53 | // 設定ファイルを読み込む
|
---|
54 | char temporary[MAX_PATH];
|
---|
55 | char temp2[MAX_PATH];
|
---|
56 | char temp3[MAX_PATH];
|
---|
57 | _splitpath(saveFilePath.c_str(),temporary,temp2,temp3,NULL);
|
---|
58 |
|
---|
59 | std::string settingXmlPath = (std::string)temporary + temp2 + temp3 + ".setting.xml";
|
---|
60 | if( Jenga::Common::Path( settingXmlPath ).IsExistFile() )
|
---|
61 | {
|
---|
62 | setting.ReadXml( settingXmlPath, false );
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | virtual ~basic_dbg_streambuf()
|
---|
68 | {
|
---|
69 | sync();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected:
|
---|
73 | int sync(void)
|
---|
74 | {
|
---|
75 | dbg_out(str().c_str());
|
---|
76 | pbump(static_cast<int>(pbase() - pptr()));
|
---|
77 | return 0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void dbg_out(const Ch_T*);
|
---|
81 | };
|
---|
82 |
|
---|
83 | template <>
|
---|
84 | inline void basic_dbg_streambuf<char>::dbg_out(const char *str)
|
---|
85 | {
|
---|
86 | std::ofstream ofs( ( saveFilePath ).c_str(), std::ios_base::app );
|
---|
87 | ofs << "[" << (count++) << "] " << str ;
|
---|
88 | ofs.close();
|
---|
89 |
|
---|
90 | OutputDebugString( str );
|
---|
91 |
|
---|
92 | if( (count-1) == setting.stopStep )
|
---|
93 | {
|
---|
94 | DebugBreak();
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
|
---|
99 | class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
|
---|
100 | {
|
---|
101 | public:
|
---|
102 | basic_dbg_ostream( const std::string &saveFilePath, bool isOptionEnabled )
|
---|
103 | : std::basic_ostream<Ch_T, Tr_T>(new basic_dbg_streambuf<Ch_T, Tr_T>(saveFilePath,isOptionEnabled))
|
---|
104 | {
|
---|
105 | std::ofstream ofs( ( saveFilePath ).c_str(), std::ios_base::trunc );
|
---|
106 | ofs.close();
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | virtual ~basic_dbg_ostream()
|
---|
111 | {
|
---|
112 | // flush(); // 不要らしい.http://www.tietew.jp/cppll/archive/607
|
---|
113 | delete rdbuf();
|
---|
114 | }
|
---|
115 | };
|
---|
116 |
|
---|
117 |
|
---|
118 | typedef basic_dbg_ostream<_TCHAR> Logger;
|
---|
119 |
|
---|
120 |
|
---|
121 | }}
|
---|
122 |
|
---|
123 | BOOST_CLASS_IMPLEMENTATION(Jenga::Common::LoggerSetting, boost::serialization::object_serializable);
|
---|