Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | // ========== Copyright Header Begin ========================================== |
2 | // | |
3 | // OpenSPARC T2 Processor File: logger.hpp | |
4 | // Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved | |
5 | // 4150 Network Circle, Santa Clara, California 95054, U.S.A. | |
6 | // | |
7 | // * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
8 | // | |
9 | // This program is free software; you can redistribute it and/or modify | |
10 | // it under the terms of the GNU General Public License as published by | |
11 | // the Free Software Foundation; version 2 of the License. | |
12 | // | |
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | // | |
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | // | |
22 | // For the avoidance of doubt, and except that if any non-GPL license | |
23 | // choice is available it will apply instead, Sun elects to use only | |
24 | // the General Public License version 2 (GPLv2) at this time for any | |
25 | // software where a choice of GPL license versions is made | |
26 | // available with the language indicating that GPLv2 or any later version | |
27 | // may be used, or where a choice of which version of the GPL is applied is | |
28 | // otherwise unspecified. | |
29 | // | |
30 | // Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
31 | // CA 95054 USA or visit www.sun.com if you need additional information or | |
32 | // have any questions. | |
33 | // | |
34 | // ========== Copyright Header End ============================================ | |
35 | #ifndef INC_Logger_hpp__ | |
36 | #define INC_Logger_hpp__ | |
37 | ||
38 | #include <iostream> | |
39 | #include <fstream> | |
40 | #include <string> | |
41 | #include "config.hpp" | |
42 | ||
43 | namespace Logger { | |
44 | ||
45 | #ifdef LOG_SYSTEMC | |
46 | #ifdef LOG_SERVICE | |
47 | #define WHERE_S __FILE__,__LINE__, "" | |
48 | #define TIME "@(" << sc_time_stamp() << ")\t" | |
49 | #else | |
50 | #define WHERE __FILE__,__LINE__, name() | |
51 | #define WHERE_S __FILE__,__LINE__, "" | |
52 | #define TIME "@(" << sc_time_stamp() << " from: " << inst << ")\t" | |
53 | #endif // LOG_SERVICE | |
54 | #else | |
55 | #define WHERE __FILE__,__LINE__, "" | |
56 | #define TIME "\t" | |
57 | #endif //LOG_SYSTEMC | |
58 | ||
59 | #define LOG_ALWAYS (logger::instance()->log_always(WHERE)) | |
60 | #define LOG_ERROR (logger::instance()->log_error(WHERE)) | |
61 | #define LOG_WARNING (logger::instance()->log_warning(WHERE)) | |
62 | #define LOG_NORMAL (logger::instance()->log_normal(WHERE)) | |
63 | #define LOG_VERBOSE (logger::instance()->log_verbose(WHERE)) | |
64 | #define LOG_INFO (logger::instance()->log_info(WHERE)) | |
65 | #define LOG_DEBUG (logger::instance()->log_debug(WHERE)) | |
66 | ||
67 | #define LOG_ALWAYS_S (logger::instance()->log_always(WHERE_S)) | |
68 | #define LOG_ERROR_S (logger::instance()->log_error(WHERE_S)) | |
69 | #define LOG_WARNING_S (logger::instance()->log_warning(WHERE_S)) | |
70 | #define LOG_NORMAL_S (logger::instance()->log_normal(WHERE_S)) | |
71 | #define LOG_VERBOSE_S (logger::instance()->log_verbose(WHERE_S)) | |
72 | #define LOG_INFO_S (logger::instance()->log_info(WHERE_S)) | |
73 | #define LOG_DEBUG_S (logger::instance()->log_debug(WHERE_S)) | |
74 | ||
75 | ||
76 | USING_NAMESPACE(std) | |
77 | ||
78 | enum LogLevel {NEVER, ALWAYS, ERROR, WARNING, NORMAL, VERBOSE, INFO, DEBUG, ALL}; | |
79 | ||
80 | class logger | |
81 | { | |
82 | public: | |
83 | static logger* instance(LogLevel log_l = ALL) { | |
84 | if (inst == NULL) { | |
85 | inst = new logger(log_l); | |
86 | } | |
87 | return inst; | |
88 | } | |
89 | ||
90 | inline logger& log_always (string file, int line, string inst) { | |
91 | log_stat = ALWAYS; | |
92 | if (disp_level >= log_stat) { | |
93 | (*disp_stream) << "\n-------------------------------------------------------------------------------\n"; | |
94 | (*disp_stream) << TIME << "@(" << file << ":" << line << "):\n"; | |
95 | } | |
96 | if (log_level >= log_stat) { | |
97 | (*log_stream) << "\n-------------------------------------------------------------------------------\n"; | |
98 | (*log_stream) << TIME << "@(" << file << ":" << line << "):\n"; | |
99 | log_stream->flush(); | |
100 | } | |
101 | return (*this); | |
102 | } | |
103 | ||
104 | inline logger& log_error (string file, int line, string inst) { | |
105 | log_stat = ERROR; | |
106 | if (disp_level >= log_stat) { | |
107 | (*disp_stream) << "\n-------------------------------------------------------------------------------\n"; | |
108 | (*disp_stream) << TIME << "ERROR@(" << file << ":" << line << "):\n"; | |
109 | } | |
110 | if (log_level >= log_stat) { | |
111 | (*log_stream) << "\n-------------------------------------------------------------------------------\n"; | |
112 | (*log_stream) << TIME << "ERROR@(" << file << ":" << line << "):\n"; | |
113 | log_stream->flush(); | |
114 | } | |
115 | return (*this); | |
116 | } | |
117 | ||
118 | inline logger& log_warning (string file, int line, string inst) { | |
119 | log_stat = WARNING; | |
120 | if (disp_level >= log_stat) { | |
121 | (*disp_stream) << "\n-------------------------------------------------------------------------------\n"; | |
122 | (*disp_stream) << TIME << "WARNING@(" << file << ":" << line << "):\n"; | |
123 | } | |
124 | if (log_level >= log_stat) { | |
125 | (*log_stream) << "\n-------------------------------------------------------------------------------\n"; | |
126 | (*log_stream) << TIME << "WARNING@(" << file << ":" << line << "):\n"; | |
127 | log_stream->flush(); | |
128 | } | |
129 | return (*this); | |
130 | } | |
131 | ||
132 | inline logger& log_normal (string file, int line, string inst) { | |
133 | log_stat = NORMAL; | |
134 | if (disp_level >= log_stat) { | |
135 | (*disp_stream) << "\n-------------------------------------------------------------------------------\n"; | |
136 | (*disp_stream) << TIME << "@(" << file << ":" << line << "):\n"; | |
137 | } | |
138 | if (log_level >= log_stat) { | |
139 | (*log_stream) << "\n-------------------------------------------------------------------------------\n"; | |
140 | (*log_stream) << TIME << "@(" << file << ":" << line << "):\n"; | |
141 | log_stream->flush(); | |
142 | } | |
143 | return (*this); | |
144 | } | |
145 | ||
146 | inline logger& log_info (string file, int line, string inst) { | |
147 | log_stat = INFO; | |
148 | if (disp_level >= log_stat) { | |
149 | (*disp_stream) << "\n-------------------------------------------------------------------------------\n"; | |
150 | (*disp_stream) << TIME << "INFO@(" << file << ":" << line << "):\n"; | |
151 | } | |
152 | if (log_level >= log_stat) { | |
153 | (*log_stream) << "\n-------------------------------------------------------------------------------\n"; | |
154 | (*log_stream) << TIME << "INFO@(" << file << ":" << line << "):\n"; | |
155 | log_stream->flush(); | |
156 | } | |
157 | return (*this); | |
158 | } | |
159 | ||
160 | inline logger& log_verbose (string file, int line, string inst) { | |
161 | log_stat = VERBOSE; | |
162 | if (disp_level >= log_stat) { | |
163 | (*disp_stream) << "\n-------------------------------------------------------------------------------\n"; | |
164 | (*disp_stream) << TIME << "VERBOSE@(" << file << ":" << line << "):\n"; | |
165 | } | |
166 | if (log_level >= log_stat) { | |
167 | (*log_stream) << "\n-------------------------------------------------------------------------------\n"; | |
168 | (*log_stream) << TIME << "VERBOSE@(" << file << ":" << line << "):\n"; | |
169 | log_stream->flush(); | |
170 | } | |
171 | return (*this); | |
172 | } | |
173 | ||
174 | inline logger& log_debug (string file, int line, string inst) { | |
175 | log_stat = DEBUG; | |
176 | if (disp_level >= log_stat) { | |
177 | (*disp_stream) << "\n-------------------------------------------------------------------------------\n"; | |
178 | (*disp_stream) << TIME << "DEBUG@(" << file << ":" << line << "):\n"; | |
179 | } | |
180 | if (log_level >= log_stat) { | |
181 | (*log_stream) << "\n-------------------------------------------------------------------------------\n"; | |
182 | (*log_stream) << TIME << "DEBUG@(" << file << ":" << line << "):\n"; | |
183 | log_stream->flush(); | |
184 | } | |
185 | return (*this); | |
186 | } | |
187 | ||
188 | ||
189 | logger& operator<<(const string __s) { | |
190 | if (disp_level >= log_stat) | |
191 | (*disp_stream) << __s; | |
192 | if (log_level >= log_stat) | |
193 | (*log_stream) << __s; | |
194 | return *this; | |
195 | } | |
196 | ||
197 | logger& operator<<(const int __i) { | |
198 | if (disp_level >= log_stat) | |
199 | (*disp_stream) << __i; | |
200 | if (log_level >= log_stat) | |
201 | (*log_stream) << __i; | |
202 | return *this; | |
203 | } | |
204 | ||
205 | logger& operator<<(int8 __i) { | |
206 | if (disp_level >= log_stat) | |
207 | (*disp_stream) << __i; | |
208 | if (log_level >= log_stat) | |
209 | (*log_stream) << __i; | |
210 | ||
211 | return *this; | |
212 | } | |
213 | logger& operator<<(int16 __i) { | |
214 | if (disp_level >= log_stat) | |
215 | (*disp_stream) << __i; | |
216 | if (log_level >= log_stat) | |
217 | (*log_stream) << __i; | |
218 | ||
219 | return *this; | |
220 | } | |
221 | logger& operator<<(int32 __i) { | |
222 | if (disp_level >= log_stat) | |
223 | (*disp_stream) << __i; | |
224 | if (log_level >= log_stat) | |
225 | (*log_stream) << __i; | |
226 | ||
227 | return *this; | |
228 | } | |
229 | logger& operator<<(int64 __i) { | |
230 | if (disp_level >= log_stat) | |
231 | (*disp_stream) << __i; | |
232 | if (log_level >= log_stat) | |
233 | (*log_stream) << __i; | |
234 | ||
235 | return *this; | |
236 | } | |
237 | logger& operator<<(const unsigned int __i) { | |
238 | if (disp_level >= log_stat) | |
239 | (*disp_stream) << __i; | |
240 | if (log_level >= log_stat) | |
241 | (*log_stream) << __i; | |
242 | ||
243 | return *this; | |
244 | } | |
245 | ||
246 | logger& operator<<(uint8 __i) { | |
247 | if (disp_level >= log_stat) | |
248 | (*disp_stream) << __i; | |
249 | if (log_level >= log_stat) | |
250 | (*log_stream) << __i; | |
251 | ||
252 | return *this; | |
253 | } | |
254 | logger& operator<<(uint16 __i) { | |
255 | if (disp_level >= log_stat) | |
256 | (*disp_stream) << __i; | |
257 | if (log_level >= log_stat) | |
258 | (*log_stream) << __i; | |
259 | ||
260 | return *this; | |
261 | } | |
262 | logger& operator<<(uint32 __i) { | |
263 | if (disp_level >= log_stat) | |
264 | (*disp_stream) << __i; | |
265 | if (log_level >= log_stat) | |
266 | (*log_stream) << __i; | |
267 | ||
268 | return *this; | |
269 | } | |
270 | logger& operator<<(uint64 __i) { | |
271 | if (disp_level >= log_stat) | |
272 | (*disp_stream) << __i; | |
273 | if (log_level >= log_stat) | |
274 | (*log_stream) << __i; | |
275 | ||
276 | return *this; | |
277 | } | |
278 | ||
279 | logger& operator<<(double __i) { | |
280 | if (disp_level >= log_stat) | |
281 | (*disp_stream) << __i; | |
282 | if (log_level >= log_stat) | |
283 | (*log_stream) << __i; | |
284 | ||
285 | return *this; | |
286 | } | |
287 | ||
288 | protected: | |
289 | logger(LogLevel log_l = ALL, LogLevel disp_l = NORMAL, const char* log_file = "simulation.log") { | |
290 | disp_stream = &cerr; | |
291 | log_stream = new ofstream(log_file, ios::out); | |
292 | (*log_stream) << hex; | |
293 | (*disp_stream) << hex; | |
294 | #ifdef NO_LOG | |
295 | log_level = NEVER; | |
296 | disp_level = NEVER; | |
297 | #else | |
298 | #ifdef DEBUG_LOG | |
299 | log_level = DEBUG; | |
300 | disp_level = NORMAL; | |
301 | #else | |
302 | #ifdef NORMAL_LOG | |
303 | log_level = NORMAL; | |
304 | disp_level = NORMAL; | |
305 | #else | |
306 | #ifdef ALWAYS_LOG | |
307 | log_level = ALWAYS; | |
308 | disp_level = NORMAL; | |
309 | ||
310 | #else | |
311 | #ifdef ALL_LOG | |
312 | log_level = log_l; | |
313 | disp_level = disp_l; | |
314 | #else | |
315 | log_level = NEVER; | |
316 | disp_level = NEVER; | |
317 | #endif | |
318 | #endif | |
319 | #endif | |
320 | #endif | |
321 | #endif | |
322 | ||
323 | ||
324 | } | |
325 | ||
326 | public: | |
327 | LogLevel log_level; | |
328 | ||
329 | private: | |
330 | LogLevel disp_level; | |
331 | LogLevel log_stat; | |
332 | ||
333 | ofstream *log_stream; | |
334 | ostream *disp_stream; | |
335 | static logger *inst; | |
336 | ||
337 | }; | |
338 | ||
339 | } // namespace logger | |
340 | ||
341 | #endif // INC_Logger_hpp__ |