OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/mmo/main
[mmo/main.git] / common / Logger.hpp
1 //
2 // Logger.hpp
3 //
4
5 #pragma once
6 #include <iostream>
7 #include "unicode.hpp"
8
9 #ifndef _WIN32
10 #define OutputDebugString(str) (std::cout << str)
11 #endif
12
13 class Logger {
14         // Singleton
15     private:
16         Logger() : ofs_("mmo_log.txt") {}
17         Logger(const Logger& logger) {}
18         virtual ~Logger() {}
19
20     public:
21         static void Info(const tstring& format) {
22             getInstance().Log(_T("INFO: "), format);
23         }
24
25         template<class T1>
26         static void Info(const tstring& format, const T1& t1) {
27             getInstance().Log(_T("INFO: "), format, t1);
28         }
29
30         template<class T1, class T2>
31         static void Info(const tstring& format, const T1& t1, const T2& t2) {
32             getInstance().Log(_T("INFO: "), format, t1, t2);
33         }
34
35         template<class T1, class T2, class T3>
36         static void Info(const tstring& format, const T1& t1, const T2& t2, const T3& t3) {
37             getInstance().Log(_T("INFO: "), format, t1, t2, t3);
38         }
39
40         template<class T1, class T2, class T3, class T4>
41         static void Info(const tstring& format, const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
42             getInstance().Log(_T("INFO: "), format, t1, t2, t3, t4);
43         }
44
45
46         static void Error(const tstring& format) {
47             getInstance().Log(_T("ERROR: "), format);
48         }
49
50         template<class T1>
51         static void Error(const tstring& format, const T1& t1) {
52             getInstance().Log(_T("ERROR: "), format, t1);
53         }
54
55         template<class T1, class T2>
56         static void Error(const tstring& format, const T1& t1, const T2& t2) {
57             getInstance().Log(_T("ERROR: "), format, t1, t2);
58         }
59
60         template<class T1, class T2, class T3>
61         static void Error(const tstring& format, const T1& t1, const T2& t2, const T3& t3) {
62             getInstance().Log(_T("ERROR: "), format, t1, t2, t3);
63         }
64
65         template<class T1, class T2, class T3, class T4>
66         static void Error(const tstring& format, const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
67             getInstance().Log(_T("ERROR: "), format, t1, t2, t3, t4);
68         }
69
70
71         static void Debug(const tstring& format) {
72                 #ifdef _DEBUG
73             getInstance().Log(_T("DEBUG: "), format);
74                 #endif
75         }
76
77         template<class T1>
78         static void Debug(const tstring& format, const T1& t1) {
79                 #ifdef _DEBUG
80             getInstance().Log(_T("DEBUG: "), format, t1);
81                 #endif
82         }
83
84         template<class T1, class T2>
85         static void Debug(const tstring& format, const T1& t1, const T2& t2) {
86                 #ifdef _DEBUG
87             getInstance().Log(_T("DEBUG: "), format, t1, t2);
88                 #endif
89         }
90
91         template<class T1, class T2, class T3>
92         static void Debug(const tstring& format, const T1& t1, const T2& t2, const T3& t3) {
93                 #ifdef _DEBUG
94             getInstance().Log(_T("DEBUG: "), format, t1, t2, t3);
95                 #endif
96         }
97
98         template<class T1, class T2, class T3, class T4>
99         static void Debug(const tstring& format, const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
100                 #ifdef _DEBUG
101             getInstance().Log(_T("DEBUG: "), format, t1, t2, t3, t4);
102                 #endif
103         }
104
105
106     private:
107         typedef boost::basic_format<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR>> tformat;
108
109         static Logger& getInstance() {
110             static Logger instance;
111             return instance;
112         }
113
114         void Log(const tstring& prefix, const tstring& format) {
115             auto out = prefix + format + _T("\n");
116             OutputDebugString(out.c_str());
117             // std::wcout << unicode::ToWString(out);
118             //ofs_ << unicode::ToString(out);
119         }
120
121         template<class T1>
122         void Log(const tstring& prefix, const tstring& format, const T1& t1) {
123             auto out = prefix + (tformat(format) % t1).str() + _T("\n");
124             OutputDebugString(out.c_str());
125             // std::wcout << unicode::ToWString(out);
126             //ofs_ << unicode::ToString(out);
127         }
128
129         template<class T1, class T2>
130         void Log(const tstring& prefix, const tstring& format, const T1& t1, const T2& t2) {
131             auto out = prefix + (tformat(format) % t1 % t2).str() + _T("\n");
132             OutputDebugString(out.c_str());
133             // std::wcout << unicode::ToWString(out);
134             //ofs_ << unicode::ToString(out);
135         }
136
137         template<class T1, class T2, class T3>
138         void Log(const tstring& prefix, const tstring& format, const T1& t1, const T2& t2, const T3& t3) {
139             auto out = prefix + (tformat(format) % t1 % t2 % t3).str() + _T("\n");
140             OutputDebugString(out.c_str());
141             // std::wcout << unicode::ToWString(out);
142             //ofs_ << unicode::ToString(out);
143         }
144
145         template<class T1, class T2, class T3, class T4>
146         void Log(const tstring& prefix, const tstring& format, const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
147             auto out = prefix + (tformat(format) % t1 % t2 % t3 % t4).str() + _T("\n");
148             OutputDebugString(out.c_str());
149             // std::wcout << unicode::ToWString(out);
150             //ofs_ << unicode::ToString(out);
151         }
152
153         std::ofstream ofs_;
154 };