NCBI C++ ToolKit
ncbi_message.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ncbi_message.cpp 85240 2019-01-28 16:27:18Z grichenk $
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  * Authors: Aleksey Grichenko
27  *
28  * File Description: IMessage/IMessageListener implementation
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbi_message.hpp>
34 #include <corelib/ncbithr.hpp>
35 #include <corelib/error_codes.hpp>
36 #include <stdlib.h>
37 #include <list>
38 
39 #define NCBI_USE_ERRCODE_X Corelib_Message
40 
42 
43 
45 {
46 }
47 
48 
51 {
52  m_Stack.push_front(SListenerNode(listener, flag));
53  return m_Stack.size();
54 }
55 
56 
58 {
59  size_t sz = m_Stack.size();
60  if (depth == 0) depth = sz;
61  if (m_Stack.empty() || sz < depth) {
62  // Nothing to pop.
64  "Unbalanced PushListener/PopListener calls: "
65  "listener index " << depth << " has been already removed");
66  return;
67  }
68  if (sz > depth) {
69  // Report lost listeners.
71  "Unbalanced PushListener/PopListener calls: "
72  "removing " << sz - depth << " lost listeners");
73  }
74  while (m_Stack.size() >= depth) {
75  m_Stack.pop_front();
76  }
77 }
78 
79 
81 {
82  return !m_Stack.empty();
83 }
84 
85 
88 {
91  if (ret == IMessageListener::eHandled &&
92  it->m_Flag == IMessageListener::eListen_Unhandled) continue;
93  if (it->m_Listener->PostMessage(message) == IMessageListener::eHandled) {
95  }
96  }
97  return ret;
98 }
99 
100 
103 {
106  if (ret == IMessageListener::eHandled &&
107  it->m_Flag == IMessageListener::eListen_Unhandled) continue;
108  if (it->m_Listener->PostProgress(progress) == IMessageListener::eHandled) {
110  }
111  }
112  return ret;
113 }
114 
115 
117 
118 
120 {
121  CMessageListener_Stack* ls = s_Listeners.GetValue();
122  if ( !ls ) {
123  ls = new CMessageListener_Stack;
124  s_Listeners.SetValue(ls, CTlsBase::DefaultCleanup<CMessageListener_Stack>);
125  }
126  _ASSERT(ls);
127  return *ls;
128 }
129 
131  EListenFlag flag)
132 {
134  return ls.PushListener(listener, flag);
135 }
136 
137 
139 {
141  return ls.PopListener(depth);
142 }
143 
144 
146 {
148 }
149 
150 
153 {
154  return s_GetListenerStack().Post(message);
155 }
156 
157 
160 {
161  return s_GetListenerStack().Post(progress);
162 }
163 
164 
166  EDiagSev sev,
167  int err_code,
168  int sub_code)
169  : m_Text(txt),
170  m_Severity(sev),
171  m_ErrCode(err_code),
172  m_SubCode(sub_code)
173 {
174 }
175 
176 
177 string CMessage_Basic::GetText(void) const
178 {
179  return m_Text;
180 }
181 
182 
184 {
185  return m_Severity;
186 }
187 
188 
189 int CMessage_Basic::GetCode(void) const
190 {
191  return m_ErrCode;
192 }
193 
194 
196 {
197  return m_SubCode;
198 }
199 
200 
202 {
203  return new CMessage_Basic(*this);
204 }
205 
206 
208 {
209  out << CNcbiDiag::SeverityName(GetSeverity()) << ": " << GetText() << endl;
210 }
211 
212 
213 string CMessage_Basic::Compose(void) const
214 {
216  Write(out);
218 }
219 
220 
222  Uint8 current,
223  Uint8 total)
224  : m_Text(txt),
225  m_Current(current),
226  m_Total(total)
227 {
228 }
229 
230 
232 {
233  return m_Text;
234 }
235 
236 
238 {
239  return m_Current;
240 }
241 
242 
244 {
245  return m_Total;
246 }
247 
248 
250 {
251  return new CProgressMessage_Basic(*this);
252 }
253 
254 
256 {
257  out << GetText() << " [" << m_Current << "/" << m_Total << "]" << endl;
258 }
259 
260 
262 {
264  Write(out);
266 }
267 
268 
271 {
272  m_Messages.push_back(AutoPtr<IMessage>(message.Clone()));
273  return eHandled;
274 }
275 
276 
279 {
280  ERR_POST(Note << progress);
281  return eHandled;
282 }
283 
284 
286 {
287  return *m_Messages[index].get();
288 }
289 
290 
292 {
293  return m_Messages.size();
294 }
295 
296 
298 {
299  m_Messages.clear();
300 }
301 
302 
AutoPtr –.
Definition: ncbimisc.hpp:401
void PopListener(size_t depth=0)
size_t PushListener(IMessageListener &listener, IMessageListener::EListenFlag flag)
list< SListenerNode > TListenerStack
IMessageListener::EPostResult Post(const IMessage &message)
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
Default IProgressMessage implementation.
IMessageListener::
IMessage::
IProgressMessage::
static unsigned char depth[2 *(256+1+29)+1]
std::ofstream out("events_result.xml")
main entry point for tests
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define ERR_POST_X_ONCE(err_subcode, message)
Error posting only once during program execution with default error code and given error subcode.
Definition: ncbidiag.hpp:621
static const char * SeverityName(EDiagSev sev)
Get a common symbolic name for the severity levels.
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
static bool HaveListeners(void)
Check if there are any listeners installed in the current thread.
EPostResult
Result of PostXXXX() operation.
virtual IMessage * Clone(void) const =0
Create a copy of the message.
virtual void Write(CNcbiOstream &out) const
Print the message and any additional information to the stream.
EDiagSev m_Severity
virtual string GetText(void) const
Get text message.
virtual size_t Count(void) const
Get total number of collected messages.
virtual string Compose(void) const
Get the whole composed message as string.
static EPostResult Post(const IMessage &message)
Post the message to listener(s), if any.
virtual EPostResult PostProgress(const IProgressMessage &progress)
Report progress.
virtual CProgressMessage_Basic * Clone(void) const
Create a copy of the message.
virtual Uint8 GetCurrent(void) const
Get current progress value (e.g. % or bytes written).
virtual int GetSubCode(void) const
Get error subcode. Zero = not set.
CMessage_Basic(const string &txt, EDiagSev sev, int err_code=0, int sub_code=0)
virtual const IMessage & GetMessage(size_t index) const
Get a previously collected message.
virtual void Write(CNcbiOstream &out) const
Print the message and any additional information to the stream.
virtual EDiagSev GetSeverity(void) const
Get message severity.
virtual IMessage * Clone(void) const
Create a copy of the message.
virtual string Compose(void) const
Get the whole composed message as string.
virtual void Clear(void)
Clear all collected messages.
virtual Uint8 GetTotal(void) const
Get total progress value (e.g. 100% or file size).
virtual string GetText(void) const
Get text message.
CProgressMessage_Basic(const string &txt, Uint8 current, Uint8 total)
EListenFlag
Which messages should be passed to the listener.
static size_t PushListener(IMessageListener &listener, EListenFlag flag=eListen_Unhandled)
Push a new listener to the stack in the current thread.
virtual int GetCode(void) const
Get error code. Zero = not set.
static void PopListener(size_t depth=0)
Remove listener(s) from the current thread's stack.
virtual EPostResult PostMessage(const IMessage &message)
Post new message to the listener.
@ eHandled
The message was successfully handled and will not be passed to other listeners installed with eListen...
@ eUnhandled
The message was not handled and should be passed to other listeners.
@ eListen_Unhandled
Default flag.
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 BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
Definition of all error codes used in corelib (xncbi.lib).
where both of them are integers Note
CMessageListener_Stack & s_GetListenerStack(void)
static CStaticTls< CMessageListener_Stack > s_Listeners
IMessage/IMessageListener interfaces and basic implementations.
Multi-threading – classes, functions, and features.
#define _ASSERT
Modified on Wed Dec 06 07:16:04 2023 by modify_doxy.py rev. 669887