oberror.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 oberror.h - Handle error messages, warnings, notices, etc.
00003  
00004 Copyright (C) 2002 by Stefan Kebekus
00005 Some portions Copyright (C) 2003-2006 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful, but
00015 WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 General Public License for more details.
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 
00050   class OBERROR OBError
00051     {
00052     public:
00053 
00055       OBError( const std::string &method = "",
00056                const std::string &errorMsg = "",
00057                const std::string &explanation = "",
00058                const std::string &possibleCause = "",
00059                const std::string &suggestedRemedy = "",
00060                const obMessageLevel = obDebug );
00061 
00063       std::string message(void) const;
00064   
00066       friend std::ostream& ".">operator<< ( std::ostream &os, const OBError &er )
00067         { return os << er.message(); };
00068 
00071       std::string    GetMethod()            { return _method;          }
00073       std::string    GetError()             { return _errorMsg;        }
00075       std::string    GetExplanation()       { return _explanation;     }
00077       std::string    GetPossibleCause()     { return _possibleCause;   }
00079       std::string    GetSuggestedRemedy()   { return _suggestedRemedy; }
00081       obMessageLevel GetLevel()             { return _level;           }
00082 
00083     protected:
00084 
00086       std::string _method;
00088       std::string _errorMsg;
00090       std::string _explanation;
00092       std::string _possibleCause;
00094       std::string _suggestedRemedy;
00095 
00097       obMessageLevel _level;
00098     };
00099 
00101   // More documentation in oberror.cpp
00102   class OBERROR OBMessageHandler
00103     {
00104     protected:
00106       unsigned int           _messageCount[5];
00107 
00108     public:
00109       OBMessageHandler();
00110       ~OBMessageHandler();
00111     
00113       void ThrowError(OBError err);
00115       void ThrowError(const std::string &method, const std::string &errorMsg, 
00116                       obMessageLevel level = obDebug);
00117 
00119       std::vector<std::string> GetMessagesOfLevel(const obMessageLevel);
00120 
00122       void StartLogging() { _logging = true; }
00124       void StopLogging()  { _logging = false; }
00125 
00127       void SetMaxLogEntries(unsigned int max) { _maxEntries = max; }
00129       unsigned int GetMaxLogEntries() { return _maxEntries; }
00130 
00132       void ClearLog() { _messageList.clear(); }
00133 
00136       void SetOutputLevel(const obMessageLevel level) { _outputLevel = level; }
00138       obMessageLevel GetOutputLevel() { return _outputLevel; }
00139 
00140       void SetOutputStream(std::ostream *os) { _outputStream = os; }
00141       std::ostream* GetOutputStream() { return _outputStream; }
00142 
00144       bool StartErrorWrap();
00146       bool StopErrorWrap();
00147 
00149       unsigned int GetErrorMessageCount() { return _messageCount[obError];}
00151       unsigned int GetWarningMessageCount() { return _messageCount[obWarning];}
00153       unsigned int GetInfoMessageCount() { return _messageCount[obInfo];}
00155       unsigned int GetAuditMessageCount() { return _messageCount[obAuditMsg];}
00157       unsigned int GetDebugMessageCount() { return _messageCount[obDebug];}
00159       std::string GetMessageSummary();
00160 
00161     protected:
00163       std::deque<OBError>    _messageList;
00164 
00166       obMessageLevel         _outputLevel;
00167 
00168       // self-explanatory
00169       std::ostream          *_outputStream;
00170 
00172       bool                   _logging;
00174       unsigned int           _maxEntries;
00175 
00177       std::streambuf        *_inWrapStreamBuf;
00179       std::streambuf        *_filterStreamBuf;
00180     }; 
00181 
00182   OBERROR extern OBMessageHandler obErrorLog;
00183 
00186 
00196   class OBERROR obLogBuf : public std::stringbuf
00197     {
00198     public:
00200       virtual ~obLogBuf() { sync(); }
00201     
00202     protected:
00204       int sync()
00205         {
00206           obErrorLog.ThrowError("", str(), obInfo);
00207           str(std::string()); // clear the buffer
00208           return 0;
00209         }
00210     };
00211 
00212 } // end namespace OpenBabel
00213 
00214 #endif
00215