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

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

Directoryクラスを追加

File size: 1.8 KB
RevLine 
[154]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
[164]14#include <jenga/include/common/Environment.h>
15
[154]16#define STDX_DSTREAM_BUFFERING
17
18
19using namespace std;
20
[164]21namespace Jenga{
22namespace Common{
[154]23
[164]24
[154]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{
[166]29protected:
30 std::string saveFilePath;
31
[154]32public:
[166]33 basic_dbg_streambuf( const std::string &saveFilePath )
34 : saveFilePath( saveFilePath )
35 {
[154]36#ifndef STDX_DSTREAM_BUFFERING
[166]37 setbuf(0,0);
[154]38#endif
[166]39 }
[154]40
[166]41 virtual ~basic_dbg_streambuf()
42 {
43 sync();
44 }
[154]45
46protected:
[166]47 int sync(void)
48 {
49 dbg_out(str().c_str());
50 pbump(static_cast<int>(pbase() - pptr()));
51 return 0;
52 }
[154]53
[166]54 void dbg_out(const Ch_T*);
[154]55};
56
57template <>
58inline void basic_dbg_streambuf<char>::dbg_out(const char *str)
59{
[166]60 ofstream ofs( ( saveFilePath ).c_str(), ios_base::app );
[154]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:
[166]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 );
[154]73 ofs.close();
[166]74 }
[154]75
[166]76
77 virtual ~basic_dbg_ostream()
78 {
79 // flush(); // 不要らしい.http://www.tietew.jp/cppll/archive/607
80 delete rdbuf();
81 }
[154]82};
83
84
[166]85typedef basic_dbg_ostream<_TCHAR> Logger;
[164]86
87
88}}
Note: See TracBrowser for help on using the repository browser.