source: dev/trunk/jenga/include/common/logger.h@ 205

Last change on this file since 205 was 205, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

File size: 2.8 KB
RevLine 
[154]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
[164]14#include <jenga/include/common/Environment.h>
[205]15#include <jenga/include/common/BoostXmlSupport.h>
[164]16
[154]17#define STDX_DSTREAM_BUFFERING
18
19
20using namespace std;
21
[164]22namespace Jenga{
23namespace Common{
[154]24
[164]25
[205]26class LoggerSetting : public BoostXmlSupport<LoggerSetting>
27{
28public:
29 int stopStep;
30
31 LoggerSetting()
32 : stopStep( -1 )
33 {
34 }
35
36 // XMLシリアライズ用
37private:
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
[154]50// VC++ で STLport だと using std::char_traits; みたいなのが必要かも
51template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
52class basic_dbg_streambuf: public std::basic_stringbuf<Ch_T, Tr_T>
53{
[166]54protected:
55 std::string saveFilePath;
[205]56 int count;
57 LoggerSetting setting;
[166]58
[154]59public:
[205]60 basic_dbg_streambuf( const std::string &saveFilePath, bool isOptionEnabled )
[166]61 : saveFilePath( saveFilePath )
[205]62 , count( 0 )
[166]63 {
[154]64#ifndef STDX_DSTREAM_BUFFERING
[166]65 setbuf(0,0);
[154]66#endif
[205]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 setting.Read( (string)temporary + temp2 + temp3 + ".setting.xml", false );
76 }
[166]77 }
[154]78
[166]79 virtual ~basic_dbg_streambuf()
80 {
81 sync();
82 }
[154]83
84protected:
[166]85 int sync(void)
86 {
87 dbg_out(str().c_str());
88 pbump(static_cast<int>(pbase() - pptr()));
89 return 0;
90 }
[154]91
[166]92 void dbg_out(const Ch_T*);
[154]93};
94
95template <>
96inline void basic_dbg_streambuf<char>::dbg_out(const char *str)
97{
[166]98 ofstream ofs( ( saveFilePath ).c_str(), ios_base::app );
[205]99 ofs << "[" << (count++) << "] " << str ;
[154]100 ofs.close();
[205]101
102 if( (count-1) == setting.stopStep )
103 {
104 DebugBreak();
105 }
[154]106}
107
108template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
109class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
110{
111public:
[205]112 basic_dbg_ostream( const string &saveFilePath, bool isOptionEnabled )
113 : std::basic_ostream<Ch_T, Tr_T>(new basic_dbg_streambuf<Ch_T, Tr_T>(saveFilePath,isOptionEnabled))
[166]114 {
115 ofstream ofs( ( saveFilePath ).c_str(), ios_base::trunc );
[154]116 ofs.close();
[166]117 }
[154]118
[166]119
120 virtual ~basic_dbg_ostream()
121 {
122 // flush(); // 不要らしい.http://www.tietew.jp/cppll/archive/607
123 delete rdbuf();
124 }
[154]125};
126
127
[166]128typedef basic_dbg_ostream<_TCHAR> Logger;
[164]129
130
131}}
[205]132
133BOOST_CLASS_IMPLEMENTATION(Jenga::Common::LoggerSetting, boost::serialization::object_serializable);
Note: See TracBrowser for help on using the repository browser.