Open Babel  3.0
oberror.h
Go to the documentation of this file.
1 /**********************************************************************
2 oberror.h - Handle error messages, warnings, notices, etc.
3 
4 Copyright (C) 2002 by Stefan Kebekus
5 Some portions Copyright (C) 2003-2006 by Geoffrey R. Hutchison
6 
7 This file is part of the Open Babel project.
8 For more information, see <http://openbabel.org/>
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation version 2 of the License.
13 
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18 ***********************************************************************/
19 
20 #ifndef OB_ERROR_H
21 #define OB_ERROR_H
22 
23 #include <openbabel/babelconfig.h>
24 
25 #include <iosfwd>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 #include <deque>
30 
31 #ifndef OBERROR
32 #define OBERROR
33 #endif
34 
35 namespace OpenBabel
36 {
37 
45  };
46 
48 
52  class OBERROR OBError
53  {
54  public:
55 
57  OBError( const std::string &method = "",
58  const std::string &errorMsg = "",
59  const std::string &explanation = "",
60  const std::string &possibleCause = "",
61  const std::string &suggestedRemedy = "",
62  const obMessageLevel = obDebug );
63 
65  std::string message(void) const;
66 
68  friend std::ostream& operator<< ( std::ostream &os, const OBError &er )
69  { return os << er.message(); };
70 
73  std::string GetMethod() const { return _method; }
75  std::string GetError() const { return _errorMsg; }
77  std::string GetExplanation() const { return _explanation; }
79  std::string GetPossibleCause() const { return _possibleCause; }
81  std::string GetSuggestedRemedy() const { return _suggestedRemedy; }
83  obMessageLevel GetLevel() const { return _level; }
84 
85  bool operator== (const OBError&)const;
86 
87  protected:
88 
90  std::string _method;
92  std::string _errorMsg;
94  std::string _explanation;
96  std::string _possibleCause;
98  std::string _suggestedRemedy;
99 
102  };
103 
105  // More documentation in oberror.cpp
106  class OBERROR OBMessageHandler
107  {
108  protected:
110  unsigned int _messageCount[5];
111 
112  public:
114  ~OBMessageHandler();
115 
117  void ThrowError(OBError err, errorQualifier qqualifier = always);
119  void ThrowError(const std::string &method, const std::string &errorMsg,
120  obMessageLevel level = obDebug, errorQualifier qualifier = always);
121 
123  std::vector<std::string> GetMessagesOfLevel(const obMessageLevel);
124 
126  void StartLogging() { _logging = true; }
128  void StopLogging() { _logging = false; }
129 
131  void SetMaxLogEntries(unsigned int max) { _maxEntries = max; }
133  unsigned int GetMaxLogEntries() { return _maxEntries; }
134 
136  void ClearLog() { _messageList.clear(); }
137 
140  void SetOutputLevel(const obMessageLevel level) { _outputLevel = level; }
142  obMessageLevel GetOutputLevel() { return _outputLevel; }
143 
144  void SetOutputStream(std::ostream *os) { _outputStream = os; }
145  std::ostream* GetOutputStream() { return _outputStream; }
146 
148  bool StartErrorWrap();
150  bool StopErrorWrap();
151 
153  unsigned int GetErrorMessageCount() { return _messageCount[obError];}
155  unsigned int GetWarningMessageCount() { return _messageCount[obWarning];}
157  unsigned int GetInfoMessageCount() { return _messageCount[obInfo];}
159  unsigned int GetAuditMessageCount() { return _messageCount[obAuditMsg];}
161  unsigned int GetDebugMessageCount() { return _messageCount[obDebug];}
163  std::string GetMessageSummary();
164 
165  protected:
167  std::deque<OBError> _messageList;
168 
171 
172  // self-explanatory
173  std::ostream *_outputStream;
174 
176  bool _logging;
178  unsigned int _maxEntries;
179 
181  std::streambuf *_inWrapStreamBuf;
183  std::streambuf *_filterStreamBuf;
184  };
185 
187  OBERROR extern OBMessageHandler obErrorLog;
188 
191 
201  class OBERROR obLogBuf : public std::stringbuf
202  {
203  public:
205  virtual ~obLogBuf() { sync(); }
206 
207  protected:
209  int sync()
210  {
211  obErrorLog.ThrowError("", str(), obInfo);
212  str(std::string()); // clear the buffer
213  return 0;
214  }
215  };
216 
217 } // end namespace OpenBabel
218 
219 #endif
220 
bool _logging
Whether messages will be logged into _messageList.
Definition: oberror.h:176
int sync()
Call OBMessageHandler::ThrowError() and flush the buffer.
Definition: oberror.h:209
std::string GetError() const
Definition: oberror.h:75
for critical errors (e.g., cannot read a file)
Definition: oberror.h:40
std::streambuf * _filterStreamBuf
The filtered obLogBuf stream buffer to wrap error messages.
Definition: oberror.h:183
unsigned int GetErrorMessageCount()
Definition: oberror.h:153
std::string _errorMsg
The actual error message.
Definition: oberror.h:92
obMessageLevel GetOutputLevel()
Definition: oberror.h:142
std::ostream * _outputStream
Definition: oberror.h:173
OBMessageHandler obErrorLog
Global OBMessageHandler error handler.
Definition: oberror.h:187
std::string message(void) const
Definition: oberror.cpp:46
Handle error messages, warnings, debugging information and the like.
Definition: oberror.h:106
unsigned int _maxEntries
The maximum size of _messageList log.
Definition: oberror.h:178
Customizable error handling and logging – store a message, including the method yielding the error...
Definition: oberror.h:52
std::deque< OBError > _messageList
Log of messages for later retrieval via GetMessagesOfLevel()
Definition: oberror.h:167
obMessageLevel
Levels of error and audit messages to allow filtering.
Definition: oberror.h:39
std::string GetPossibleCause() const
Definition: oberror.h:79
for non-critical problems (e.g., molecule appears empty)
Definition: oberror.h:41
Definition: oberror.h:47
void ThrowError(char *str)
Definition: obutil.cpp:55
for messages only useful for debugging purposes
Definition: oberror.h:44
obMessageLevel _outputLevel
Filtering level for messages and logging (messages of lower priority will be ignored.
Definition: oberror.h:170
std::string _explanation
Optional explanation message: more detailed than the brief error.
Definition: oberror.h:94
std::string GetExplanation() const
Definition: oberror.h:77
std::streambuf * _inWrapStreamBuf
The default stream buffer for the output stream (saved if wrapping is ued)
Definition: oberror.h:181
errorQualifier
Definition: oberror.h:47
void StartLogging()
Start logging messages (default)
Definition: oberror.h:126
for messages auditing methods which destroy or perceive molecular data (e.g., kekulization, atom typing, etc.)
Definition: oberror.h:43
unsigned int GetAuditMessageCount()
Definition: oberror.h:159
unsigned int GetDebugMessageCount()
Definition: oberror.h:161
std::ostream * GetOutputStream()
Definition: oberror.h:145
virtual ~obLogBuf()
Close the output buffer, flush, and call OBMessageHandler::ThrowError()
Definition: oberror.h:205
std::string GetSuggestedRemedy() const
Definition: oberror.h:81
unsigned int GetInfoMessageCount()
Definition: oberror.h:157
void SetOutputStream(std::ostream *os)
Definition: oberror.h:144
void StopLogging()
Stop logging messages completely.
Definition: oberror.h:128
void ClearLog()
Clear the current message log entirely.
Definition: oberror.h:136
void SetMaxLogEntries(unsigned int max)
Set the maximum number of entries (or 0 for no limit)
Definition: oberror.h:131
std::ostream & operator<<(std::ostream &, const vector3 &)
Prints a representation of the vector as a row vector of the form "<0.1,1,2>".
Definition: vector3.cpp:109
std::string GetMethod() const
Definition: oberror.h:73
unsigned int GetWarningMessageCount()
Definition: oberror.h:155
Definition: oberror.h:47
obMessageLevel GetLevel() const
Definition: oberror.h:83
std::string _method
The method causing the error (typically from the compiler macro FUNCTION)
Definition: oberror.h:90
std::string _possibleCause
Optional cause message.
Definition: oberror.h:96
for informative messages (e.g., file is a non-standard format)
Definition: oberror.h:42
A minimal streambuf derivative to wrap calls to cerr into calls to OBMessageHandler as needed...
Definition: oberror.h:201
unsigned int GetMaxLogEntries()
Definition: oberror.h:133
std::string _suggestedRemedy
Optional workaround or remedy.
Definition: oberror.h:98
void SetOutputLevel(const obMessageLevel level)
Set the level of messages to output (i.e., messages with at least this priority will be output) ...
Definition: oberror.h:140
obMessageLevel _level
The severity level: used for filtering via OBMessageHandler.
Definition: oberror.h:101
bool operator==(const OBBitVec &bv1, const OBBitVec &bv2)
Definition: bitvec.cpp:525
Global namespace for all Open Babel code.
Definition: alias.h:22