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

Last change on this file since 164 was 164, checked in by dai_9181, 17 years ago
File size: 1.7 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{
29public:
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
42protected:
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
53template <>
54inline void basic_dbg_streambuf<char>::dbg_out(const char *str)
55{
[164]56 ofstream ofs( ( Environment::GetAppDir() + "\\logger.log" ).c_str(), ios_base::app );
[154]57 ofs << str ;
58 ofs.close();
59}
60
61template <typename Ch_T, typename Tr_T = std::char_traits<Ch_T> >
62class basic_dbg_ostream: public std::basic_ostream<Ch_T, Tr_T>
63{
64public:
65basic_dbg_ostream() : std::basic_ostream<Ch_T, Tr_T>(new \
66 basic_dbg_streambuf<Ch_T, Tr_T>())
67 {
[164]68 ofstream ofs( ( Environment::GetAppDir() + "\\logger.log" ).c_str(), ios_base::trunc );
[154]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
80static basic_dbg_ostream<_TCHAR> logger;
[164]81
82
83}}
Note: See TracBrowser for help on using the repository browser.