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 102637 2024-06-17 14:09:43Z foleyjp $
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 public:
50  ~ILineErrorListener() override {}
51 
52  /// Store error in the container, and
53  /// return true if error was stored fine, and
54  /// return false if the caller should terminate all further processing.
55  ///
56  virtual bool PutError(const ILineError&) = 0;
57 
58  bool PutMessage(const IObjtoolsMessage& message) override
59  {
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 // ============================================================================
108 {
109 public:
111  m_pProgressOstrm(nullptr) {}
112  ~CMessageListenerBase() override {}
113 
114 public:
115  size_t Count() const override { return m_Errors.size(); }
116 
117  size_t LevelCount(EDiagSev eSev) override
118  {
119  size_t uCount(0);
120  for (size_t u = 0; u < Count(); ++u) {
121  if (m_Errors[u]->GetSeverity() == eSev)
122  ++uCount;
123  }
124  return uCount;
125  }
126 
127  void ClearAll() override { m_Errors.clear(); }
128 
129  const ILineError& GetError(size_t uPos) const override
130  {
131  return *dynamic_cast<ILineError*>(m_Errors[uPos].get());
132  }
133 
134  virtual void Dump()
135  {
136  if (m_pProgressOstrm)
137  Dump(*m_pProgressOstrm);
138  }
139 
140  virtual void Dump(std::ostream& out)
141  {
142  if (m_Errors.size()) {
143  TLineErrVec::iterator it;
144  for (it = m_Errors.begin(); it != m_Errors.end(); ++it) {
145  (*it)->Dump(out);
146  out << endl;
147  }
148  } else {
149  out << "(( no errors ))" << endl;
150  }
151  }
152 
153  virtual void DumpAsXML(std::ostream& out)
154  {
155  if (m_Errors.size()) {
156  TLineErrVec::iterator it;
157  for (it = m_Errors.begin(); it != m_Errors.end(); ++it) {
158  (*it)->DumpAsXML(out);
159  out << endl;
160  }
161  } else {
162  out << "(( no errors ))" << endl;
163  }
164  }
165 
166  void PutProgress(
167  const string& sMessage,
168  const Uint8 iNumDone,
169  const Uint8 iNumTotal) override;
170 
171  /// This sets the stream to which progress messages are written.
172  ///
173  /// @param pProgressOstrm
174  /// The output stream for progress messages. Set this to NULL to
175  /// stop writing progress messages.
176  /// @param eNcbiOwnership
177  /// Indicates whether this CMessageListenerBase should own
178  /// pProgressOstrm.
179  virtual void SetProgressOstream(
180  CNcbiOstream* pProgressOstrm,
181  ENcbiOwnership eNcbiOwnership = eNoOwnership)
182  {
183  m_pProgressOstrm = pProgressOstrm;
184  if (eNcbiOwnership == eTakeOwnership && pProgressOstrm) {
185  m_progressOstrmDestroyer.reset(pProgressOstrm);
186  } else {
187  m_progressOstrmDestroyer.reset();
188  }
189  }
190 
191 private:
192  // private so later we can change the structure if
193  // necessary (e.g. to have indexing and such to speed up
194  // level-counting)
195  // typedef std::vector< unique_ptr<ILineError> > TLineErrVec;
196 
197  using TLineErrVec = vector<unique_ptr<IObjtoolsMessage>>;
199 
200 
201  // The stream to which progress messages are written.
202  // If NULL, progress messages are not written.
204 
205  // do not read this pointer. It's just used to make
206  // sure m_pProgressOstrm is destroyed if we own it.
207  unique_ptr<CNcbiOstream> m_progressOstrmDestroyer;
208 
209 protected:
210  // Child classes should use this to store errors
211  // into m_Errors
212  void StoreError(const ILineError& err)
213  {
214  m_Errors.emplace_back(err.Clone());
215  }
216 
217  void StoreMessage(const IObjtoolsMessage& message)
218  {
219  m_Errors.emplace_back(message.Clone());
220  }
221 };
222 
223 // ============================================================================
224 // Accept everything.
226 {
227 public:
230 
231  bool PutMessage(const IObjtoolsMessage& message) override
232  {
233  StoreMessage(message);
234  return true;
235  }
236 
237  bool PutError(const ILineError& err) override
238  {
239  return PutMessage(err);
240  }
241 };
242 
243 // ============================================================================
244 // Don't accept any errors, at all.
246 {
247 public:
250 
251  bool PutMessage(const IObjtoolsMessage& message) override
252  {
253  StoreMessage(message);
254  return false;
255  }
256 
257  bool PutError(const ILineError& err) override
258  {
259  return PutMessage(err);
260  }
261 };
262 
263 // ===========================================================================
264 // Accept up to <<count>> errors, any level.
266 {
267 public:
269  size_t uMaxCount) :
270  m_uMaxCount(uMaxCount) {}
272 
273  bool PutMessage(const IObjtoolsMessage& message) override
274  {
275  StoreMessage(message);
276  return (Count() < m_uMaxCount);
277  }
278 
279  bool PutError(const ILineError& err) override
280  {
281  return PutMessage(err);
282  }
283 
284 protected:
285  size_t m_uMaxCount;
286 };
287 
288 // ===========================================================================
289 // Accept evrything up to a certain level.
291 {
292 public:
293  CMessageListenerLevel(int iLevel) :
294  m_iAcceptLevel(iLevel) {}
296 
297  bool PutMessage(const IObjtoolsMessage& message) override
298  {
299  StoreMessage(message);
300  return (message.GetSeverity() <= m_iAcceptLevel);
301  }
302 
303  bool PutError(const ILineError& err) override
304  {
305  return PutMessage(err);
306  }
307 
308 protected:
310 };
311 
312 // ===========================================================================
313 // Accept everything, and besides storing all errors, post them.
315 {
316 public:
318  m_Info(info) {}
320 
321  bool PutError(const ILineError& err) override
322  {
324  << err.Message() << Endm;
325 
326  StoreError(err);
327  return true;
328  }
329 
330 private:
332 };
333 
334 
335 // ===========================================================================
337 {
338 public:
339  CGPipeMessageListener(bool ignoreBadModValue = false);
340 
341  bool PutError(const ILineError& err) override final;
342 
343 private:
345 };
346 
348 
350 
351 #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
Returns the number of errors seen so far at the given severity.
size_t Count() const override
void ClearAll() override
Clear all accumulated messages.
void StoreError(const ILineError &err)
virtual void DumpAsXML(std::ostream &out)
vector< unique_ptr< IObjtoolsMessage > > TLineErrVec
CNcbiOstream * m_pProgressOstrm
const ILineError & GetError(size_t uPos) const override
0-based error retrieval.
void StoreMessage(const IObjtoolsMessage &message)
unique_ptr< CNcbiOstream > m_progressOstrmDestroyer
CMessageListenerCount(size_t uMaxCount)
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
Store error in the container, and return true if error was stored fine, and return false if the calle...
bool PutError(const ILineError &err) override
Store error in the container, and return true if error was stored fine, and return false if the calle...
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
Store error in the container, and return true if error was stored fine, and return false if the calle...
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
Store error in the container, and return true if error was stored fine, and return false if the calle...
bool PutMessage(const IObjtoolsMessage &message) override
bool PutError(const ILineError &err) override
Store error in the container, and return true if error was stored fine, and return false if the calle...
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 ILineError * Clone(void) const =0
This is here because the copy constructor may be protected eventually.
virtual EDiagSev Severity(void) const
Definition: line_error.hpp:161
virtual string Message() const
Definition: line_error.cpp:110
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 Fri Sep 20 14:57:22 2024 by modify_doxy.py rev. 669887