NCBI C++ ToolKit
message_listener.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: message_listener.hpp 100091 2023-06-14 20:05:59Z stakhovv $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Frank Ludwig
27  *
28  * File Description:
29  * Classes for listening to errors, progress, etc.
30  *
31  */
32 
33 #ifndef OBJTOOLS_READERS___MESSAGELISTENER__HPP
34 #define OBJTOOLS_READERS___MESSAGELISTENER__HPP
35 
36 #include <corelib/ncbistd.hpp>
37 #include <corelib/ncbiobj.hpp>
38 #include <corelib/ncbi_message.hpp>
41 
43 
44 BEGIN_SCOPE(objects) // namespace ncbi::objects::
45 
46 // ============================================================================
48 // ============================================================================
49 {
50 public:
51  ~ILineErrorListener() override {}
52 
53  /// Store error in the container, and
54  /// return true if error was stored fine, and
55  /// return false if the caller should terminate all further processing.
56  ///
57  virtual bool PutError(const ILineError&) = 0;
58 
59  bool PutMessage(const IObjtoolsMessage& message) override {
60  const ILineError* le = dynamic_cast<const ILineError*>(&message);
61  if (! le)
62  return true;
63  return PutError(*le);
64  }
65 
66  // IListener::Get() implementation
67  virtual const ILineError& Get(size_t index) const
68  {
69  return this->GetError(index);
70  }
71 
72  /// 0-based error retrieval.
73  virtual const ILineError& GetError(size_t) const = 0;
74 
75  virtual size_t Count() const = 0;
76 
77  /// Returns the number of errors seen so far at the given severity.
78  virtual size_t LevelCount(EDiagSev) = 0;
79 
80  /// Clear all accumulated messages.
81  virtual void ClearAll() = 0;
82 
83  // IListener::Progress() implementation
84  virtual void Progress(const string& message,
85  Uint8 current,
86  Uint8 total) { PutProgress(message, current, total); }
87 
88  /// This is used for processing progress messages.
89  virtual void PutProgress(
90  const string& sMessage,
91  const Uint8 iNumDone = 0,
92  const Uint8 iNumTotal = 0) = 0;
93 
94  virtual const ILineError& GetMessage(size_t index) const
95  {
96  return Get(index);
97  }
98 
99  virtual void Clear()
100  {
101  ClearAll();
102  }
103 };
104 
105 
106 // Compatibility declaration, avoid using objects::IMessageListener -
107 // use ncbi::IMessageListener or objects::ILineErrorListener instead.
109 {
110 public:
111  ~IMessageListener() override {}
112 };
113 
114 
115 // ============================================================================
116 class NCBI_XOBJREAD_EXPORT CMessageListenerBase : public objects::IMessageListener
117 // ============================================================================
118 {
119 public:
120  CMessageListenerBase() : m_pProgressOstrm(nullptr) {}
121  ~CMessageListenerBase() override {}
122 
123 public:
124  size_t Count() const override { return m_Errors.size(); }
125 
126  size_t LevelCount(EDiagSev eSev) override
127  {
128  size_t uCount(0);
129  for (size_t u = 0; u < Count(); ++u) {
130  if (m_Errors[u]->GetSeverity() == eSev)
131  ++uCount;
132  }
133  return uCount;
134  }
135 
136  void ClearAll() override { m_Errors.clear(); }
137 
138  const ILineError& GetError(size_t uPos) const override
139  {
140  return *dynamic_cast<ILineError*>(m_Errors[uPos].get());
141  }
142 
143  virtual void Dump()
144  {
145  if (m_pProgressOstrm)
146  Dump(*m_pProgressOstrm);
147  }
148 
149  virtual void Dump(std::ostream& out)
150  {
151  if (m_Errors.size()) {
152  TLineErrVec::iterator it;
153  for (it = m_Errors.begin(); it != m_Errors.end(); ++it) {
154  (*it)->Dump(out);
155  out << endl;
156  }
157  } else {
158  out << "(( no errors ))" << endl;
159  }
160  }
161 
162  virtual void DumpAsXML(std::ostream& out)
163  {
164  if (m_Errors.size()) {
165  TLineErrVec::iterator it;
166  for (it = m_Errors.begin(); it != m_Errors.end(); ++it) {
167  (*it)->DumpAsXML(out);
168  out << endl;
169  }
170  } else {
171  out << "(( no errors ))" << endl;
172  }
173  }
174 
175  void PutProgress(
176  const string& sMessage,
177  const Uint8 iNumDone,
178  const Uint8 iNumTotal) override;
179 
180  /// This sets the stream to which progress messages are written.
181  ///
182  /// @param pProgressOstrm
183  /// The output stream for progress messages. Set this to NULL to
184  /// stop writing progress messages.
185  /// @param eNcbiOwnership
186  /// Indicates whether this CMessageListenerBase should own
187  /// pProgressOstrm.
188  virtual void SetProgressOstream(
189  CNcbiOstream* pProgressOstrm,
190  ENcbiOwnership eNcbiOwnership = eNoOwnership)
191  {
192  m_pProgressOstrm = pProgressOstrm;
193  if (eNcbiOwnership == eTakeOwnership && pProgressOstrm) {
194  m_progressOstrmDestroyer.reset(pProgressOstrm);
195  } else {
196  m_progressOstrmDestroyer.reset();
197  }
198  }
199 
200 private:
201  // private so later we can change the structure if
202  // necessary (e.g. to have indexing and such to speed up
203  // level-counting)
204  // typedef std::vector< AutoPtr<ILineError> > TLineErrVec;
205 
206  using TLineErrVec = vector<AutoPtr<IObjtoolsMessage>>;
208 
209 
210  // The stream to which progress messages are written.
211  // If NULL, progress messages are not written.
213 
214  // do not read this pointer. It's just used to make
215  // sure m_pProgressOstrm is destroyed if we own it.
217 
218 protected:
219  // Child classes should use this to store errors
220  // into m_Errors
221  void StoreError(const ILineError& err)
222  {
223  m_Errors.emplace_back(err.Clone());
224  }
225 
226  void StoreMessage(const IObjtoolsMessage& message)
227  {
228  m_Errors.emplace_back(dynamic_cast<IObjtoolsMessage*>(message.Clone()));
229  }
230 };
231 
232 // ============================================================================
234 //
235 // Accept everything.
236 // ============================================================================
237  public CMessageListenerBase
238 {
239 public:
242 
243  bool PutMessage(const IObjtoolsMessage& message) override
244  {
245  StoreMessage(message);
246  return true;
247  }
248 
249  bool PutError(const ILineError& err) override
250  {
251  return PutMessage(err);
252  }
253 };
254 
255 // ============================================================================
257 //
258 // Don't accept any errors, at all.
259 // ============================================================================
260  public CMessageListenerBase
261 {
262 public:
265 
266  bool PutMessage(const IObjtoolsMessage& message) override
267  {
268  StoreMessage(message);
269  return false;
270  }
271 
272  bool PutError(const ILineError& err) override
273  {
274  return PutMessage(err);
275  }
276 };
277 
278 // ===========================================================================
280 //
281 // Accept up to <<count>> errors, any level.
282 // ===========================================================================
283  public CMessageListenerBase
284 {
285 public:
287  size_t uMaxCount) :
288  m_uMaxCount(uMaxCount) {}
290 
291  bool PutMessage(const IObjtoolsMessage& message) override
292  {
293  StoreMessage(message);
294  return (Count() < m_uMaxCount);
295  }
296 
297  bool PutError(const ILineError& err) override
298  {
299  return PutMessage(err);
300  }
301 
302 protected:
303  size_t m_uMaxCount;
304 };
305 
306 // ===========================================================================
308 //
309 // Accept evrything up to a certain level.
310 // ===========================================================================
311  public CMessageListenerBase
312 {
313 public:
314  CMessageListenerLevel(int iLevel) :
315  m_iAcceptLevel(iLevel) {}
317 
318  bool PutMessage(const IObjtoolsMessage& message) override
319  {
320  StoreMessage(message);
321  return (message.GetSeverity() <= m_iAcceptLevel);
322  }
323 
324  bool PutError(const ILineError& err) override
325  {
326  return PutMessage(err);
327  }
328 
329 protected:
331 };
332 
333 // ===========================================================================
335 //
336 // Accept everything, and besides storing all errors, post them.
337 // ===========================================================================
338  public CMessageListenerBase
339 {
340 public:
342  m_Info(info) {}
344 
345  bool PutError(const ILineError& err) override
346  {
348  << err.Message() << Endm;
349 
350  StoreError(err);
351  return true;
352  }
353 
354 private:
356 };
357 
358 
359 // ===========================================================================
361 // ===========================================================================
362  public CMessageListenerBase
363 {
364 public:
365  CGPipeMessageListener(bool ignoreBadModValue=false);
366 
367  bool PutError(const ILineError& err) override final;
368 private:
370 };
371 
373 
375 
376 #endif // OBJTOOLS_READERS___MESSAGELISTENER__HPP
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
virtual void SetProgressOstream(CNcbiOstream *pProgressOstrm, ENcbiOwnership eNcbiOwnership=eNoOwnership)
This sets the stream to which progress messages are written.
virtual void Dump(std::ostream &out)
size_t LevelCount(EDiagSev eSev) override
AutoPtr< CNcbiOstream > m_progressOstrmDestroyer
size_t Count() const override
void ClearAll() override
void StoreError(const ILineError &err)
virtual void DumpAsXML(std::ostream &out)
vector< AutoPtr< IObjtoolsMessage > > TLineErrVec
CNcbiOstream * m_pProgressOstrm
const ILineError & GetError(size_t uPos) const override
void StoreMessage(const IObjtoolsMessage &message)
CMessageListenerCount(size_t uMaxCount)
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
bool PutError(const ILineError &err) override
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
CMessageListenerWithLog(const CDiagCompileInfo &info)
const CDiagCompileInfo m_Info
CNcbiDiag –.
Definition: ncbidiag.hpp:924
CObject –.
Definition: ncbiobj.hpp:180
virtual bool PutError(const ILineError &)=0
Store error in the container, and return true if error was stored fine, and return false if the calle...
virtual void ClearAll()=0
Clear all accumulated messages.
virtual const ILineError & GetMessage(size_t index) const
virtual size_t LevelCount(EDiagSev)=0
Returns the number of errors seen so far at the given severity.
virtual void Progress(const string &message, Uint8 current, Uint8 total)
virtual size_t Count() const =0
virtual const ILineError & Get(size_t index) const
~ILineErrorListener() override
virtual const ILineError & GetError(size_t) const =0
0-based error retrieval.
bool PutMessage(const IObjtoolsMessage &message) override
virtual void Clear()
virtual void PutProgress(const string &sMessage, const Uint8 iNumDone=0, const Uint8 iNumTotal=0)=0
This is used for processing progress messages.
virtual EDiagSev Severity(void) const
Definition: line_error.hpp:370
virtual ILineError * Clone(void) const
This is here because the copy constructor may be protected eventually.
Definition: line_error.hpp:111
virtual std::string Message(void) const
Definition: line_error.hpp:143
IMessageListener::
~IMessageListener() override
virtual EDiagSev GetSeverity(void) const =0
virtual IObjtoolsMessage * Clone(void) const =0
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
@ eTakeOwnership
An object can take ownership of another.
Definition: ncbi_types.h:136
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
const CNcbiDiag & GetRef(void) const
Some compilers (e.g.
Definition: ncbidiag.hpp:946
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
@ eDPF_Log
Print the posted message only; without severity, location, prefix, etc.
Definition: ncbidiag.hpp:747
@ eDPF_IsMessage
Definition: ncbidiag.hpp:758
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
ENcbiOwnership
Ownership relations between objects.
Definition: ncbi_types.h:134
#define NCBI_XOBJREAD_EXPORT
Definition: ncbi_export.h:1315
static MDB_envinfo info
Definition: mdb_load.c:37
const TYPE & Get(const CNamedParameterList *param)
IMessage/IMessageListener interfaces and basic implementations.
#define nullptr
Definition: ncbimisc.hpp:45
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
bool le(T x_, T y_, T round_)
Definition: njn_approx.hpp:84
void Dump(CSplitCacheApp *app, const C &obj, ESerialDataFormat format, const string &key, const string &suffix=kEmptyStr)
Modified on Wed Dec 06 07:14:00 2023 by modify_doxy.py rev. 669887