casacore
LoggerHolder.h
Go to the documentation of this file.
1 //# LoggerHolder.h: Class holding a hierarchy of loggers
2 //# Copyright (C) 2001,2003
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: aips2-request@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 //#
27 //# $Id$
28 
29 #ifndef TABLES_LOGGERHOLDER_H
30 #define TABLES_LOGGERHOLDER_H
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
34 #include <casacore/casa/Logging/LogIO.h>
35 #include <casacore/casa/Containers/Block.h>
36 #include <casacore/casa/Utilities/CountedPtr.h>
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 //# Forward Declarations
41 class LoggerHolderRep;
42 class LoggerHolderIterator;
43 class TableLogSink;
44 
45 // <summary>
46 // Class holding a hierarchy of loggers.
47 // </summary>
48 
49 // <use visibility=export>
50 
51 // <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
52 // </reviewed>
53 
54 // <prerequisite>
55 // <li> <linkto class="LogIO">LogIO</linkto> <li>
56 // </prerequisite>
57 
58 // <synopsis>
59 // The LoggerHolder class implements a hierarchy of loggers.
60 // It has a log sink of its own and can have multiple parent LoggerHolder
61 // objects representing the log info of parent objects.
62 // It is used by class
63 // <linkto class=ImageInterface>ImageInterface</linkto>, but could also
64 // be used elsewhere.
65 //
66 // The sink of a LoggerHolder can be different depending on the type of image.
67 // E.g. for a transient image it can be a
68 // <linkto class=MemoryLogSink>MemoryLogSink</linkto>, while for a persistent
69 // image it will be a <linkto class=TableLogSink>TableLogSink</linkto>.
70 // <br>An important feature is that an LoggerHolder can have zero or more
71 // parent LoggerHolder objects. In that way the log of the parent object
72 // of an image object can be made part of the log of the image object itself,
73 // without having to copy the log.
74 //
75 // To iterate through all messages in a LoggerHolder (including all parents),
76 // the <linkto class=LoggerHolderIterator>LoggerHolderIterator</linkto> can
77 // be used. This is an STL-style const_iterator object.
78 //
79 // LoggerHolder uses reference counting
80 // (of class <linkto class=LoggerHolderRep>LoggerHolderRep</linkto>)
81 // to be able to retain
82 // the object after the (ImageInterface) object containing it is gone.
83 // Otherwise classes like SubImage would lose their log info.
84 // </synopsis>
85 
86 // <example>
87 // <srcblock>
88 // LoggerHolder logger ("tLoggerHolder_tmp.log", True);
89 // logger.logio() << "test1" << LogIO::POST;
90 // logger.logio() << "test2" << LogIO::POST;
91 // for (LoggerHolder::const_iterator iter = logger.begin();
92 // iter != logger.end();
93 // iter++) {
94 // cout << iter->time() << ' ' << iter->message() << endl;
95 // }
96 // </srcblock>
97 // This example shows the construction of an LoggerHolder with a
98 // TableLogSink sink. Thereafter some messages are written.
99 // The latter part shows how to iterate through all messages.
100 //
101 // <srcblock>
102 // LoggerHolder logger (False);
103 // logger.addParent (parent.logger());
104 // logger.logio() << "test1" << LogIO::POST;
105 // logger.logio() << "test2" << LogIO::POST;
106 // </srcblock>
107 // This example shows the construction of an LoggerHolder with a
108 // MemoryLogSink sink (e.g. for a SubImage). Thereafter the logger of
109 // the parent image is added to it.
110 // Finally some messages are written.
111 // </example>
112 
113 // <motivation>
114 // This class simplifies and unifies all Image logging activities.
115 // </motivation>
116 
117 //# <todo asof="2001/06/14">
118 //# </todo>
119 
121 {
122 public:
123  // Create with a NullSink or MemoryLogSink (default).
124  explicit LoggerHolder (Bool nullSink = False);
125 
126  // Create with a TableLogSink.
127  LoggerHolder (const String& logTableName, Bool isWritable);
128 
129  // Copy constructor (reference sematics).
131 
133 
134  // Assignment (reference semantics).
136 
137  // Add a logger from a parent.
138  void addParent (const LoggerHolder&);
139 
140  // Append the entries of the other logger to this one.
141  void append (const LoggerHolder& other);
142 
143  // Reopen a readonly logtable for read/write (if needed).
144  void reopenRW();
145 
146  // Reopen the log table if needed (after a tempClose).
147  void reopen();
148 
149  // Temporarily close all log tables.
150  // By default the possible parent log tables are also closed.
151  void tempClose (Bool closeParents = True) const;
152 
153  // Unlock the log table.
154  void unlock();
155 
156  // Flush the log table.
157  void flush();
158 
159  // Resync the log table (if needed).
160  void resync();
161 
162  // Is the log table temporarily closed?
163  Bool isTempClosed() const;
164 
165  // Get access to the logger.
166  // It assumes that it will be used to post a message, so it reopens
167  // the log table for read/write if needed).
168  LogIO& logio();
169 
170  // Get access to the log sink (reopen the log table if needed).
171  // It is not assumed you want to write. If you want to do that,
172  // you should first call reopenRW() to ensure you can write.
173  // <group>
174  LogSink& sink();
175  const LogSink& sink() const;
176  // </group>
177 
178  // Clear the log.
179  // It removes the parents and removes all messages from the sink.
180  void clear();
181 
182  // Remove all parents.
184 
185  // Return the block of parents.
186  const Block<LoggerHolder>& parents() const;
187 
188  // Define the STL-style iterators.
189  // Only a const forward iterator is available.
190  // It makes it possible to iterate through all messages in the logger.
191  // <srcblock>
192  // LoggerHolder logger("log.name", False)
193  // for (LoggerHolder::const_iterator iter=arr.begin();
194  // iter!=arr.end(); iter++) {
195  // cout << iter.message() << endl;
196  // }
197  // </srcblock>
198  // <group name=STL-iterator>
199  // STL-style typedefs.
201  // Get the begin and end iterator object.
202  const_iterator begin() const;
203  const_iterator end() const;
204  // </group>
205 
206 
207 private:
209 };
210 
211 
212 
213 
214 // <summary>
215 // Representation of the class holding a hierarchy of loggers.
216 // </summary>
217 
218 // <use visibility=local>
219 
220 // <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
221 // </reviewed>
222 
223 // <prerequisite>
224 // <li> <linkto class="LogIO">LogIO</linkto> <li>
225 // </prerequisite>
226 
227 // <synopsis>
228 // The LoggerHolderRep class is the reference counted implementation
229 // of <linkto class=LoggerHolder>LoggerHolder</linkto>.
230 // See that class for more information.
231 // </synopsis>
232 
233 // <motivation>
234 // Reference counting was needed to be able to keep a LoggerHolder
235 // object after the (ImageInterface) object containing it is gone.
236 // </motivation>
237 
238 //# <todo asof="2001/06/14">
239 //# </todo>
240 
242 {
243 public:
244  // Create with a NullSink or MemoryLogSink (default).
245  LoggerHolderRep (Bool nullSink);
246 
247  // Create with a TableLogSink.
248  LoggerHolderRep (const String& logTableName, Bool isWritable);
249 
250  // Copy constructor.
252 
254 
255  // Assignment.
256  // It removes the current parents.
258 
259  // Add a logger from a parent.
260  void addParent (const LoggerHolder&);
261 
262  // Append the entries of the other logger to this one.
263  void append (const LoggerHolder& other);
264 
265  // Reopen a readonly logtable for read/write (if needed).
266  void reopenRW();
267 
268  // Reopen the log table if needed (after a tempClose).
269  void reopen()
270  { if (itsIsClosed) doReopen(); }
271 
272  // Temporarily close all log tables.
273  // By default the possible parent log tables are also closed.
274  void tempClose (Bool closeParents = True);
275 
276  // Unlock the log table.
277  void unlock();
278 
279  // Flush the log table.
280  void flush();
281 
282  // Resync the log table (if needed).
283  void resync();
284 
285  // Is the log table temporarily closed?
287  { return itsIsClosed; }
288 
289  // Get access to the logger.
290  // It assumes that it will be used to post a message, so it reopens
291  // the log table for read/write if needed).
293 
294  // Get access to the log sink (reopen the log table if needed).
295  // It is not assumed you want to write. If you want to do that,
296  // you should first call reopenRW() to ensure you can write.
298 
299  // Clear the log.
300  // It removes the parents and removes all messages from the sink.
301  void clear();
302 
303  // Remove all parents.
305 
306  // Return the block of parents.
308  { return itsParents; }
309 
310  // Define the STL-style iterators.
311  // Only a const forward iterator is available.
312  // It makes it possible to iterate through all messages in the logger.
313  // <srcblock>
314  // LoggerHolder logger("log.name", False)
315  // for (LoggerHolder::const_iterator iter=arr.begin();
316  // iter!=arr.end(); iter++) {
317  // cout << iter.message() << endl;
318  // }
319  // </srcblock>
320  // <group name=STL-iterator-rep>
321  // STL-style typedefs.
323  // Get the begin and end iterator object.
324  const_iterator begin() const;
326  // </group>
327 
328 
329 private:
330  // Do the actual reopen.
331  void doReopen();
332 
333 
341 };
342 
343 
344 
345 
346 // <summary>
347 // Class representing an entry in a LoggerHolder.
348 // </summary>
349 
350 // <use visibility=local>
351 
352 // <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
353 // </reviewed>
354 
355 // <prerequisite>
356 // <li> <linkto class="LoggerHolder">LoggerHolder</linkto> <li>
357 // </prerequisite>
358 
359 // <synopsis>
360 // This class makes it possible to use the iterator in the STL-style.
361 // It only contains a 'pointer' to the current entry in the current logger.
362 // Function like <src>time()</src> can be used to retrieve the message parts.
363 // </synopsis>
364 
366 {
367 public:
369  : itsSink(0), itsIndex(0) {}
370 
371  LogHolderIterEntry (const LogSink* sink, uInt index)
372  : itsSink(sink), itsIndex(index) {}
373 
375  : itsSink(that.itsSink), itsIndex(that.itsIndex) {}
376 
378  {}
379 
381  { itsSink=that.itsSink; itsIndex=that.itsIndex; return *this; }
382 
383  // Get the message parts.
384  // <group>
385  Double time() const
386  { return itsSink->getTime(itsIndex); }
387  String message() const
388  { return itsSink->getMessage(itsIndex); }
389  String priority() const
390  { return itsSink->getPriority(itsIndex); }
391  String location() const
392  { return itsSink->getLocation(itsIndex); }
393  String objectID() const
394  { return itsSink->getObjectID(itsIndex); }
395  // </group>
396 
397 private:
398  const LogSink* itsSink;
400 };
401 
402 
403 
404 
405 // <summary>
406 // Class doing the actual iteration through an LoggerHolder.
407 // </summary>
408 
409 // <use visibility=local>
410 
411 // <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
412 // </reviewed>
413 
414 // <prerequisite>
415 // <li> <linkto class="LoggerHolder">LoggerHolder</linkto> <li>
416 // </prerequisite>
417 
418 // <synopsis>
419 // This class makes it possible to use the iterator in the STL-style.
420 // It is used by
421 //<linkto class=LoggerHolderIterator>LoggerHolderIterator</linkto>
422 // which is the class as seen by the user.
423 // LogHolderIter makes it easier to make the first entry available on
424 // construction of an LoggerHolderIterator.
425 // </synopsis>
426 
428 {
429 public:
430  // Construct the iterator on the given LoggerHolderRep.
432 
434 
435  // Increment to next message.
436  // Returns False if at the end.
438 
439  // Get the entry.
441  { return itsEntry; }
442 
443  const LoggerHolder& logger() const
444  { return *itsLogger; }
445 
446 private:
447  // Copy constructor is not needed, thus forbidden.
449 
450  // Assignment is not needed, thus forbidden.
452 
453 
459 };
460 
461 
462 
463 // <summary>
464 // Class to iterate through an LoggerHolder.
465 // </summary>
466 
467 // <use visibility=export>
468 
469 // <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
470 // </reviewed>
471 
472 // <prerequisite>
473 // <li> <linkto class="LoggerHolder">LoggerHolder</linkto> <li>
474 // </prerequisite>
475 
476 // <synopsis>
477 // This class makes it possible to iterate in the STL-style through all
478 // entries of an LoggerHolder object. If the logger has parent LoggerHolder
479 // objects, it first iterates through all parents (recursively) and
480 // finally through all entries in the LoggerHolder object itself.
481 // </synopsis>
482 
483 // <example>
484 // <srcblock>
485 // LoggerHolder logger ("tLoggerHolder_tmp.log", True);
486 // logger.logio() << "test1" << LogIO::POST;
487 // logger.logio() << "test2" << LogIO::POST;
488 // for (LoggerHolder::const_iterator iter = logger.begin();
489 // iter != logger.end();
490 // iter++) {
491 // cout << iter->time() << ' ' << iter->message() << endl;
492 // }
493 // </srcblock>
494 // </example>
495 
497 {
498 public:
500  : itsIter(0), itsNotAtEnd(False) {}
501 
503 
505 
507  { delete itsIter; }
508 
510 
511  // Increment to next message.
512  // <group>
513  void operator++()
514  { next(); }
515  void operator++ (int)
516  { next(); }
517  // </group>
518 
519  // Is the iterator not at the end yet?
521  { return itsNotAtEnd; }
522 
523  // Get the entry.
524  // <group>
526  { return itsIter->getEntry(); }
528  { return &(itsIter->getEntry()); }
529  // </group>
530 
531  const LoggerHolder& logger() const
532  { return itsIter->logger(); }
533 
534 private:
535  // Get the next entry (if available).
536  void next()
537  { itsNotAtEnd = itsIter->next(); }
538 
539 
542 };
543 
544 
545 
546 inline void LoggerHolder::reopen()
547 {
548  itsRep->reopen();
549 }
551 {
552  return itsRep->isTempClosed();
553 }
555 {
556  return itsRep->logio();
557 }
559 {
560  return itsRep->sink();
561 }
562 inline const LogSink& LoggerHolder::sink() const
563 {
564  return itsRep->sink();
565 }
567 {
568  return itsRep->parents();
569 }
571 {
572  return LoggerHolderIterator (this);
573 }
575 {
576  return LoggerHolderIterator();
577 }
578 
579 
580 
581 
582 } //# NAMESPACE CASACORE - END
583 
584 #endif
simple 1-D array
Definition: Block.h:200
Referenced counted pointer for constant data.
Definition: CountedPtr.h:81
Class representing an entry in a LoggerHolder.
Definition: LoggerHolder.h:366
LogHolderIterEntry(const LogHolderIterEntry &that)
Definition: LoggerHolder.h:374
LogHolderIterEntry(const LogSink *sink, uInt index)
Definition: LoggerHolder.h:371
LogHolderIterEntry & operator=(const LogHolderIterEntry &that)
Definition: LoggerHolder.h:380
Double time() const
Get the message parts.
Definition: LoggerHolder.h:385
Class doing the actual iteration through an LoggerHolder.
Definition: LoggerHolder.h:428
const LoggerHolder * itsLogger
Definition: LoggerHolder.h:454
LogHolderIter & operator=(const LogHolderIter &)
Assignment is not needed, thus forbidden.
LogHolderIter * itsParentIter
Definition: LoggerHolder.h:456
const LogHolderIterEntry & getEntry() const
Get the entry.
Definition: LoggerHolder.h:440
LogHolderIter(const LogHolderIter &)
Copy constructor is not needed, thus forbidden.
const LoggerHolder & logger() const
Definition: LoggerHolder.h:443
LogHolderIterEntry itsEntry
Definition: LoggerHolder.h:458
LogHolderIter(const LoggerHolder *)
Construct the iterator on the given LoggerHolderRep.
Bool next()
Increment to next message.
virtual String getLocation(uInt i) const
virtual String getMessage(uInt i) const
virtual Double getTime(uInt i) const
Get given part of the i-th message from the local sink.
virtual String getObjectID(uInt i) const
virtual String getPriority(uInt i) const
Class to iterate through an LoggerHolder.
Definition: LoggerHolder.h:497
void next()
Get the next entry (if available).
Definition: LoggerHolder.h:536
LoggerHolderIterator(const LoggerHolder *)
void operator++()
Increment to next message.
Definition: LoggerHolder.h:513
Bool operator!=(const LoggerHolderIterator &)
Is the iterator not at the end yet?
Definition: LoggerHolder.h:520
const LogHolderIterEntry * operator->() const
Definition: LoggerHolder.h:527
const LoggerHolder & logger() const
Definition: LoggerHolder.h:531
const LogHolderIterEntry & operator*() const
Get the entry.
Definition: LoggerHolder.h:525
LoggerHolderIterator(const LoggerHolderIterator &)
LoggerHolderIterator & operator=(const LoggerHolderIterator &)
Representation of the class holding a hierarchy of loggers.
Definition: LoggerHolder.h:242
void doReopen()
Do the actual reopen.
LogIO & logio()
Get access to the logger.
LoggerHolderRep(const String &logTableName, Bool isWritable)
Create with a TableLogSink.
void tempClose(Bool closeParents=True)
Temporarily close all log tables.
LoggerHolderRep(Bool nullSink)
Create with a NullSink or MemoryLogSink (default).
LoggerHolderRep(const LoggerHolderRep &)
Copy constructor.
void reopen()
Reopen the log table if needed (after a tempClose).
Definition: LoggerHolder.h:269
void unlock()
Unlock the log table.
LoggerHolderIterator const_iterator
Define the STL-style iterators.
Definition: LoggerHolder.h:323
LogSink & sink()
Get access to the log sink (reopen the log table if needed).
void addParent(const LoggerHolder &)
Add a logger from a parent.
Block< LoggerHolder > itsParents
Definition: LoggerHolder.h:334
void reopenRW()
Reopen a readonly logtable for read/write (if needed).
void removeParents()
Remove all parents.
LoggerHolderRep & operator=(const LoggerHolderRep &)
Assignment.
void flush()
Flush the log table.
const_iterator begin() const
Get the begin and end iterator object.
const Block< LoggerHolder > & parents() const
Return the block of parents.
Definition: LoggerHolder.h:307
void resync()
Resync the log table (if needed).
void append(const LoggerHolder &other)
Append the entries of the other logger to this one.
TableLogSink * itsTablePtr
Definition: LoggerHolder.h:338
const_iterator end() const
Bool isTempClosed() const
Is the log table temporarily closed?
Definition: LoggerHolder.h:286
void clear()
Clear the log.
void tempClose(Bool closeParents=True) const
Temporarily close all log tables.
void append(const LoggerHolder &other)
Append the entries of the other logger to this one.
LogIO & logio()
Get access to the logger.
Definition: LoggerHolder.h:554
LoggerHolderIterator const_iterator
Define the STL-style iterators.
Definition: LoggerHolder.h:201
void reopenRW()
Reopen a readonly logtable for read/write (if needed).
void resync()
Resync the log table (if needed).
void addParent(const LoggerHolder &)
Add a logger from a parent.
void clear()
Clear the log.
LoggerHolder(const String &logTableName, Bool isWritable)
Create with a TableLogSink.
LoggerHolder(const LoggerHolder &)
Copy constructor (reference sematics).
void unlock()
Unlock the log table.
const_iterator begin() const
Get the begin and end iterator object.
Definition: LoggerHolder.h:570
CountedPtr< LoggerHolderRep > itsRep
Definition: LoggerHolder.h:208
LoggerHolder(Bool nullSink=False)
Create with a NullSink or MemoryLogSink (default).
LogSink & sink()
Get access to the log sink (reopen the log table if needed).
Definition: LoggerHolder.h:558
LoggerHolder & operator=(const LoggerHolder &)
Assignment (reference semantics).
void reopen()
Reopen the log table if needed (after a tempClose).
Definition: LoggerHolder.h:546
const Block< LoggerHolder > & parents() const
Return the block of parents.
Definition: LoggerHolder.h:566
Bool isTempClosed() const
Is the log table temporarily closed?
Definition: LoggerHolder.h:550
void flush()
Flush the log table.
const_iterator end() const
Definition: LoggerHolder.h:574
void removeParents()
Remove all parents.
String: the storage and methods of handling collections of characters.
Definition: String.h:225
this file contains all the compiler specific defines
Definition: mainpage.dox:28
const Bool False
Definition: aipstype.h:44
unsigned int uInt
Definition: aipstype.h:51
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
const Bool True
Definition: aipstype.h:43
double Double
Definition: aipstype.h:55