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 |
|
---|
20 | // VC++ で STLport だと using std::char_traits; みたいなのが必要かも
|
---|
21 | template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
|
---|
22 | class basic_dbg_streambuf: public std::basic_stringbuf<Ch_T, Tr_T>
|
---|
23 | {
|
---|
24 | public:
|
---|
25 | basic_dbg_streambuf()
|
---|
26 | {
|
---|
27 | #ifndef STDX_DSTREAM_BUFFERING
|
---|
28 | setbuf(0,0);
|
---|
29 | #endif
|
---|
30 | }
|
---|
31 |
|
---|
32 | virtual ~basic_dbg_streambuf()
|
---|
33 | {
|
---|
34 | sync();
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected:
|
---|
38 | int sync(void)
|
---|
39 | {
|
---|
40 | dbg_out(str().c_str());
|
---|
41 | pbump(static_cast<int>(pbase() - pptr()));
|
---|
42 | return 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | void dbg_out(const Ch_T*);
|
---|
46 | };
|
---|
47 |
|
---|
48 | template <>
|
---|
49 | inline void basic_dbg_streambuf<char>::dbg_out(const char *str)
|
---|
50 | {
|
---|
51 | ofstream ofs( ( (string)/*BasicSystemDir +*/ "logger.log" ).c_str(), ios_base::app );
|
---|
52 | ofs << str ;
|
---|
53 | ofs.close();
|
---|
54 | }
|
---|
55 |
|
---|
56 | template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
|
---|
57 | class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | basic_dbg_ostream() : std::basic_ostream<Ch_T, Tr_T>(new \
|
---|
61 | basic_dbg_streambuf<Ch_T, Tr_T>())
|
---|
62 | {
|
---|
63 | ofstream ofs( ( (string)/*BasicSystemDir +*/ + "logger.log" ).c_str(), ios_base::trunc );
|
---|
64 | ofs.close();
|
---|
65 | }
|
---|
66 |
|
---|
67 | virtual ~basic_dbg_ostream()
|
---|
68 | {
|
---|
69 | // flush(); // 不要らしい.http://www.tietew.jp/cppll/archive/607
|
---|
70 | delete rdbuf();
|
---|
71 | }
|
---|
72 | };
|
---|
73 |
|
---|
74 | // ログ生成しない場合はこの下の行をコメントアウトする
|
---|
75 | #define USE_TRACE
|
---|
76 |
|
---|
77 | #ifdef USE_TRACE
|
---|
78 | static basic_dbg_ostream<_TCHAR> logger;
|
---|
79 | #define trace(s) logger << s << endl
|
---|
80 | #else
|
---|
81 | #define trace(s)
|
---|
82 | #endif
|
---|