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