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

Last change on this file since 162 was 154, checked in by dai_9181, 17 years ago
File size: 1.8 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#define STDX_DSTREAM_BUFFERING
15
16
17using namespace std;
18
19
20// VC++ で STLport だと using std::char_traits; みたいなのが必要かも
21template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
22class basic_dbg_streambuf: public std::basic_stringbuf<Ch_T, Tr_T>
23{
24public:
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
37protected:
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
48template <>
49inline 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
56template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
57class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
58{
59public:
60basic_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
78static basic_dbg_ostream<_TCHAR> logger;
79#define trace(s) logger << s << endl
80#else
81#define trace(s)
82#endif
Note: See TracBrowser for help on using the repository browser.