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