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 | public:
|
---|
30 | basic_dbg_streambuf()
|
---|
31 | {
|
---|
32 | #ifndef STDX_DSTREAM_BUFFERING
|
---|
33 | setbuf(0,0);
|
---|
34 | #endif
|
---|
35 | }
|
---|
36 |
|
---|
37 | virtual ~basic_dbg_streambuf()
|
---|
38 | {
|
---|
39 | sync();
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected:
|
---|
43 | int sync(void)
|
---|
44 | {
|
---|
45 | dbg_out(str().c_str());
|
---|
46 | pbump(static_cast<int>(pbase() - pptr()));
|
---|
47 | return 0;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void dbg_out(const Ch_T*);
|
---|
51 | };
|
---|
52 |
|
---|
53 | template <>
|
---|
54 | inline void basic_dbg_streambuf<char>::dbg_out(const char *str)
|
---|
55 | {
|
---|
56 | ofstream ofs( ( Environment::GetAppDir() + "\\logger.log" ).c_str(), ios_base::app );
|
---|
57 | ofs << str ;
|
---|
58 | ofs.close();
|
---|
59 | }
|
---|
60 |
|
---|
61 | template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
|
---|
62 | class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
|
---|
63 | {
|
---|
64 | public:
|
---|
65 | basic_dbg_ostream() : std::basic_ostream<Ch_T, Tr_T>(new \
|
---|
66 | basic_dbg_streambuf<Ch_T, Tr_T>())
|
---|
67 | {
|
---|
68 | ofstream ofs( ( Environment::GetAppDir() + "\\logger.log" ).c_str(), ios_base::trunc );
|
---|
69 | ofs.close();
|
---|
70 | }
|
---|
71 |
|
---|
72 | virtual ~basic_dbg_ostream()
|
---|
73 | {
|
---|
74 | // flush(); // 不要らしい.http://www.tietew.jp/cppll/archive/607
|
---|
75 | delete rdbuf();
|
---|
76 | }
|
---|
77 | };
|
---|
78 |
|
---|
79 |
|
---|
80 | static basic_dbg_ostream<_TCHAR> logger;
|
---|
81 |
|
---|
82 |
|
---|
83 | }}
|
---|