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-2005 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 "babelconfig.h"
00024 
00025 #ifndef EXTERN
00026 #  define EXTERN extern
00027 #endif
00028 
00029 #if HAVE_IOSTREAM
00030 #include <iostream>
00031 #elif HAVE_IOSTREAM_H
00032 #include <iostream.h>
00033 #endif
00034 #if HAVE_SSTREAM
00035         #include <sstream>
00036 #elif
00037         #include <sstream.h>
00038 #endif
00039 #include <string>
00040 #include <vector>
00041 #include <deque>
00042 
00043 namespace OpenBabel
00044 {
00045 
00047 enum obMessageLevel {
00048    obError,     
00049    obWarning,   
00050    obInfo,      
00051    obAuditMsg,  
00052    obDebug      
00053 };
00054 
00057 class OBAPI OBError
00058 {
00059 public:
00060 
00062   OBError( const std::string &method = "",
00063            const std::string &errorMsg = "",
00064            const std::string &explanation = "",
00065            const std::string &possibleCause = "",
00066            const std::string &suggestedRemedy = "",
00067            const obMessageLevel = obDebug );
00068 
00070   std::string message(void) const;
00071   
00073   friend std::ostream& operator<< ( std::ostream &os, const OBError &er )
00074     { return os << er.message(); };
00075 
00076   std::string    GetMethod()            { return _method;          }
00077   std::string    GetError()             { return _errorMsg;        }
00078   std::string    GetExplanation()       { return _explanation;     }
00079   std::string    GetPossibleCause()     { return _possibleCause;   }
00080   std::string    GetSuggestedRemedy()   { return _suggestedRemedy; }
00081   obMessageLevel GetLevel()             { return _level;           }
00082 
00083  protected:
00084 
00085     std::string _method;
00086     std::string _errorMsg;
00087     std::string _explanation;
00088     std::string _possibleCause;
00089     std::string _suggestedRemedy;
00090 
00091     obMessageLevel _level;
00092 };
00093 
00095 class OBAPI OBMessageHandler
00096   {
00097   public:
00098     OBMessageHandler();
00099     ~OBMessageHandler();
00100     
00102     void ThrowError(OBError err);
00104     void ThrowError(const std::string &method, const std::string &errorMsg, 
00105                     obMessageLevel level = obDebug);
00106 
00108     std::vector<std::string> GetMessagesOfLevel(const obMessageLevel);
00109 
00111     void StartLogging() { _logging = true; }
00113     void StopLogging()  { _logging = false; }
00114 
00116     void SetMaxLogEntries(unsigned int max) { _maxEntries = max; }
00118     unsigned int GetMaxLogEntries() { return _maxEntries; }
00119 
00121     void ClearLog() { _messageList.clear(); }
00122 
00125     void SetOutputLevel(const obMessageLevel level) { _outputLevel = level; }
00127     obMessageLevel GetOutputLevel() { return _outputLevel; }
00128 
00129     void SetOutputStream(std::ostream *os) { _outputStream = os; }
00130     std::ostream* GetOutputStream() { return _outputStream; }
00131 
00133     bool StartErrorWrap();
00135     bool StopErrorWrap();
00136 
00137   protected:
00139     std::deque<OBError>    _messageList;
00140 
00142     obMessageLevel         _outputLevel;
00143 
00144     // self-explanatory
00145     std::ostream          *_outputStream;
00146 
00148     bool                   _logging;
00150     unsigned int           _maxEntries;
00151 
00153     std::streambuf        *_inWrapStreamBuf;
00155     std::streambuf        *_filterStreamBuf;
00156   }; 
00157 
00158 EXTERN OBMessageHandler obErrorLog;
00159 
00161  class OBAPI obLogBuf : public std::stringbuf
00162   {
00163     public:
00164       virtual ~obLogBuf() { sync(); }
00165     
00166     protected:
00168     int sync()
00169     {
00170       obErrorLog.ThrowError("", str(), obInfo);
00171       str(std::string()); // clear the buffer
00172                         return 0;
00173     }
00174   };
00175 
00176 } // end namespace OpenBabel
00177 
00178 #endif
00179