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