COPASI API  4.16.103
CCopasiMessage.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 // CCopasiMessage
16 //
17 // New Class based on pmutils read functionality
18 // (C) Stefan Hoops 2001
19 
20 #include <string>
21 #include <iostream>
22 
23 #include <time.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 
27 #include "copasi.h"
28 #include "CCopasiMessage.h"
29 #include "messages.h"
30 #include "CCopasiException.h"
31 #include "utility.h"
32 
34 #include "commandline/COptions.h"
35 
36 #define INITIALTEXTSIZE 1024
37 
38 #ifdef WIN32
39 /**
40  * The stack of messages. Each message created with one of
41  * the specific constructors is automatically added to the stack.
42  */
43 static std::deque< CCopasiMessage > mMessageDeque;
44 #else
45 std::deque< CCopasiMessage > CCopasiMessage::mMessageDeque;
46 #endif
47 
48 // static
49 bool CCopasiMessage::IsGUI = false;
50 
52 {
53  if (mMessageDeque.empty())
55  MCCopasiMessage + 1);
56 
57  return mMessageDeque.front();
58 }
59 
61 {
62  if (mMessageDeque.empty())
64  MCCopasiMessage + 1);
65 
66  return mMessageDeque.back();
67 }
68 
70 {
71  if (mMessageDeque.empty())
73  MCCopasiMessage + 1);
74 
75  CCopasiMessage Message(mMessageDeque.front());
76  mMessageDeque.pop_front();
77 
78  return Message;
79 }
80 
82 {
83  if (mMessageDeque.empty())
85  MCCopasiMessage + 1);
86 
87  CCopasiMessage Message(mMessageDeque.back());
88  mMessageDeque.pop_back();
89 
90  return Message;
91 }
92 
93 std::string CCopasiMessage::getAllMessageText(const bool & chronological)
94 {
95  std::string Text = "";
96  CCopasiMessage(*getMessage)() = chronological ? getFirstMessage : getLastMessage;
97 
98  while (!mMessageDeque.empty())
99  {
100  if (Text != "") Text += "\n";
101 
102  Text += getMessage().getText();
103  }
104 
105  return Text;
106 }
107 
109 {
110  mMessageDeque.clear();
111  return;
112 }
113 
115 {
116  return mMessageDeque.size();
117 }
118 
120 {
121  CCopasiMessage::Type HighestSeverity = RAW;
122  std::deque< CCopasiMessage >::const_iterator it = mMessageDeque.begin();
123  std::deque< CCopasiMessage >::const_iterator end = mMessageDeque.end();
124 
125  for (; it != end; ++it)
126  if (it->getType() > HighestSeverity) HighestSeverity = it->getType();
127 
128  return HighestSeverity;
129 }
130 
131 bool CCopasiMessage::checkForMessage(const size_t & number)
132 {
133  std::deque< CCopasiMessage >::const_iterator it = mMessageDeque.begin();
134  std::deque< CCopasiMessage >::const_iterator end = mMessageDeque.end();
135 
136  for (; it != end; ++it)
137  if (it->getNumber() == number) return true;
138 
139  return false;
140 }
141 
142 // static
143 void CCopasiMessage::setIsGUI(const bool & isGUI)
144 {
145  IsGUI = isGUI;
146 }
147 
149  mText(),
150  mType(CCopasiMessage::RAW),
151  mNumber(0)
152 {}
153 
155  mText(src.mText),
156  mType(src.mType),
157  mNumber(src.mNumber)
158 {}
159 
161  const char *format, ...)
162 {
163  C_INT32 TextSize = INITIALTEXTSIZE;
164  C_INT32 Printed = 0;
165 
166  char *Text = NULL;
167  Text = new char[TextSize + 1];
168 
169  va_list Arguments; // = NULL;
170  va_start(Arguments, format);
171  Printed = vsnprintf(Text, TextSize, format, Arguments);
172  va_end(Arguments);
173 
174  while (Printed < 0 || TextSize < Printed)
175  {
176  delete [] Text;
177 
178  (Printed < 0) ? TextSize *= 2 : TextSize = Printed;
179  Text = new char[TextSize + 1];
180 
181  va_list Arguments; // = NULL;
182  va_start(Arguments, format);
183  Printed = vsnprintf(Text, TextSize, format, Arguments);
184  va_end(Arguments);
185  }
186 
187  mText = Text;
188  delete[] Text;
189  mType = type;
190  mNumber = 0;
191 
192  handler();
193 }
194 
196  size_t number, ...)
197 {
198  C_INT32 i = 0;
199 
200  C_INT32 TextSize = INITIALTEXTSIZE;
201  C_INT32 Printed = 0;
202 
203  while (Messages[i].No != number && Messages[i].Text)
204  i++;
205 
206  if (!Messages[i].Text)
207  fatalError();
208 
209  char *Text = NULL;
210  Text = new char[TextSize + 1];
211 
212  va_list Arguments; // = NULL;
213  va_start(Arguments, number);
214  Printed = vsnprintf(Text, TextSize, Messages[i].Text, Arguments);
215  va_end(Arguments);
216 
217  while (Printed < 0 || TextSize < Printed)
218  {
219  delete [] Text;
220 
221  (Printed < 0) ? TextSize *= 2 : TextSize = Printed;
222  Text = new char[TextSize + 1];
223 
224  va_list Arguments; // = NULL;
225  va_start(Arguments, number);
226  Printed = vsnprintf(Text, TextSize, Messages[i].Text, Arguments);
227  va_end(Arguments);
228  }
229 
230  mText = Text;
231  delete [] Text;
232 
233  mType = type;
234  mNumber = number;
235 
236  handler();
237 }
238 
239 void CCopasiMessage::handler(const bool & /* _throw */)
240 {
241  std::string Text = mText;
242 
243  switch (mType)
244  {
245  case RAW:
246  mText = "";
247  break;
248 
249  case TRACE:
250  mText = ">TRACE ";
251  mText += LocalTimeStamp();
252  mText += "<\n";
253  break;
254 
255  case WARNING:
256  mText = ">WARNING ";
257  mText += LocalTimeStamp();
258  mText += "<\n";
259  break;
260 
261  case COMMANDLINE:
262  if (IsGUI)
263  {
264  mText = ">WARNING ";
265  }
266  else
267  {
268  mText = ">ERROR ";
269  }
270 
271  mText += LocalTimeStamp();
272  mText += "<\n";
273  break;
274 
275  case ERROR:
276  mText = ">ERROR ";
277  mText += LocalTimeStamp();
278  mText += "<\n";
279  break;
280 
281  case EXCEPTION:
282  mText = ">EXCEPTION ";
283  mText += LocalTimeStamp();
284  mText += "<\n";
285  break;
286 
287  case RAW_FILTERED:
288  mText = ">RAW(filtered) ";
289  mText += LocalTimeStamp();
290  mText += "<\n";
291  break;
292 
293  case TRACE_FILTERED:
294  mText = ">TRACE(filtered) ";
295  mText += LocalTimeStamp();
296  mText += "<\n";
297  break;
298 
300  mText = ">COMMANDLINE(filtered) ";
301  mText += LocalTimeStamp();
302  mText += "<\n";
303  break;
304 
305  case WARNING_FILTERED:
306  mText = ">WARNING(filtered) ";
307  mText += LocalTimeStamp();
308  mText += "<\n";
309  break;
310 
311  case ERROR_FILTERED:
312  mText = ">ERROR(filtered) ";
313  mText += LocalTimeStamp();
314  mText += "<\n";
315  break;
316 
317  case EXCEPTION_FILTERED:
318  mText = ">EXCEPTION(filtered) ";
319  mText += LocalTimeStamp();
320  mText += "<\n";
321  break;
322  }
323 
324  mText += Text;
325 
326  if (mType != RAW) lineBreak();
327 
328  // Remove the message: No more messages.
329  if (mMessageDeque.size() == 1 &&
330  mMessageDeque.back().getNumber() == MCCopasiMessage + 1)
331  getLastMessage();
332 
333  mMessageDeque.push_back(*this);
334 
335  // All messages are printed to std::cerr
336  if (COptions::compareValue("Verbose", true) &&
337  mNumber != MCCopasiMessage + 1)
338  {
339  std::cerr << mText << std::endl;
340 
341 #ifdef COPASI_DEBUG_TRACE
342  DebugFile << mText << std::endl;
343 #endif // COPASI_DEBUG_TRACE
344  }
345 
346  if (mType == EXCEPTION)
347  throw CCopasiException(*this);
348 }
349 
350 // overload assignment operator
352 {
353  mText = RHS.mText;
354  mType = RHS.mType;
355  mNumber = RHS.mNumber;
356 
357  return *this;
358 }
360 const std::string & CCopasiMessage::getText(void) const {return mText;}
362 const size_t & CCopasiMessage::getNumber(void) const {return mNumber;}
363 
365 {
366  std::string Search("\n");
367  std::string Replace("\n ");
368  std::string::size_type pos = 0;
369 
370  while (true)
371  {
372  pos = mText.find(Search, pos);
373 
374  if (pos == std::string::npos)
375  break;
376 
377  mText.replace(pos, Search.length(), Replace);
378 
379  pos += Replace.length();
380  }
381 }
std::string LocalTimeStamp()
Definition: utility.cpp:53
static const CCopasiMessage & peekLastMessage()
static bool IsGUI
const std::string & getText() const
static std::string getAllMessageText(const bool &chronological=true)
#define fatalError()
const size_t & getNumber() const
static CCopasiMessage getFirstMessage()
static void setIsGUI(const bool &isGUI)
#define C_INT32
Definition: copasi.h:90
CCopasiMessage::Type mType
static void clearDeque()
#define MCCopasiMessage
void handler(const bool &_throw=false)
static std::deque< CCopasiMessage > mMessageDeque
static const CCopasiMessage & peekFirstMessage()
static bool compareValue(const std::string &name, const CType &value)
Definition: COptions.h:128
const CCopasiMessage::Type & getType() const
struct MESSAGES Message
static size_t size()
CCopasiMessage & operator=(const CCopasiMessage &RHS)
#define INITIALTEXTSIZE
std::string mText
static bool checkForMessage(const size_t &number)
static CCopasiMessage getLastMessage()
const MESSAGES Messages[]
Definition: messages.h:18
static CCopasiMessage::Type getHighestSeverity()