Loading...
Searching...
No Matches
oberror.h
Go to the documentation of this file.
/**********************************************************************
oberror.h - Handle error messages, warnings, notices, etc.
Copyright (C) 2002 by Stefan Kebekus
Some portions Copyright (C) 2003-2006 by Geoffrey R. Hutchison
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
***********************************************************************/
#ifndef OB_ERROR_H
#define OB_ERROR_H
#include <openbabel/babelconfig.h>
#include <iosfwd>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#ifndef OBERROR
#define OBERROR
#endif
namespace OpenBabel
{
enum obMessageLevel {
obError,
obWarning,
obInfo,
obAuditMsg,
obDebug
};
enum errorQualifier {always, onceOnly};
class OBERROR OBError
{
public:
OBError( const std::string &method = "",
const std::string &errorMsg = "",
const std::string &explanation = "",
const std::string &possibleCause = "",
const std::string &suggestedRemedy = "",
const obMessageLevel = obDebug );
std::string message(void) const;
friend std::ostream& operator<< ( std::ostream &os, const OBError &er )
{ return os << er.message(); };
std::string GetMethod() const { return _method; }
std::string GetError() const { return _errorMsg; }
std::string GetExplanation() const { return _explanation; }
std::string GetPossibleCause() const { return _possibleCause; }
std::string GetSuggestedRemedy() const { return _suggestedRemedy; }
obMessageLevel GetLevel() const { return _level; }
bool operator== (const OBError&)const;
protected:
std::string _method;
std::string _errorMsg;
std::string _explanation;
std::string _possibleCause;
std::string _suggestedRemedy;
obMessageLevel _level;
};
// More documentation in oberror.cpp
class OBERROR OBMessageHandler
{
protected:
unsigned int _messageCount[5];
public:
OBMessageHandler();
~OBMessageHandler();
void ThrowError(OBError err, errorQualifier qqualifier = always);
void ThrowError(const std::string &method, const std::string &errorMsg,
obMessageLevel level = obDebug, errorQualifier qualifier = always);
std::vector<std::string> GetMessagesOfLevel(const obMessageLevel);
void StartLogging() { _logging = true; }
void StopLogging() { _logging = false; }
void SetMaxLogEntries(unsigned int max) { _maxEntries = max; }
unsigned int GetMaxLogEntries() { return _maxEntries; }
void ClearLog() { _messageList.clear(); }
void SetOutputLevel(const obMessageLevel level) { _outputLevel = level; }
obMessageLevel GetOutputLevel() { return _outputLevel; }
void SetOutputStream(std::ostream *os) { _outputStream = os; }
std::ostream* GetOutputStream() { return _outputStream; }
bool StartErrorWrap();
bool StopErrorWrap();
unsigned int GetErrorMessageCount() { return _messageCount[obError];}
unsigned int GetWarningMessageCount() { return _messageCount[obWarning];}
unsigned int GetInfoMessageCount() { return _messageCount[obInfo];}
unsigned int GetAuditMessageCount() { return _messageCount[obAuditMsg];}
unsigned int GetDebugMessageCount() { return _messageCount[obDebug];}
std::string GetMessageSummary();
protected:
std::deque<OBError> _messageList;
obMessageLevel _outputLevel;
// self-explanatory
std::ostream *_outputStream;
bool _logging;
unsigned int _maxEntries;
std::streambuf *_inWrapStreamBuf;
std::streambuf *_filterStreamBuf;
};
OBERROR extern OBMessageHandler obErrorLog;
class OBERROR obLogBuf : public std::stringbuf
{
public:
virtual ~obLogBuf() { sync(); }
protected:
int sync()
{
obErrorLog.ThrowError("", str(), obInfo);
str(std::string()); // clear the buffer
return 0;
}
};
} // end namespace OpenBabel
#endif
OpenBabel::OBError
Customizable error handling and logging – store a message, including the method yielding the error,...
Definition oberror.h:53
OpenBabel::OBError::GetLevel
obMessageLevel GetLevel() const
Definition oberror.h:83
OpenBabel::OBError::_level
obMessageLevel _level
The severity level: used for filtering via OBMessageHandler.
Definition oberror.h:101
OpenBabel::OBError::GetExplanation
std::string GetExplanation() const
Definition oberror.h:77
OpenBabel::OBError::OBError
OBError(const std::string &method="", const std::string &errorMsg="", const std::string &explanation="", const std::string &possibleCause="", const std::string &suggestedRemedy="", const obMessageLevel=obDebug)
Constructor for an error message e.g. OBError(FUNCTION, " message ").
Definition oberror.cpp:35
OpenBabel::OBError::_explanation
std::string _explanation
Optional explanation message: more detailed than the brief error.
Definition oberror.h:94
OpenBabel::OBError::GetSuggestedRemedy
std::string GetSuggestedRemedy() const
Definition oberror.h:81
OpenBabel::OBError::GetPossibleCause
std::string GetPossibleCause() const
Definition oberror.h:79
OpenBabel::OBError::message
std::string message(void) const
Definition oberror.cpp:46
OpenBabel::OBError::_possibleCause
std::string _possibleCause
Optional cause message.
Definition oberror.h:96
OpenBabel::OBError::_suggestedRemedy
std::string _suggestedRemedy
Optional workaround or remedy.
Definition oberror.h:98
OpenBabel::OBError::_method
std::string _method
The method causing the error (typically from the compiler macro FUNCTION).
Definition oberror.h:90
OpenBabel::OBError::GetError
std::string GetError() const
Definition oberror.h:75
OpenBabel::OBError::_errorMsg
std::string _errorMsg
The actual error message.
Definition oberror.h:92
OpenBabel::OBError::GetMethod
std::string GetMethod() const
Definition oberror.h:73
OpenBabel::OBMessageHandler
Handle error messages, warnings, debugging information and the like.
Definition oberror.h:107
OpenBabel::OBMessageHandler::GetInfoMessageCount
unsigned int GetInfoMessageCount()
Definition oberror.h:157
OpenBabel::OBMessageHandler::StopLogging
void StopLogging()
Stop logging messages completely.
Definition oberror.h:128
OpenBabel::OBMessageHandler::_messageList
std::deque< OBError > _messageList
Log of messages for later retrieval via GetMessagesOfLevel().
Definition oberror.h:167
OpenBabel::OBMessageHandler::ThrowError
void ThrowError(OBError err, errorQualifier qqualifier=always)
Throw an error with an already-formatted OBError object.
Definition oberror.cpp:163
OpenBabel::OBMessageHandler::StartLogging
void StartLogging()
Start logging messages (default).
Definition oberror.h:126
OpenBabel::OBMessageHandler::_logging
bool _logging
Whether messages will be logged into _messageList.
Definition oberror.h:176
OpenBabel::OBMessageHandler::_inWrapStreamBuf
std::streambuf * _inWrapStreamBuf
The default stream buffer for the output stream (saved if wrapping is ued).
Definition oberror.h:181
OpenBabel::OBMessageHandler::GetOutputLevel
obMessageLevel GetOutputLevel()
Definition oberror.h:142
OpenBabel::OBMessageHandler::GetErrorMessageCount
unsigned int GetErrorMessageCount()
Definition oberror.h:153
OpenBabel::OBMessageHandler::GetAuditMessageCount
unsigned int GetAuditMessageCount()
Definition oberror.h:159
OpenBabel::OBMessageHandler::_messageCount
unsigned int _messageCount[5]
Count of messages at each message level.
Definition oberror.h:110
OpenBabel::OBMessageHandler::OBMessageHandler
OBMessageHandler()
Definition oberror.cpp:145
OpenBabel::OBMessageHandler::GetDebugMessageCount
unsigned int GetDebugMessageCount()
Definition oberror.h:161
OpenBabel::OBMessageHandler::SetMaxLogEntries
void SetMaxLogEntries(unsigned int max)
Set the maximum number of entries (or 0 for no limit).
Definition oberror.h:131
OpenBabel::OBMessageHandler::_maxEntries
unsigned int _maxEntries
The maximum size of _messageList log.
Definition oberror.h:178
OpenBabel::OBMessageHandler::GetOutputStream
std::ostream * GetOutputStream()
Definition oberror.h:145
OpenBabel::OBMessageHandler::SetOutputStream
void SetOutputStream(std::ostream *os)
Definition oberror.h:144
OpenBabel::OBMessageHandler::_filterStreamBuf
std::streambuf * _filterStreamBuf
The filtered obLogBuf stream buffer to wrap error messages.
Definition oberror.h:183
OpenBabel::OBMessageHandler::GetMessagesOfLevel
std::vector< std::string > GetMessagesOfLevel(const obMessageLevel)
Definition oberror.cpp:192
OpenBabel::OBMessageHandler::_outputLevel
obMessageLevel _outputLevel
Filtering level for messages and logging (messages of lower priority will be ignored.
Definition oberror.h:170
OpenBabel::OBMessageHandler::GetMaxLogEntries
unsigned int GetMaxLogEntries()
Definition oberror.h:133
OpenBabel::OBMessageHandler::GetWarningMessageCount
unsigned int GetWarningMessageCount()
Definition oberror.h:155
OpenBabel::OBMessageHandler::_outputStream
std::ostream * _outputStream
Definition oberror.h:173
OpenBabel::OBMessageHandler::SetOutputLevel
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
OpenBabel::OBMessageHandler::ClearLog
void ClearLog()
Clear the current message log entirely.
Definition oberror.h:136
OpenBabel::obLogBuf
A minimal streambuf derivative to wrap calls to cerr into calls to OBMessageHandler as needed.
Definition oberror.h:202
OpenBabel::obLogBuf::sync
int sync()
Call OBMessageHandler::ThrowError() and flush the buffer.
Definition oberror.h:209
OpenBabel::obLogBuf::~obLogBuf
virtual ~obLogBuf()
Close the output buffer, flush, and call OBMessageHandler::ThrowError().
Definition oberror.h:205
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
OpenBabel::obErrorLog
OBMessageHandler obErrorLog
Global OBMessageHandler error handler.
Definition oberror.h:187
OpenBabel::errorQualifier
errorQualifier
Definition oberror.h:47
OpenBabel::always
@ always
Definition oberror.h:47
OpenBabel::onceOnly
@ onceOnly
Definition oberror.h:47
OpenBabel::obMessageLevel
obMessageLevel
Levels of error and audit messages to allow filtering.
Definition oberror.h:39
OpenBabel::obWarning
@ obWarning
for non-critical problems (e.g., molecule appears empty)
Definition oberror.h:41
OpenBabel::obInfo
@ obInfo
for informative messages (e.g., file is a non-standard format)
Definition oberror.h:42
OpenBabel::obError
@ obError
for critical errors (e.g., cannot read a file)
Definition oberror.h:40
OpenBabel::obAuditMsg
@ obAuditMsg
for messages auditing methods which destroy or perceive molecular data (e.g., kekulization,...
Definition oberror.h:43
OpenBabel::obDebug
@ obDebug
for messages only useful for debugging purposes
Definition oberror.h:44