Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef OB_ERROR_H
00021 #define OB_ERROR_H
00022
00023 #include <openbabel/babelconfig.h>
00024
00025 #include <iosfwd>
00026 #include <sstream>
00027 #include <string>
00028 #include <vector>
00029 #include <deque>
00030
00031 #ifndef OBERROR
00032 #define OBERROR
00033 #endif
00034
00035 namespace OpenBabel
00036 {
00037
00039 enum obMessageLevel {
00040 obError,
00041 obWarning,
00042 obInfo,
00043 obAuditMsg,
00044 obDebug
00045 };
00046
00047 enum errorQualifier {always, onceOnly};
00048
00052 class OBERROR OBError
00053 {
00054 public:
00055
00057 OBError( const std::string &method = "",
00058 const std::string &errorMsg = "",
00059 const std::string &explanation = "",
00060 const std::string &possibleCause = "",
00061 const std::string &suggestedRemedy = "",
00062 const obMessageLevel = obDebug );
00063
00065 std::string message(void) const;
00066
00068 friend std::ostream& operator<< ( std::ostream &os, const OBError &er )
00069 { return os << er.message(); };
00070
00073 std::string GetMethod() const { return _method; }
00075 std::string GetError() const { return _errorMsg; }
00077 std::string GetExplanation() const { return _explanation; }
00079 std::string GetPossibleCause() const { return _possibleCause; }
00081 std::string GetSuggestedRemedy() const { return _suggestedRemedy; }
00083 obMessageLevel GetLevel() const { return _level; }
00084
00085 bool operator== (const OBError&)const;
00086
00087 protected:
00088
00090 std::string _method;
00092 std::string _errorMsg;
00094 std::string _explanation;
00096 std::string _possibleCause;
00098 std::string _suggestedRemedy;
00099
00101 obMessageLevel _level;
00102 };
00103
00105
00106 class OBERROR OBMessageHandler
00107 {
00108 protected:
00110 unsigned int _messageCount[5];
00111
00112 public:
00113 OBMessageHandler();
00114 ~OBMessageHandler();
00115
00117 void ThrowError(OBError err, errorQualifier qqualifier = always);
00119 void ThrowError(const std::string &method, const std::string &errorMsg,
00120 obMessageLevel level = obDebug, errorQualifier qualifier = always);
00121
00123 std::vector<std::string> GetMessagesOfLevel(const obMessageLevel);
00124
00126 void StartLogging() { _logging = true; }
00128 void StopLogging() { _logging = false; }
00129
00131 void SetMaxLogEntries(unsigned int max) { _maxEntries = max; }
00133 unsigned int GetMaxLogEntries() { return _maxEntries; }
00134
00136 void ClearLog() { _messageList.clear(); }
00137
00140 void SetOutputLevel(const obMessageLevel level) { _outputLevel = level; }
00142 obMessageLevel GetOutputLevel() { return _outputLevel; }
00143
00144 void SetOutputStream(std::ostream *os) { _outputStream = os; }
00145 std::ostream* GetOutputStream() { return _outputStream; }
00146
00148 bool StartErrorWrap();
00150 bool StopErrorWrap();
00151
00153 unsigned int GetErrorMessageCount() { return _messageCount[obError];}
00155 unsigned int GetWarningMessageCount() { return _messageCount[obWarning];}
00157 unsigned int GetInfoMessageCount() { return _messageCount[obInfo];}
00159 unsigned int GetAuditMessageCount() { return _messageCount[obAuditMsg];}
00161 unsigned int GetDebugMessageCount() { return _messageCount[obDebug];}
00163 std::string GetMessageSummary();
00164
00165 protected:
00167 std::deque<OBError> _messageList;
00168
00170 obMessageLevel _outputLevel;
00171
00172
00173 std::ostream *_outputStream;
00174
00176 bool _logging;
00178 unsigned int _maxEntries;
00179
00181 std::streambuf *_inWrapStreamBuf;
00183 std::streambuf *_filterStreamBuf;
00184 };
00185
00186 OBERROR extern OBMessageHandler obErrorLog;
00187
00190
00200 class OBERROR obLogBuf : public std::stringbuf
00201 {
00202 public:
00204 virtual ~obLogBuf() { sync(); }
00205
00206 protected:
00208 int sync()
00209 {
00210 obErrorLog.ThrowError("", str(), obInfo);
00211 str(std::string());
00212 return 0;
00213 }
00214 };
00215
00216 }
00217
00218 #endif
00219