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;
39class LoggerHolderIterator;
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{
119public:
120 // Create with a NullSink or MemoryLogSink (default).
121 explicit LoggerHolder (Bool nullSink = False);
122
123 // Create with a TableLogSink.
124 LoggerHolder (const String& logTableName, Bool isWritable);
125
126 // Copy constructor (reference semantics).
128
130
131 // Assignment (reference semantics).
133
134 // Add a logger from a parent.
135 void addParent (const LoggerHolder&);
136
137 // Append the entries of the other logger to this one.
138 void append (const LoggerHolder& other);
139
140 // Reopen a readonly logtable for read/write (if needed).
141 void reopenRW();
142
143 // Reopen the log table if needed (after a tempClose).
144 void reopen();
145
146 // Temporarily close all log tables.
147 // By default the possible parent log tables are also closed.
148 void tempClose (Bool closeParents = True) const;
149
150 // Unlock the log table.
151 void unlock();
152
153 // Flush the log table.
154 void flush();
155
156 // Resync the log table (if needed).
157 void resync();
158
159 // Is the log table temporarily closed?
160 Bool isTempClosed() const;
161
162 // Get access to the logger.
163 // It assumes that it will be used to post a message, so it reopens
164 // the log table for read/write if needed).
165 LogIO& logio();
166
167 // Get access to the log sink (reopen the log table if needed).
168 // It is not assumed you want to write. If you want to do that,
169 // you should first call reopenRW() to ensure you can write.
170 // <group>
171 LogSink& sink();
172 const LogSink& sink() const;
173 // </group>
174
175 // Clear the log.
176 // It removes the parents and removes all messages from the sink.
177 void clear();
178
179 // Remove all parents.
181
182 // Return the block of parents.
183 const Block<LoggerHolder>& parents() const;
184
185 // Define the STL-style iterators.
186 // Only a const forward iterator is available.
187 // It makes it possible to iterate through all messages in the logger.
188 // <srcblock>
189 // LoggerHolder logger("log.name", False)
190 // for (LoggerHolder::const_iterator iter=arr.begin();
191 // iter!=arr.end(); iter++) {
192 // cout << iter.message() << endl;
193 // }
194 // </srcblock>
195 // <group name=STL-iterator>
196 // STL-style typedefs.
198 // Get the begin and end iterator object.
199 const_iterator begin() const;
200 const_iterator end() const;
201 // </group>
202
203
204private:
205 std::shared_ptr<LoggerHolderRep> itsRep;
206};
207
208
209
210
211// <summary>
212// Representation of the class holding a hierarchy of loggers.
213// </summary>
214
215// <use visibility=local>
216
217// <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
218// </reviewed>
219
220// <prerequisite>
221// <li> <linkto class="LogIO">LogIO</linkto> <li>
222// </prerequisite>
223
224// <synopsis>
225// The LoggerHolderRep class is the reference counted implementation
226// of <linkto class=LoggerHolder>LoggerHolder</linkto>.
227// See that class for more information.
228// </synopsis>
229
230// <motivation>
231// Reference counting was needed to be able to keep a LoggerHolder
232// object after the (ImageInterface) object containing it is gone.
233// </motivation>
234
235//# <todo asof="2001/06/14">
236//# </todo>
237
239{
240public:
241 // Create with a NullSink or MemoryLogSink (default).
243
244 // Create with a TableLogSink.
245 LoggerHolderRep (const String& logTableName, Bool isWritable);
246
247 // Copy constructor.
249
251
252 // Assignment.
253 // It removes the current parents.
255
256 // Add a logger from a parent.
257 void addParent (const LoggerHolder&);
258
259 // Append the entries of the other logger to this one.
260 void append (const LoggerHolder& other);
261
262 // Reopen a readonly logtable for read/write (if needed).
263 void reopenRW();
264
265 // Reopen the log table if needed (after a tempClose).
266 void reopen()
267 { if (itsIsClosed) doReopen(); }
268
269 // Temporarily close all log tables.
270 // By default the possible parent log tables are also closed.
271 void tempClose (Bool closeParents = True);
272
273 // Unlock the log table.
274 void unlock();
275
276 // Flush the log table.
277 void flush();
278
279 // Resync the log table (if needed).
280 void resync();
281
282 // Is the log table temporarily closed?
284 { return itsIsClosed; }
285
286 // Get access to the logger.
287 // It assumes that it will be used to post a message, so it reopens
288 // the log table for read/write if needed).
290
291 // Get access to the log sink (reopen the log table if needed).
292 // It is not assumed you want to write. If you want to do that,
293 // you should first call reopenRW() to ensure you can write.
295
296 // Clear the log.
297 // It removes the parents and removes all messages from the sink.
298 void clear();
299
300 // Remove all parents.
302
303 // Return the block of parents.
305 { return itsParents; }
306
307 // Define the STL-style iterators.
308 // Only a const forward iterator is available.
309 // It makes it possible to iterate through all messages in the logger.
310 // <srcblock>
311 // LoggerHolder logger("log.name", False)
312 // for (LoggerHolder::const_iterator iter=arr.begin();
313 // iter!=arr.end(); iter++) {
314 // cout << iter.message() << endl;
315 // }
316 // </srcblock>
317 // <group name=STL-iterator-rep>
318 // STL-style typedefs.
320 // Get the begin and end iterator object.
321 const_iterator begin() const;
323 // </group>
324
325
326private:
327 // Do the actual reopen.
328 void doReopen();
329
330
338};
339
340
341
342
343// <summary>
344// Class representing an entry in a LoggerHolder.
345// </summary>
346
347// <use visibility=local>
348
349// <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
350// </reviewed>
351
352// <prerequisite>
353// <li> <linkto class="LoggerHolder">LoggerHolder</linkto> <li>
354// </prerequisite>
355
356// <synopsis>
357// This class makes it possible to use the iterator in the STL-style.
358// It only contains a 'pointer' to the current entry in the current logger.
359// Function like <src>time()</src> can be used to retrieve the message parts.
360// </synopsis>
361
363{
364public:
367
368 LogHolderIterEntry (const LogSink* sink, uInt index)
369 : itsSink(sink), itsIndex(index) {}
370
373
376
378 { itsSink=that.itsSink; itsIndex=that.itsIndex; return *this; }
379
380 // Get the message parts.
381 // <group>
382 Double time() const
383 { return itsSink->getTime(itsIndex); }
385 { return itsSink->getMessage(itsIndex); }
387 { return itsSink->getPriority(itsIndex); }
389 { return itsSink->getLocation(itsIndex); }
391 { return itsSink->getObjectID(itsIndex); }
392 // </group>
393
394private:
397};
398
399
400
401
402// <summary>
403// Class doing the actual iteration through an LoggerHolder.
404// </summary>
405
406// <use visibility=local>
407
408// <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
409// </reviewed>
410
411// <prerequisite>
412// <li> <linkto class="LoggerHolder">LoggerHolder</linkto> <li>
413// </prerequisite>
414
415// <synopsis>
416// This class makes it possible to use the iterator in the STL-style.
417// It is used by
418//<linkto class=LoggerHolderIterator>LoggerHolderIterator</linkto>
419// which is the class as seen by the user.
420// LogHolderIter makes it easier to make the first entry available on
421// construction of an LoggerHolderIterator.
422// </synopsis>
423
425{
426public:
427 // Construct the iterator on the given LoggerHolderRep.
429
431
432 // Copy constructor is not needed, thus forbidden.
433 LogHolderIter (const LogHolderIter&) = delete;
434
435 // Assignment is not needed, thus forbidden.
437
438 // Increment to next message.
439 // Returns False if at the end.
441
442 // Get the entry.
444 { return itsEntry; }
445
446 const LoggerHolder& logger() const
447 { return *itsLogger; }
448
449private:
455};
456
457
458
459// <summary>
460// Class to iterate through an LoggerHolder.
461// </summary>
462
463// <use visibility=export>
464
465// <reviewed reviewer="" date="" tests="tLoggerHolder.cc" demos="">
466// </reviewed>
467
468// <prerequisite>
469// <li> <linkto class="LoggerHolder">LoggerHolder</linkto> <li>
470// </prerequisite>
471
472// <synopsis>
473// This class makes it possible to iterate in the STL-style through all
474// entries of an LoggerHolder object. If the logger has parent LoggerHolder
475// objects, it first iterates through all parents (recursively) and
476// finally through all entries in the LoggerHolder object itself.
477// </synopsis>
478
479// <example>
480// <srcblock>
481// LoggerHolder logger ("tLoggerHolder_tmp.log", True);
482// logger.logio() << "test1" << LogIO::POST;
483// logger.logio() << "test2" << LogIO::POST;
484// for (LoggerHolder::const_iterator iter = logger.begin();
485// iter != logger.end();
486// iter++) {
487// cout << iter->time() << ' ' << iter->message() << endl;
488// }
489// </srcblock>
490// </example>
491
493{
494public:
497
499
501
504
506
507 // Increment to next message.
508 // <group>
510 { next(); }
511 void operator++ (int)
512 { next(); }
513 // </group>
514
515 // Is the iterator not at the end yet?
518
519 // Get the entry.
520 // <group>
522 { return itsIter->getEntry(); }
524 { return &(itsIter->getEntry()); }
525 // </group>
526
527 const LoggerHolder& logger() const
528 { return itsIter->logger(); }
529
530private:
531 // Get the next entry (if available).
532 void next()
533 { itsNotAtEnd = itsIter->next(); }
534
535
538};
539
540
541
543{
544 itsRep->reopen();
545}
547{
548 return itsRep->isTempClosed();
549}
551{
552 return itsRep->logio();
553}
555{
556 return itsRep->sink();
557}
558inline const LogSink& LoggerHolder::sink() const
559{
560 return itsRep->sink();
561}
563{
564 return itsRep->parents();
565}
574
575
576
577
578} //# NAMESPACE CASACORE - END
579
580#endif
simple 1-D array
Definition Block.h:198
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.
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.
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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
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