ICP  1
DAEstatus.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 #include "DAEstatus.h"
4 #include "icputils.h"
5 
7 {
10  m_report_func_arg = (void*)std::cerr;
11  m_report_copy_func = NULL;
13  m_report_immediately = false;
14  InitializeCriticalSection(&m_critical);
15 }
16 
18 {
19  DeleteCriticalSection(&m_critical);
20 }
21 
22 int DAEstatus::add(int facility, int severity, int errtype, const std::string& text)
23 {
24  return add(DAEstatus_message(severity, errtype, facility, text));
25 }
26 
28 {
30  m_status_list.push_back(mess);
31  int severity = mess.m_severity;
32  if (severity%2 == 0) // adding an error condition?
33  {
34  if (m_severity%2 == 0) // currently an error?
35  {
36  m_severity = (severity > m_severity ? severity : m_severity);
37  }
38  else
39  {
41  }
42  }
43  if (m_report_copy_func != NULL)
44  {
45  (*m_report_copy_func)(mess, m_report_copy_func_arg);
46  }
48  {
49  reportAll(false);
50  }
51  return 0;
52 }
53 
54 int DAEstatus::addVa(int facility, int severity, int errtype, const char* format, ... )
55 {
56  int stat;
57  va_list ap;
58  va_start(ap, format);
59  stat = addVaMain(facility, severity, errtype, format, ap);
60  va_end(ap);
61  return stat;
62 }
63 
64 int DAEstatus::addVaMain(int facility, int severity, int errtype, const char* format, va_list ap)
65 {
66  static const int maxlen = 10000;
67  char* buffer;
68  int stat, len;
69  len = _vscprintf(format, ap);
70  if ( (len >= 0) && (len < maxlen) )
71  {
72  buffer = new char[len+1]; // + 1 for NULL
73  vsprintf(buffer, format, ap);
74  buffer[len] = '\0'; // just in case
75  stat = add(facility, severity, errtype, buffer);
76  delete []buffer;
77  }
78  else
79  {
80  stat = add(facility, severity, errtype, "<invalid vararg error message - probably too long in DAEstatus::addVaMain>");
81  }
82  return stat;
83 }
84 
85 
86 int DAEstatus::addInfo(int facility, const std::string& text)
87 {
88  return add(facility, SEV_INFO, ERRTYPE_NONE, text);
89 }
90 
91 int DAEstatus::addInfoVa(int facility, const char* format, ... )
92 {
93  int stat;
94  va_list ap;
95  va_start(ap, format);
96  stat = addVaMain(facility, SEV_INFO, ERRTYPE_NONE, format, ap);
97  va_end(ap);
98  return stat;
99 }
100 
101 int DAEstatus::addWarning(int facility, const std::string& text)
102 {
103  return add(facility, SEV_WARNING, ERRTYPE_NONE, text);
104 }
105 
106 int DAEstatus::addWarningVa(int facility, const char* format, ... )
107 {
108  int stat;
109  va_list ap;
110  va_start(ap, format);
111  stat = addVaMain(facility, SEV_WARNING, ERRTYPE_NONE, format, ap);
112  va_end(ap);
113  return stat;
114 }
115 
116 int DAEstatus::addDebug(int facility, const std::string& text)
117 {
118  return add(facility, SEV_DEBUG, ERRTYPE_NONE, text);
119 }
120 
121 int DAEstatus::addDebugVa(int facility, const char* format, ... )
122 {
123  int stat;
124  va_list ap;
125  va_start(ap, format);
126  stat = addVaMain(facility, SEV_DEBUG, ERRTYPE_NONE, format, ap);
127  va_end(ap);
128  return stat;
129 }
130 
131 int DAEstatus::add(DAEstatus& dstatus, bool clear)
132 {
133  status_list_t::iterator iter;
134  ICPCritical cs(&m_critical);
135  for(iter = dstatus.m_status_list.begin(); iter != dstatus.m_status_list.end(); iter++)
136  {
137  add(*iter);
138  }
139  cs.unlock();
140  if (clear)
141  {
142  dstatus.clearAll();
143  }
144  return DAEstatus::Success;
145 }
146 
148 {
149  m_severity = SEV_OK;
150  status_list_t::iterator iter;
151  ICPCritical cs(&m_critical);
152  for(iter = m_status_list.begin(); iter != m_status_list.end(); ++iter)
153  {
154  if ( *iter >= m_severity )
155  {
156  m_severity = iter->m_severity;
157  }
158  }
159  return 0;
160 }
161 
162 bool DAEstatus::check(int severity, int direction) const
163 {
164  return DAEstatus_message::compare(m_severity, severity, direction);
165 }
166 
168 {
169  int sev;
170  status_list_t::iterator iter;
171  ICPCritical cs(&m_critical);
172  m_severity = SEV_OK;
173  for(iter = m_status_list.begin(); iter != m_status_list.end(); ++iter)
174  {
175  sev = iter->resetSeverityToAtMost(severity);
177  {
178  m_severity = sev;
179  }
180  }
181  return 0;
182 }
183 
185 {
186  int sev;
187  status_list_t::iterator iter;
188  ICPCritical cs(&m_critical);
189  m_severity = SEV_OK;
190  for(iter = m_status_list.begin(); iter != m_status_list.end(); ++iter)
191  {
192  sev = iter->resetSeverityToAtLeast(severity);
194  {
195  m_severity = sev;
196  }
197  }
198  return 0;
199 }
200 
201 int DAEstatus::clear(int severity, int direction, bool reset_overall_severity)
202 {
203  status_list_t::iterator iter;
204  ICPCritical cs(&m_critical);
205  iter = m_status_list.begin();
206  while(iter != m_status_list.end())
207  {
208  if (iter->check(severity, direction))
209  {
210  iter = m_status_list.erase(iter);
211  }
212  else
213  {
214  iter++;
215  }
216  }
217  if (reset_overall_severity)
218  {
220  }
221  return 0;
222 }
223 
224 int DAEstatus::report(int severity, int direction, bool reset_overall_severity, bool remove_messages)
225 {
226  return report(m_report_func, m_report_func_arg, severity, direction, reset_overall_severity, remove_messages);
227 }
228 
229 int DAEstatus::report(DAEreport_func_t* report_func, void* report_func_arg, int severity, int direction, bool reset_overall_severity, bool remove_messages)
230 {
231  status_list_t::iterator iter;
232  ICPCritical cs(&m_critical);
233  iter = m_status_list.begin();
234  while(iter != m_status_list.end())
235  {
236  if (iter->check(severity, direction))
237  {
238  (*report_func)(*iter, report_func_arg);
239  if (remove_messages)
240  {
241  iter = m_status_list.erase(iter);
242  }
243  else
244  {
245  iter++;
246  }
247  }
248  else
249  {
250  iter++;
251  }
252  }
253  if (reset_overall_severity)
254  {
256  }
257  return 0;
258 }
259 
260 int DAEstatus::report(std::ostream& str, int severity, int direction, bool reset_overall_severity, bool remove_messages)
261 {
262  return report(&ostreamReportFunction, &str, severity, direction, reset_overall_severity, remove_messages);
263 }
264 
265 std::ostream& DAEstatus::print(std::ostream& str)
266 {
267  reportAll(str, false, false);
268  return str;
269 }
270 
271 // DAEstatus_message bits
272 
273 std::ostream& DAEstatus_message::print(std::ostream& str, bool format_with_T, bool add_cr) const
274 {
275  Poco::StringTokenizer tokenizer(this->str(), "\r\n", Poco::StringTokenizer::TOK_IGNORE_EMPTY /*|Poco::StringTokenizer::TOK_TRIM*/ );
276  for (Poco::StringTokenizer::Iterator it = tokenizer.begin(); it != tokenizer.end(); ++it)
277  {
278  if (it->empty())
279  {
280  continue;
281  }
282  if ( it != tokenizer.begin() )
283  {
284  str << (add_cr ? "\r\n" : "\n");
285  }
286  str << timeStr(format_with_T) << " " << *it;
287  }
288  return str;
289 }
290 
291 DAEstatus_message::DAEstatus_message(int severity, int errtype,
292  int facility, const std::string& text) :
293  m_severity(severity), m_errtype(errtype),
294  m_facility(facility), m_text(text), m_time(0)
295 {
296  time(&m_time);
297  if ( !m_text.empty() && (m_text.back() == '\n') )
298  {
299  m_text.pop_back();
300  }
301 }
302 
303  bool DAEstatus_message::compareEQ(int left_sev, int right_sev)
304  {
305  return left_sev == right_sev;
306  }
307 
308  // left_sev <= right_sev i.e. error of right_sev or less return true
309  bool DAEstatus_message::compareLE(int left_sev, int right_sev)
310  {
311  if ( (left_sev%2 == 0) && (right_sev %2 == 0) ) // both errors
312  {
313  return left_sev <= right_sev;
314  }
315  else if (left_sev%2 == 0) // left error, right not
316  {
317  return false;
318  }
319  else
320  {
321  return true;
322  }
323  }
324 
325  // left_sev >= right_sev i.e. error of right_sev or more return true
326  bool DAEstatus_message::compareGE(int left_sev, int right_sev)
327  {
328  if ( (left_sev%2 == 0) && (right_sev %2 == 0) ) // both errors
329  {
330  return left_sev >= right_sev;
331  }
332  else if (left_sev%2 == 0) // left error, right not
333  {
334  return true;
335  }
336  else
337  {
338  return false;
339  }
340  }
341 
342 
343 const int DAEstatus::Success = 0;
344 const int DAEstatus::Failure = 1;
bool check(int severity, int direction) const
Definition: DAEstatus.cpp:162
int addVa(int facility, int severity, int errtype, const char *format,...)
Definition: DAEstatus.cpp:54
#define SEV_WARNING
int clearAll(bool reset_overall_severity=true)
Definition: DAEstatus.h:165
int report(int severity, int direction, bool reset_overall_severity=true, bool remove_messages=true)
Definition: DAEstatus.cpp:224
static bool compareEQ(int left_sev, int right_sev)
Definition: DAEstatus.cpp:303
int DAEreport_func_t(const DAEstatus_message &mess, void *arg)
Definition: DAEstatus.h:119
#define SEV_OK
static const int Failure
Definition: DAEstatus.h:141
int add(DAEstatus &dstatus, bool clear)
Definition: DAEstatus.cpp:131
int addDebug(int facility, const std::string &text)
Definition: DAEstatus.cpp:116
static const int Success
Definition: DAEstatus.h:140
static bool compareLE(int left_sev, int right_sev)
Definition: DAEstatus.cpp:309
DAEreport_func_t * m_report_func
Definition: DAEstatus.h:127
std::string str() const
Definition: DAEstatus.h:82
#define ERRTYPE_NONE
int reportAll(bool reset_overall_severity=true, bool remove_messages=true)
Definition: DAEstatus.h:169
void * m_report_copy_func_arg
Definition: DAEstatus.h:130
int clear(int severity, int direction, bool reset_overall_severity=true)
Definition: DAEstatus.cpp:201
std::ostream & print(std::ostream &str, bool format_with_T=false, bool add_cr=false) const
Definition: DAEstatus.cpp:273
int m_severity
Definition: DAEstatus.h:126
#define SEV_INFO
int addVaMain(int facility, int severity, int errtype, const char *format, va_list ap)
Definition: DAEstatus.cpp:64
#define SEV_DEBUG
int addDebugVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:121
int addWarning(int facility, const std::string &text)
Definition: DAEstatus.cpp:101
int resetSeverityToAtLeast(int severity)
Definition: DAEstatus.cpp:184
status_list_t m_status_list
Definition: DAEstatus.h:125
int addWarningVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:106
int recalculateSeverity()
Definition: DAEstatus.cpp:147
void unlock()
Definition: icputils.h:354
void * m_report_func_arg
Definition: DAEstatus.h:128
int severity()
Definition: DAEstatus.h:142
std::ostream & print(std::ostream &str)
Definition: DAEstatus.cpp:265
static bool compareGE(int left_sev, int right_sev)
Definition: DAEstatus.cpp:326
static bool compare(int left_sev, int right_sev, int direction)
Definition: DAEstatus.h:88
CRITICAL_SECTION m_critical
Definition: DAEstatus.h:132
int addInfo(int facility, const std::string &text)
Definition: DAEstatus.cpp:86
static int ostreamReportFunction(const DAEstatus_message &mess, void *arg)
Definition: DAEstatus.h:192
int resetSeverityToAtMost(int severity)
Definition: DAEstatus.cpp:167
bool m_report_immediately
Definition: DAEstatus.h:131
std::string timeStr(bool format_with_T) const
Definition: DAEstatus.h:75
std::string m_text
Definition: DAEstatus.h:15
int addInfoVa(int facility, const char *format,...)
Definition: DAEstatus.cpp:91
DAEreport_func_t * m_report_copy_func
Definition: DAEstatus.h:129