Line data Source code
1 : //# PlotLogger.cc: Classes to perform various logging actions for the plotter.
2 : //# Copyright (C) 2009
3 : //# Associated Universities, Inc. Washington DC, USA.
4 : //#
5 : //# This library is free software; you can redistribute it and/or modify it
6 : //# under the terms of the GNU Library General Public License as published by
7 : //# the Free Software Foundation; either version 2 of the License, or (at your
8 : //# option) any later version.
9 : //#
10 : //# This library is distributed in the hope that it will be useful, but WITHOUT
11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 : //# License for more details.
14 : //#
15 : //# You should have received a copy of the GNU Library General Public License
16 : //# along with this library; if not, write to the Free Software Foundation,
17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 : //#
19 : //# Correspondence concerning AIPS++ should be addressed as follows:
20 : //# Internet email: casa-feedback@nrao.edu.
21 : //# Postal address: AIPS++ Project Office
22 : //# National Radio Astronomy Observatory
23 : //# 520 Edgemont Road
24 : //# Charlottesville, VA 22903-2475 USA
25 : //#
26 : //# $Id: $
27 : #include <graphics/GenericPlotter/PlotLogger.h>
28 :
29 : #include <casacore/casa/Logging/LogFilter.h>
30 : #include <casacore/casa/Logging/LogSink.h>
31 : #include <casacore/casa/Logging/StreamLogSink.h>
32 : #include <casacore/casa/OS/Memory.h>
33 : #include <graphics/GenericPlotter/Plotter.h>
34 :
35 : #include <fstream>
36 : #include <ctime>
37 :
38 : using namespace std;
39 :
40 : using namespace casacore;
41 : namespace casa {
42 :
43 : ////////////////////////////////
44 : // PLOTLOGMESSAGE DEFINITIONS //
45 : ////////////////////////////////
46 :
47 : // Static //
48 :
49 : const int PlotLogMessage::DEFAULT_EVENT_TYPE = PlotLogger::MSG_INFO;
50 :
51 :
52 : // Non-Static //
53 :
54 0 : PlotLogMessage::PlotLogMessage(int eventType) :
55 : LogMessage(PlotLogger::EVENT_PRIORITY(eventType)),
56 0 : m_eventType(eventType) { }
57 :
58 0 : PlotLogMessage::PlotLogMessage(const String& origin1, const String& origin2,
59 0 : int eventType) :
60 0 : LogMessage(LogOrigin(origin1, origin2),
61 : PlotLogger::EVENT_PRIORITY(eventType)),
62 0 : m_eventType(eventType) { }
63 :
64 0 : PlotLogMessage::PlotLogMessage(const String& origin1, const String& origin2,
65 0 : const String& message, int eventType) :
66 0 : LogMessage(message, LogOrigin(origin1, origin2),
67 : PlotLogger::EVENT_PRIORITY(eventType)),
68 0 : m_eventType(eventType) { }
69 :
70 0 : PlotLogMessage::PlotLogMessage(const PlotLogMessage& copy) : LogMessage(copy),
71 0 : m_eventType(copy.eventType()) { }
72 :
73 0 : PlotLogMessage::~PlotLogMessage() { }
74 :
75 0 : int PlotLogMessage::eventType() const { return m_eventType; }
76 :
77 :
78 : ////////////////////////////////////
79 : // PLOTLOGMEASUREMENT DEFINITIONS //
80 : ////////////////////////////////////
81 :
82 : // Static //
83 :
84 : const PlotLogMeasurement::TimeUnit PlotLogMeasurement::DEFAULT_TIME_UNIT =
85 : SECOND;
86 : const PlotLogMeasurement::MemoryUnit PlotLogMeasurement::DEFAULT_MEMORY_UNIT =
87 : KILOBYTE;
88 :
89 0 : String PlotLogMeasurement::timeUnits(TimeUnit t) {
90 0 : switch(t) {
91 0 : case SECOND: return "seconds";
92 :
93 0 : default: return "";
94 : }
95 : }
96 :
97 0 : String PlotLogMeasurement::memoryUnits(MemoryUnit m) {
98 0 : switch(m) {
99 0 : case BYTE: return "bytes";
100 0 : case KILOBYTE: return "kilobytes";
101 0 : case MEGABYTE: return "megabytes";
102 :
103 0 : default: return "";
104 : }
105 : }
106 :
107 :
108 : // Non-Static //
109 :
110 0 : PlotLogMeasurement::PlotLogMeasurement(const String& origin1,
111 : const String& origin2, TimeUnit timeUnit, MemoryUnit memoryUnit,
112 0 : int eventType) : PlotLogMessage(origin1, origin2, eventType),
113 0 : m_time(-1), m_memory(0), m_timeUnit(timeUnit),m_memoryUnit(memoryUnit){
114 0 : startMeasurement();
115 0 : }
116 :
117 0 : PlotLogMeasurement::PlotLogMeasurement(const PlotLogMeasurement& copy) :
118 0 : PlotLogMessage(copy), m_startTime(copy.m_startTime),
119 0 : m_startMemory(copy.m_startMemory), m_time(copy.m_time),
120 0 : m_memory(copy.m_memory), m_timeUnit(copy.m_timeUnit),
121 0 : m_memoryUnit(copy.m_memoryUnit) { }
122 :
123 0 : PlotLogMeasurement::~PlotLogMeasurement() { }
124 :
125 :
126 0 : time_t PlotLogMeasurement::startTime() const { return m_startTime; }
127 0 : unsigned int PlotLogMeasurement::startMemory() const { return m_startMemory; }
128 0 : double PlotLogMeasurement::time() const { return m_time; }
129 0 : double PlotLogMeasurement::memory() const { return m_memory; }
130 0 : PlotLogMeasurement::TimeUnit PlotLogMeasurement::timeUnit() const {
131 0 : return m_timeUnit; }
132 0 : PlotLogMeasurement::MemoryUnit PlotLogMeasurement::memoryUnit() const {
133 0 : return m_memoryUnit; }
134 :
135 0 : void PlotLogMeasurement::startMeasurement() {
136 0 : m_startTime = std::time(NULL);
137 0 : m_startMemory = Memory::allocatedMemoryInBytes();
138 0 : }
139 :
140 0 : void PlotLogMeasurement::stopMeasurement() {
141 : // Get measurement values.
142 0 : time_t t = std::time(NULL);
143 0 : m_time = t - m_startTime;
144 0 : m_memory = ((double)Memory::allocatedMemoryInBytes()) - m_startMemory;
145 0 : if(m_memoryUnit == KILOBYTE) m_memory /= 1024;
146 0 : else if(m_memoryUnit == MEGABYTE) m_memory /= 1024 * 1024;
147 :
148 : // Set string message.
149 0 : stringstream ss;
150 0 : ss << "END Time: ";
151 0 : if(m_time >= 0) ss << "+" << m_time << " " << timeUnits(m_timeUnit);
152 0 : else ss << "unreported";
153 0 : ss << ". Memory: ";
154 0 : if(m_memory >= 0) ss << "+";
155 0 : ss << m_memory << " " << memoryUnits(m_memoryUnit) << '.';
156 0 : message(ss.str(), true);
157 :
158 0 : startMeasurement();
159 0 : }
160 :
161 :
162 : ///////////////////////////////
163 : // PLOTLOGLOCATE DEFINITIONS //
164 : ///////////////////////////////
165 :
166 : // Static //
167 :
168 0 : PlotLogLocate PlotLogLocate::canvasLocate(PlotCanvas* canv,
169 : const PlotRegion& reg) {
170 0 : if(canv == NULL)
171 0 : return PlotLogLocate("PlotLogLocate", "canvasLocate", reg, NULL);
172 :
173 0 : return PlotLogLocate("PlotLogLocate","canvasLocate",reg,canv->locate(reg));
174 : }
175 :
176 :
177 : // Non-Static //
178 :
179 0 : PlotLogLocate::PlotLogLocate(const String& origin1, const String& origin2,
180 : const PlotRegion& locateRegion,
181 : vector<vector<pair<unsigned int,unsigned int> > >* locatedIndices,
182 0 : int eventType, bool deleteIndicesOnDestruction) :
183 0 : PlotLogMessage(origin1, origin2, eventType), m_region(locateRegion),
184 0 : m_indices(locatedIndices), m_shouldDelete(deleteIndicesOnDestruction) {
185 : // Generate message.
186 0 : stringstream ss;
187 0 : unsigned int np = numSearchedPlots(), ni = numLocatedIndices();
188 0 : ss << "Locating indices with x in [" << m_region.left() << ", "
189 0 : << m_region.right() << "] and y in [" << m_region.bottom() << ", "
190 0 : << m_region.top() << ". Searched " << np << " plots and found " << ni
191 0 : << " indices.";
192 0 : if(np > 0 && ni > 0) ss << '\n';
193 :
194 0 : bool first = true;
195 : unsigned int n, from, to;
196 0 : for(unsigned int i = 0; i < np; i++) {
197 0 : n = m_indices->at(i).size();
198 0 : if(n > 0) {
199 0 : if(!first) ss << '\n';
200 0 : first = false;
201 : }
202 0 : if(np > 0 && n > 0) ss << "Plot " << i << ": ";
203 0 : if(n > 0) ss << "[";
204 :
205 0 : for(unsigned int j = 0; j < n; ++j) {
206 0 : from = (*m_indices)[i][j].first;
207 0 : to = (*m_indices)[i][j].second;
208 0 : ss << from;
209 0 : if(to != from) ss << "-" << to;
210 0 : if(j < n - 1) ss << ", ";
211 : }
212 0 : if(n > 0) ss << "]";
213 : }
214 :
215 0 : message(ss.str(), true);
216 0 : }
217 :
218 0 : PlotLogLocate::PlotLogLocate(const PlotLogLocate& copy) : PlotLogMessage(copy),
219 0 : m_region(copy.locateRegion()), m_indices(copy.indices()),
220 0 : m_shouldDelete(copy.willDeleteIndices()) {
221 0 : const_cast<PlotLogLocate&>(copy).m_shouldDelete = false;
222 0 : }
223 :
224 0 : PlotLogLocate::~PlotLogLocate() {
225 0 : if(m_shouldDelete && m_indices != NULL) delete m_indices;
226 0 : }
227 :
228 :
229 0 : const PlotRegion& PlotLogLocate::locateRegion() const { return m_region; }
230 0 : unsigned int PlotLogLocate::numLocatedIndices() const {
231 0 : if(m_indices == NULL) return 0;
232 0 : unsigned int n = 0;
233 0 : for(unsigned int i = 0; i < m_indices->size(); i++)
234 0 : for(unsigned int j = 0; j < m_indices->at(i).size(); j++)
235 0 : n += (*m_indices)[i][j].second - (*m_indices)[i][j].first + 1;
236 :
237 0 : return n;
238 : }
239 :
240 0 : unsigned int PlotLogLocate::numSearchedPlots() const {
241 0 : return (m_indices != NULL) ? m_indices->size() : 0; }
242 :
243 : vector<vector<pair<unsigned int, unsigned int> > >*
244 0 : PlotLogLocate::indices() const { return m_indices; }
245 :
246 : vector<pair<unsigned int, unsigned int> >*
247 0 : PlotLogLocate::plotIndices(unsigned int index) const {
248 0 : if(m_indices == NULL || index >= m_indices->size()) return NULL;
249 0 : else return &(*m_indices)[index];
250 : }
251 :
252 0 : bool PlotLogLocate::willDeleteIndices() const { return m_shouldDelete; }
253 :
254 :
255 : ///////////////////////////////
256 : // PLOTLOGMETHOD DEFINITIONS //
257 : ///////////////////////////////
258 :
259 0 : PlotLogMethod::PlotLogMethod(const String& className, const String& methodName,
260 0 : bool entering, const String& message, int eventType) :
261 0 : PlotLogMessage(className, methodName, eventType) {
262 0 : stringstream ss;
263 0 : if(entering) ss << "ENTERING.";
264 0 : else ss << "EXITING. ";
265 0 : if(!message.empty()) ss << " " << message;
266 :
267 0 : PlotLogMessage::message(ss.str(), true);
268 0 : }
269 :
270 0 : PlotLogMethod::~PlotLogMethod() { }
271 :
272 :
273 : ///////////////////////////////
274 : // PLOTLOGOBJECT DEFINITIONS //
275 : ///////////////////////////////
276 :
277 0 : PlotLogObject::PlotLogObject(const String& className, void* address,
278 0 : bool creation, const String& message, int eventType) :
279 0 : PlotLogMessage(className, creation ? "alloc" : "dealloc", eventType) {
280 0 : stringstream ss;
281 0 : if(creation) ss << "Creating";
282 0 : else ss << "Destroying";
283 0 : ss << " object at " << address << ".";
284 0 : if(!message.empty()) ss << " " << message;
285 :
286 0 : PlotLogMessage::message(ss.str(), true);
287 0 : }
288 :
289 0 : PlotLogObject::~PlotLogObject() { }
290 :
291 :
292 : //////////////////////////////////
293 : // PLOTLOGGERFILTER DEFINITIONS //
294 : //////////////////////////////////
295 :
296 0 : PlotLoggerFilter::PlotLoggerFilter(int eventFlags,
297 0 : LogMessage::Priority minPriority) : m_eventFlags(eventFlags),
298 0 : m_minPriority(minPriority) { }
299 :
300 0 : PlotLoggerFilter::~PlotLoggerFilter() { }
301 :
302 0 : LogFilterInterface* PlotLoggerFilter::clone() const {
303 0 : return new PlotLoggerFilter(m_eventFlags, m_minPriority); }
304 :
305 0 : Bool PlotLoggerFilter::pass(const LogMessage& message) const {
306 0 : if(message.priority() < m_minPriority) return false;
307 :
308 : /*
309 : * This causes an error when a non-PlotLogMessage is sent into the filter.
310 : * Cannot dynamic cast because LogMessage is not polymorphic. :(
311 : * Instead, moved this functionality to PlotLogger::postMessage()...
312 : try {
313 : int type = ((const PlotLogMessage&)message).eventType();
314 : return type <= 0 || m_eventFlags & type;
315 : } catch(...) { return true; }
316 : */
317 0 : return true;
318 : }
319 :
320 0 : int PlotLoggerFilter::eventFlags() const { return m_eventFlags; }
321 0 : void PlotLoggerFilter::setEventFlags(int flags) { m_eventFlags = flags; }
322 :
323 0 : LogMessage::Priority PlotLoggerFilter::minimumPriority() const {
324 0 : return m_minPriority; }
325 0 : void PlotLoggerFilter::setMinimumPriority(LogMessage::Priority minPriority) {
326 0 : m_minPriority = minPriority; }
327 :
328 :
329 : ////////////////////////////
330 : // PLOTLOGGER DEFINITIONS //
331 : ////////////////////////////
332 :
333 : // Static //
334 :
335 0 : int PlotLogger::ALL_EVENTS_FLAG() {
336 0 : int flag = 0;
337 0 : vector<int> v = ALL_EVENTS();
338 0 : for(unsigned int i = 0; i < v.size(); i++) flag |= v[i];
339 0 : return flag;
340 0 : }
341 :
342 0 : vector<int> PlotLogger::ALL_EVENTS() {
343 0 : vector<int> v(6 + EXTENDED_TYPES.size());
344 0 : v[0] = MSG_DEBUG; v[1] = DRAW_TOTAL;
345 0 : v[2] = DRAW_INDIVIDUAL; v[3] = METHODS_MAJOR;
346 0 : v[4] = OBJECTS_MAJOR; v[5] = EXPORT_TOTAL;
347 0 : for(unsigned int i = 6; i < v.size(); i++)
348 0 : v[i] = EXTENDED_TYPES[i - 6];
349 0 : return v;
350 : }
351 :
352 0 : int PlotLogger::REGISTER_EVENT_TYPE(const String& name,
353 : LogMessage::Priority priority) {
354 : static int value = EXPORT_TOTAL;
355 0 : value *= 2;
356 0 : EXTENDED_TYPES.push_back(value);
357 0 : EXTENDED_NAMES.push_back(name);
358 0 : SET_EVENT_PRIORITY(value, priority);
359 0 : return value;
360 : }
361 :
362 0 : void PlotLogger::UNREGISTER_EVENT_TYPE(int event) {
363 0 : for(unsigned int i = 0; i < EXTENDED_TYPES.size(); i++) {
364 0 : if(event == EXTENDED_TYPES[i]) {
365 0 : EXTENDED_TYPES.erase(EXTENDED_TYPES.begin() + i);
366 0 : EXTENDED_NAMES.erase(EXTENDED_NAMES.begin() + i);
367 : }
368 : }
369 0 : }
370 :
371 0 : void PlotLogger::UNREGISTER_EVENT_TYPE(const String& name) {
372 0 : for(unsigned int i = 0; i < EXTENDED_TYPES.size(); i++) {
373 0 : if(name == EXTENDED_NAMES[i]) {
374 0 : EXTENDED_TYPES.erase(EXTENDED_TYPES.begin() + i);
375 0 : EXTENDED_NAMES.erase(EXTENDED_NAMES.begin() + i);
376 : }
377 : }
378 0 : }
379 :
380 0 : vector<String> PlotLogger::EVENT_NAMES() {
381 0 : vector<int> e = ALL_EVENTS();
382 0 : vector<String> v(e.size());
383 0 : for(unsigned int i = 0; i < v.size(); i++) v[i] = EVENT(e[i]);
384 0 : return v;
385 0 : }
386 :
387 0 : String PlotLogger::EVENT(int type) {
388 0 : if(type == MSG_INFO) return "MSG_INFO";
389 0 : else if(type == MSG_WARN) return "MSG_WARN";
390 0 : else if(type == MSG_ERROR) return "MSG_ERROR";
391 0 : else if(type == MSG_DEBUG) return "MSG_DEBUG";
392 0 : else if(type == DRAW_TOTAL) return "DRAW_TOTAL";
393 0 : else if(type == DRAW_INDIVIDUAL) return "DRAW_INDIVIDUAL";
394 0 : else if(type == METHODS_MAJOR) return "METHODS_MAJOR";
395 0 : else if(type == OBJECTS_MAJOR) return "OBJECTS_MAJOR";
396 0 : else if(type == EXPORT_TOTAL) return "EXPORT_TOTAL";
397 : else {
398 0 : for(unsigned int i = 0; i < EXTENDED_TYPES.size(); i++)
399 0 : if(type == EXTENDED_TYPES[i]) return EXTENDED_NAMES[i];
400 0 : return "";
401 : }
402 : }
403 :
404 0 : int PlotLogger::EVENT(const String& name) {
405 0 : if(name == EVENT(MSG_INFO)) return MSG_INFO;
406 0 : else if(name == EVENT(MSG_WARN)) return MSG_WARN;
407 0 : else if(name == EVENT(MSG_ERROR)) return MSG_ERROR;
408 0 : else if(name == EVENT(MSG_DEBUG)) return MSG_DEBUG;
409 0 : else if(name == EVENT(DRAW_TOTAL)) return DRAW_TOTAL;
410 0 : else if(name == EVENT(DRAW_INDIVIDUAL)) return DRAW_INDIVIDUAL;
411 0 : else if(name == EVENT(METHODS_MAJOR)) return METHODS_MAJOR;
412 0 : else if(name == EVENT(OBJECTS_MAJOR)) return OBJECTS_MAJOR;
413 0 : else if(name == EVENT(EXPORT_TOTAL)) return EXPORT_TOTAL;
414 : else {
415 0 : for(unsigned int i = 0; i < EXTENDED_NAMES.size(); i++)
416 0 : if(name == EXTENDED_NAMES[i]) return EXTENDED_TYPES[i];
417 0 : return NO_EVENTS;
418 : }
419 : }
420 :
421 0 : int PlotLogger::FLAG_FROM_EVENTS(const vector<int>& events) {
422 0 : int flag = 0;
423 0 : for(unsigned int i = 0; i < events.size(); i++) flag |= events[i];
424 0 : return flag;
425 : }
426 :
427 0 : int PlotLogger::FLAG_FROM_EVENTS(const vector<String>& names) {
428 0 : int flag = 0;
429 0 : for(unsigned int i = 0; i < names.size(); i++) flag |= EVENT(names[i]);
430 0 : return flag;
431 : }
432 :
433 0 : int PlotLogger::FLAG_FROM_PRIORITY(LogMessage::Priority minPriority) {
434 0 : int flag = 0;
435 0 : vector<int> v = ALL_EVENTS();
436 0 : for(unsigned int i = 0; i < v.size(); i++)
437 0 : if(EVENT_PRIORITY(v[i]) >= minPriority) flag |= v[i];
438 0 : return flag;
439 0 : }
440 :
441 0 : LogMessage::Priority PlotLogger::EVENT_PRIORITY(int event) {
442 0 : if(EVENT_PRIORITIES.find(event) == EVENT_PRIORITIES.end()) {
443 0 : LogMessage::Priority p = LogMessage::NORMAL;
444 0 : if(event == MSG_DEBUG || event == METHODS_MAJOR ||
445 0 : event == OBJECTS_MAJOR) p = LogMessage::DEBUGGING;
446 0 : else if(event == DRAW_INDIVIDUAL) p = LogMessage::NORMAL5;
447 0 : else if(event == MSG_WARN) p = LogMessage::WARN;
448 0 : else if(event == MSG_ERROR) p = LogMessage::SEVERE;
449 0 : EVENT_PRIORITIES[event] = p;
450 : }
451 0 : return EVENT_PRIORITIES[event];
452 : }
453 :
454 0 : void PlotLogger::SET_EVENT_PRIORITY(int event, LogMessage::Priority priority) {
455 0 : EVENT_PRIORITIES[event] = priority; }
456 :
457 :
458 : // Subclass of LogFilterInterface that refuses all messages. Used to disable
459 : // the global log sink temporarily.
460 : // <group>
461 : class NullLogFilter : public LogFilterInterface {
462 : public:
463 0 : NullLogFilter() { }
464 : NullLogFilter(const NullLogFilter& copy):
465 : LogFilterInterface()
466 : { (void)copy; }
467 0 : ~NullLogFilter() { }
468 0 : LogFilterInterface* clone() const {
469 0 : return new NullLogFilter();
470 : }
471 0 : Bool pass(const LogMessage& message) const {
472 : (void)message;
473 0 : return false;
474 : }
475 : };
476 : // </group>
477 :
478 :
479 0 : void PlotLogger::disableGlobalSink() {
480 0 : if(DISABLED_GLOBAL_FILTER == NULL) {
481 0 : DISABLED_GLOBAL_FILTER = LogSink::globalSink().filter().clone();
482 0 : LogSink::globalSink().filter(NullLogFilter());
483 : }
484 0 : }
485 :
486 0 : void PlotLogger::enableGlobalSink() {
487 0 : if(DISABLED_GLOBAL_FILTER != NULL) {
488 0 : LogSink::globalSink().filter(*DISABLED_GLOBAL_FILTER);
489 0 : delete DISABLED_GLOBAL_FILTER;
490 0 : DISABLED_GLOBAL_FILTER = NULL;
491 : }
492 0 : }
493 :
494 :
495 : vector<int> PlotLogger::EXTENDED_TYPES = vector<int>();
496 :
497 : vector<String> PlotLogger::EXTENDED_NAMES = vector<String>();
498 :
499 : map<int, LogMessage::Priority> PlotLogger::EVENT_PRIORITIES =
500 : map<int, LogMessage::Priority>();
501 :
502 : LogFilterInterface* PlotLogger::DISABLED_GLOBAL_FILTER = NULL;
503 :
504 :
505 : // Non-Static //
506 :
507 0 : PlotLogger::PlotLogger(Plotter*, int filterEventFlags,
508 0 : LogMessage::Priority filterMinPriority) :
509 0 : m_logger(&LogSink::globalSink(), false),
510 0 : m_filter(filterEventFlags, filterMinPriority) {
511 0 : m_logger->filter(m_filter);
512 0 : }
513 :
514 0 : PlotLogger::~PlotLogger() { }
515 :
516 0 : CountedPtr<LogSinkInterface> PlotLogger::sink() { return m_logger; }
517 0 : const CountedPtr<LogSinkInterface> PlotLogger::sink() const {
518 0 : return m_logger; }
519 :
520 0 : LogSinkInterface* PlotLogger::localSinkCopy() const {
521 0 : if(m_loggerLocation.empty()) return &LogSink::globalSink();
522 0 : else if(m_logger.null()) return NULL;
523 : else {
524 0 : const StreamLogSink* s= dynamic_cast<const StreamLogSink*>(&*m_logger);
525 0 : if(s == NULL) return const_cast<LogSinkInterface*>(&*m_logger);
526 0 : else return new StreamLogSink(*s);
527 : }
528 : }
529 :
530 0 : const String& PlotLogger::sinkLocation() const { return m_loggerLocation; }
531 0 : void PlotLogger::setSinkLocation(const String& logFile) {
532 0 : CountedPtr<LogSinkInterface> oldSink = m_logger;
533 : try {
534 0 : if(logFile.empty())
535 0 : m_logger = CountedPtr<LogSinkInterface>(&LogSink::globalSink(),
536 0 : false);
537 : else
538 : m_logger = new StreamLogSink(LogMessage::NORMAL,
539 0 : new ofstream(logFile.c_str(), ios::app), true);
540 0 : m_logger->filter(m_filter);
541 0 : m_loggerLocation = logFile;
542 0 : } catch(...) {
543 0 : m_logger = oldSink;
544 0 : }
545 0 : }
546 :
547 :
548 0 : LogMessage::Priority PlotLogger::filterMinPriority() const {
549 0 : return m_filter.minimumPriority(); }
550 0 : void PlotLogger::setFilterMinPriority(PlotLogMessage::Priority minPriority) {
551 0 : if(minPriority != m_filter.minimumPriority()) {
552 0 : m_filter.setMinimumPriority(minPriority);
553 0 : if(!m_logger.null()) m_logger->filter(m_filter);
554 : }
555 0 : }
556 :
557 0 : bool PlotLogger::filterEventFlag(int flag) const {
558 0 : return flag <= 0 || flag & m_filter.eventFlags(); }
559 0 : void PlotLogger::setFilterEventFlag(int flag, bool on) {
560 0 : int flags = m_filter.eventFlags();
561 0 : if(on && !(flags & flag)) setFilterEventFlags(flags | flag);
562 0 : else if(!on && (flags & flag)) setFilterEventFlags(flags & ~flag);
563 0 : }
564 :
565 0 : int PlotLogger::filterEventFlags() const { return m_filter.eventFlags(); }
566 0 : void PlotLogger::setFilterEventFlags(int flags) {
567 0 : if(flags != m_filter.eventFlags()) {
568 0 : m_filter.setEventFlags(flags);
569 0 : if(!m_logger.null()) m_logger->filter(m_filter);
570 : }
571 0 : }
572 :
573 0 : void PlotLogger::postMessage(const PlotLogMessage& message) {
574 : try {
575 0 : if(!m_logger.null()) {
576 : // Do events type check here.
577 0 : int type = message.eventType();
578 0 : if(type <= 0 || m_filter.eventFlags() & type)
579 0 : m_logger->postLocally(message);
580 : }
581 0 : } catch(...) { }
582 0 : }
583 :
584 0 : void PlotLogger::postMessage(const String& origin1, const String& origin2,
585 : const String& message, int eventType) {
586 0 : postMessage(PlotLogMessage(origin1, origin2, message, eventType)); }
587 :
588 0 : PlotLogMessage PlotLogger::markMeasurement(const String& origin1,
589 : const String& origin2, int eventType, bool postStartMessage) {
590 : PlotLogMessage startMessage(origin1, origin2,
591 0 : "START Current memory usage: " +
592 0 : String::toString(Memory::allocatedMemoryInBytes()/1024.0) + " " +
593 0 : PlotLogMeasurement::memoryUnits(PlotLogMeasurement::KILOBYTE)+".",
594 0 : eventType);
595 0 : if(postStartMessage) postMessage(startMessage);
596 0 : m_measurements.push_back(PlotLogMeasurement(origin1, origin2,
597 : PlotLogMeasurement::DEFAULT_TIME_UNIT,
598 : PlotLogMeasurement::DEFAULT_MEMORY_UNIT, eventType));
599 0 : return startMessage;
600 0 : }
601 :
602 0 : PlotLogMeasurement PlotLogger::releaseMeasurement(bool postReleaseMessage) {
603 0 : if(m_measurements.size() == 0) // invalid
604 0 : return PlotLogMeasurement("PlotLogger", "releaseMeasurement");
605 :
606 : // Go through and update previous times.
607 0 : PlotLogMeasurement m = m_measurements[m_measurements.size() - 1];
608 0 : m_measurements.erase(m_measurements.begin() + m_measurements.size() - 1);
609 0 : m.stopMeasurement();
610 0 : if(postReleaseMessage) postMessage(m);
611 :
612 0 : return m;
613 0 : }
614 :
615 0 : PlotLogLocate PlotLogger::locate(PlotCanvas* canvas, const PlotRegion& region,
616 : int eventType, bool postLocateMessage) {
617 0 : if(canvas == NULL) return PlotLogLocate("", "", region, NULL);
618 :
619 0 : vector<vector<pair<unsigned int, unsigned int> > >* res = canvas->locate(
620 : region);
621 0 : PlotLogLocate msg("PlotCanvas", "locate", region, res, eventType, false);
622 0 : if(postLocateMessage) postMessage(msg);
623 0 : return msg;
624 0 : }
625 :
626 : }
|