source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/include/logger.h@ 519

Last change on this file since 519 was 519, checked in by dai_9181, 16 years ago

jenga.hを追加。

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