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 |
|
---|
16 | #define STDX_DSTREAM_BUFFERING
|
---|
17 |
|
---|
18 |
|
---|
19 | using namespace std;
|
---|
20 |
|
---|
21 | namespace Jenga{
|
---|
22 | namespace Common{
|
---|
23 |
|
---|
24 |
|
---|
25 | // VC++ で STLport だと using std::char_traits; みたいなのが必要かも
|
---|
26 | template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
|
---|
27 | class basic_dbg_streambuf: public std::basic_stringbuf<Ch_T, Tr_T>
|
---|
28 | {
|
---|
29 | protected:
|
---|
30 | std::string saveFilePath;
|
---|
31 |
|
---|
32 | public:
|
---|
33 | basic_dbg_streambuf( const std::string &saveFilePath )
|
---|
34 | : saveFilePath( saveFilePath )
|
---|
35 | {
|
---|
36 | #ifndef STDX_DSTREAM_BUFFERING
|
---|
37 | setbuf(0,0);
|
---|
38 | #endif
|
---|
39 | }
|
---|
40 |
|
---|
41 | virtual ~basic_dbg_streambuf()
|
---|
42 | {
|
---|
43 | sync();
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected:
|
---|
47 | int sync(void)
|
---|
48 | {
|
---|
49 | dbg_out(str().c_str());
|
---|
50 | pbump(static_cast<int>(pbase() - pptr()));
|
---|
51 | return 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | void dbg_out(const Ch_T*);
|
---|
55 | };
|
---|
56 |
|
---|
57 | template <>
|
---|
58 | inline void basic_dbg_streambuf<char>::dbg_out(const char *str)
|
---|
59 | {
|
---|
60 | ofstream ofs( ( saveFilePath ).c_str(), ios_base::app );
|
---|
61 | ofs << str ;
|
---|
62 | ofs.close();
|
---|
63 | }
|
---|
64 |
|
---|
65 | template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
|
---|
66 | class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
|
---|
67 | {
|
---|
68 | public:
|
---|
69 | basic_dbg_ostream( const string &saveFilePath )
|
---|
70 | : std::basic_ostream<Ch_T, Tr_T>(new basic_dbg_streambuf<Ch_T, Tr_T>(saveFilePath))
|
---|
71 | {
|
---|
72 | ofstream ofs( ( saveFilePath ).c_str(), ios_base::trunc );
|
---|
73 | ofs.close();
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | virtual ~basic_dbg_ostream()
|
---|
78 | {
|
---|
79 | // flush(); // 不要らしい.http://www.tietew.jp/cppll/archive/607
|
---|
80 | delete rdbuf();
|
---|
81 | }
|
---|
82 | };
|
---|
83 |
|
---|
84 |
|
---|
85 | typedef basic_dbg_ostream<_TCHAR> Logger;
|
---|
86 |
|
---|
87 |
|
---|
88 | }}
|
---|