| 1 | /*! |
|---|
| 2 | * \file |
|---|
| 3 | * \brief Error handling functions - header file |
|---|
| 4 | * \author Tobias Ringstrom and Adam Piatyszek |
|---|
| 5 | * |
|---|
| 6 | * ------------------------------------------------------------------------- |
|---|
| 7 | * |
|---|
| 8 | * IT++ - C++ library of mathematical, signal processing, speech processing, |
|---|
| 9 | * and communications classes and functions |
|---|
| 10 | * |
|---|
| 11 | * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) |
|---|
| 12 | * |
|---|
| 13 | * This program is free software; you can redistribute it and/or modify |
|---|
| 14 | * it under the terms of the GNU General Public License as published by |
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 16 | * (at your option) any later version. |
|---|
| 17 | * |
|---|
| 18 | * This program is distributed in the hope that it will be useful, |
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 21 | * GNU General Public License for more details. |
|---|
| 22 | * |
|---|
| 23 | * You should have received a copy of the GNU General Public License |
|---|
| 24 | * along with this program; if not, write to the Free Software |
|---|
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 26 | * |
|---|
| 27 | * ------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #ifndef ITASSERT_H |
|---|
| 31 | #define ITASSERT_H |
|---|
| 32 | |
|---|
| 33 | #ifndef _MSC_VER |
|---|
| 34 | # include <itpp/config.h> |
|---|
| 35 | #else |
|---|
| 36 | # include <itpp/config_msvc.h> |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | #include <sstream> |
|---|
| 40 | #include <string> |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | namespace itpp { |
|---|
| 44 | |
|---|
| 45 | /*! |
|---|
| 46 | \addtogroup errorhandlingfunc |
|---|
| 47 | |
|---|
| 48 | For the following macros, the argument \c s is a string that is displayed. |
|---|
| 49 | |
|---|
| 50 | \code |
|---|
| 51 | it_assert(t,s); // Abort if t is not true |
|---|
| 52 | it_assert_debug(t,s); // Abort if t is not true and NDEBUG is not defined |
|---|
| 53 | it_error_if(t,s); // Abort if t is true |
|---|
| 54 | it_error(s); // Abort |
|---|
| 55 | it_info(s); // Display a message |
|---|
| 56 | it_info_debug(s); // Display a message if NDEBUG is not defined |
|---|
| 57 | it_info_no_endl(s); // Display a message without appended "std::endl" |
|---|
| 58 | it_info_no_endl_debug(s); // Display a message without appended "std::endl" if NDEBUG is not defined |
|---|
| 59 | it_warning(s); // Display a warning |
|---|
| 60 | \endcode |
|---|
| 61 | |
|---|
| 62 | \c it_assert(), \c it_error(), \c it_error_if(), \c it_info(), \c |
|---|
| 63 | it_info_no_endl() and \c it_warning() are always active, whereas \c |
|---|
| 64 | it_assert_debug(), \c it_info_debug() and \c it_info_no_endl_debug() |
|---|
| 65 | depends on the \c NDEBUG compile time definition. If \c NDEBUG is |
|---|
| 66 | defined, then none of these macros is executed. |
|---|
| 67 | |
|---|
| 68 | \note \c it_assert0() and \c it_assert1() macros are still defined for |
|---|
| 69 | backward compatibility, but \c it_assert_debug() should be used instead |
|---|
| 70 | of them. |
|---|
| 71 | */ |
|---|
| 72 | //!@{ |
|---|
| 73 | |
|---|
| 74 | //! Helper function for the \c it_assert and \c it_assert_debug macros |
|---|
| 75 | void it_assert_f(std::string ass, std::string msg, std::string file, int line); |
|---|
| 76 | //! Helper function for the \c it_error and \c it_error_if macros |
|---|
| 77 | void it_error_f(std::string msg, std::string file, int line); |
|---|
| 78 | //! Helper function for the \c it_info and \c it_info_debug macros |
|---|
| 79 | void it_info_f(std::string msg); |
|---|
| 80 | //! Helper function for the \c it_warning macro |
|---|
| 81 | void it_warning_f(std::string msg, std::string file, int line); |
|---|
| 82 | |
|---|
| 83 | //! Enable/disable using exceptions for error handling. |
|---|
| 84 | void it_enable_exceptions(bool on); |
|---|
| 85 | //! Enable warnings |
|---|
| 86 | void it_enable_warnings(); |
|---|
| 87 | //! Disable warnings |
|---|
| 88 | void it_disable_warnings(); |
|---|
| 89 | //! Redirect warnings to the ostream warn_stream |
|---|
| 90 | void it_redirect_warnings(std::ostream *warn_stream); |
|---|
| 91 | |
|---|
| 92 | //! Style of assert, error and warning messages. |
|---|
| 93 | enum error_msg_style { Full, Minimum }; |
|---|
| 94 | |
|---|
| 95 | //! Set preffered style of assert, error and warning messages |
|---|
| 96 | void it_error_msg_style(error_msg_style style); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | //! Abort if \c t is not true |
|---|
| 100 | #define it_assert(t,s) \ |
|---|
| 101 | if (!(t)) { \ |
|---|
| 102 | std::ostringstream m_sout; \ |
|---|
| 103 | m_sout << s; \ |
|---|
| 104 | itpp::it_assert_f(#t,m_sout.str(),__FILE__,__LINE__); \ |
|---|
| 105 | } else \ |
|---|
| 106 | ((void) 0) |
|---|
| 107 | |
|---|
| 108 | #if defined(NDEBUG) |
|---|
| 109 | //! Abort if \c t is not true and NDEBUG is not defined |
|---|
| 110 | # define it_assert_debug(t,s) ((void) 0) |
|---|
| 111 | #else |
|---|
| 112 | //! Abort if \c t is not true and NDEBUG is not defined |
|---|
| 113 | # define it_assert_debug(t,s) it_assert(t,s) |
|---|
| 114 | #endif // if defined(NDEBUG) |
|---|
| 115 | |
|---|
| 116 | //! Deprecated macro. Please use \c it_assert_debug() instead. |
|---|
| 117 | #define it_assert0(t,s) it_assert_debug(t,s) |
|---|
| 118 | //! Deprecated macro. Please use \c it_assert_debug() instead. |
|---|
| 119 | #define it_assert1(t,s) it_assert_debug(t,s) |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | //! Abort if \c t is true |
|---|
| 123 | #define it_error_if(t,s) \ |
|---|
| 124 | if((t)) { \ |
|---|
| 125 | std::ostringstream m_sout; \ |
|---|
| 126 | m_sout << s; \ |
|---|
| 127 | itpp::it_error_f(m_sout.str(),__FILE__,__LINE__); \ |
|---|
| 128 | } else \ |
|---|
| 129 | ((void) 0) |
|---|
| 130 | |
|---|
| 131 | //! Abort unconditionally |
|---|
| 132 | #define it_error(s) \ |
|---|
| 133 | if (true) { \ |
|---|
| 134 | std::ostringstream m_sout; \ |
|---|
| 135 | m_sout << s; \ |
|---|
| 136 | itpp::it_error_f(m_sout.str(),__FILE__,__LINE__); \ |
|---|
| 137 | } else \ |
|---|
| 138 | ((void) 0) |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | //! Print information message |
|---|
| 142 | #define it_info(s) \ |
|---|
| 143 | if (true) { \ |
|---|
| 144 | std::ostringstream m_sout; \ |
|---|
| 145 | m_sout << s << std::endl; \ |
|---|
| 146 | itpp::it_info_f(m_sout.str()); \ |
|---|
| 147 | } else \ |
|---|
| 148 | ((void) 0) |
|---|
| 149 | |
|---|
| 150 | //! Print information message withot \c std::endl at the end |
|---|
| 151 | #define it_info_no_endl(s) \ |
|---|
| 152 | if (true) { \ |
|---|
| 153 | std::ostringstream m_sout; \ |
|---|
| 154 | m_sout << s; \ |
|---|
| 155 | itpp::it_info_f(m_sout.str()); \ |
|---|
| 156 | } else \ |
|---|
| 157 | ((void) 0) |
|---|
| 158 | |
|---|
| 159 | #if defined(NDEBUG) |
|---|
| 160 | //! Print information message if NDEBUG is not defined |
|---|
| 161 | # define it_info_debug(s) ((void) 0) |
|---|
| 162 | /*! |
|---|
| 163 | \brief Print information message withot \c std::endl at the end if |
|---|
| 164 | NDEBUG is not defined |
|---|
| 165 | */ |
|---|
| 166 | # define it_info_no_endl_debug(s) ((void) 0) |
|---|
| 167 | #else |
|---|
| 168 | //! Print information message if NDEBUG is not defined |
|---|
| 169 | # define it_info_debug(s) it_info(s) |
|---|
| 170 | /*! |
|---|
| 171 | \brief Print information message withot \c std::endl at the end if |
|---|
| 172 | NDEBUG is not defined |
|---|
| 173 | */ |
|---|
| 174 | # define it_info_no_endl_debug(s) it_info_no_endl(s) |
|---|
| 175 | #endif // if defined(NDEBUG) |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | //! Display a warning message |
|---|
| 179 | #define it_warning(s) \ |
|---|
| 180 | if (true) { \ |
|---|
| 181 | std::ostringstream m_sout; \ |
|---|
| 182 | m_sout << s; \ |
|---|
| 183 | itpp::it_warning_f(m_sout.str(),__FILE__,__LINE__); \ |
|---|
| 184 | } else \ |
|---|
| 185 | ((void) 0) |
|---|
| 186 | |
|---|
| 187 | //!@} |
|---|
| 188 | |
|---|
| 189 | } // namespace itpp |
|---|
| 190 | |
|---|
| 191 | #endif // #ifndef ITASSERT_H |
|---|