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

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

Directoryクラスを追加

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#include <jenga/include/common/Environment.h>
15
16#define STDX_DSTREAM_BUFFERING
17
18
19using namespace std;
20
21namespace Jenga{
22namespace Common{
23
24
25// VC++ で STLport だと using std::char_traits; みたいなのが必要かも
26template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
27class basic_dbg_streambuf: public std::basic_stringbuf<Ch_T, Tr_T>
28{
29protected:
30 std::string saveFilePath;
31
32public:
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
46protected:
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
57template <>
58inline 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
65template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
66class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
67{
68public:
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
85typedef basic_dbg_ostream<_TCHAR> Logger;
86
87
88}}
Note: See TracBrowser for help on using the repository browser.