PLearn 0.1
plerror.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 // Authors: Pascal Vincent & Yoshua Bengio
00037 
00038 #define PL_LOG_MODULE_NAME "plerror"
00039 
00040 #include <cstdarg>
00041 //#include <cstdlib>
00042 #include <iostream>
00043 
00044 #include "plerror.h"
00045 #include <plearn/io/pl_log.h>
00046 #include <plearn/io/PPath.h>
00047 
00048 #if USING_MPI
00049 #include <plearn/sys/PLMPI.h>
00050 
00051 #endif //USING_MPI
00052 
00053 
00054 //extern ofstream debug_stream;
00055 
00056 namespace PLearn {
00057 using namespace std;
00058 
00059 ostream* error_stream = &cerr;
00060 
00061 #define ERROR_MSG_SIZE 4096
00062 #ifndef USER_SUPPLIED_ERROR
00063 void errormsg2(const char* filename,const int linenumber,const char* msg, ...){
00064     va_list args;
00065     va_start(args,msg);
00066     char message[ERROR_MSG_SIZE];
00067     
00068     snprintf(message, ERROR_MSG_SIZE, "In file: \"%s\" at line %d\n",
00069             PPath(filename).basename().c_str(), linenumber);
00070     PLASSERT(ERROR_MSG_SIZE>=strlen(message)+strlen(msg));
00071     strncat(message,msg,ERROR_MSG_SIZE);
00072     verrormsg(message, args);
00073 
00074     va_end(args);
00075 
00076 }
00077 
00078 void errormsg(const char* msg, ...)
00079 {
00080     va_list args;
00081     va_start(args,msg);
00082     verrormsg(msg, args);
00083     va_end(args);
00084 }
00085 
00086 void verrormsg(const char* msg, va_list args)
00087 {
00088     char message[ERROR_MSG_SIZE];
00089 
00090 #if !defined(ULTRIX) && !defined(_MINGW_) && !defined(WIN32)
00091     vsnprintf(message, ERROR_MSG_SIZE,msg,args);
00092 #else
00093     vsprintf(message,msg,args);
00094 #endif
00095     //output to module log: can be useful when throwing exceptions
00096     DBG_MODULE_LOG << endl << "-------------- PLERROR:" << message << endl << "--------------" << endl;
00097 #ifndef USE_EXCEPTIONS
00098 #if USING_MPI
00099     *error_stream <<" ERROR from rank=" << PLMPI::rank << ": " <<message<<endl;
00100 #else //USING_MPI
00101     *error_stream <<" ERROR: "<<message<<endl;
00102 #endif //USING_MPI
00103     exit(1);
00104 #else
00105 // Commented out as one error message seems to be enough.
00106 //  IMP_LOG << "Throwing PLearnError exception: " << message << endl;
00107     throw PLearnError(message);                
00108 #endif
00109 }
00110 #endif
00111 
00112 
00113 void warningmsg(const char* msg, ...)
00114 {
00115     va_list args;
00116     va_start(args,msg);
00117     vwarningmsg(msg, args);
00118     va_end(args);
00119 }
00120 
00121 void vwarningmsg(const char* msg, va_list args)
00122 {
00123     char message[ERROR_MSG_SIZE];
00124 
00125 #if !defined(ULTRIX) && !defined(_MINGW_) && !defined(WIN32)
00126     vsnprintf(message,ERROR_MSG_SIZE,msg,args);
00127 #else
00128     vsprintf(message,msg,args);
00129 #endif
00130 
00131     // *error_stream <<" WARNING: "<<message<<endl;
00132     NORMAL_LOG << " WARNING: " << message << endl;
00133 }
00134 
00135 void warn_err(bool warn, const char* msg, ...)
00136 {
00137     va_list args;
00138     va_start(args,msg);
00139     if(warn) vwarningmsg(msg,args);
00140     else verrormsg(msg, args);
00141     va_end(args);
00142 }
00143 
00144 void warn_err2(const char* filename, const int linenumber, bool warn, const char* msg,...)
00145 {
00146     va_list args;
00147     va_start(args,msg);
00148 
00149     if(warn) vwarningmsg(msg,args);
00150     else{
00151         char message[ERROR_MSG_SIZE];
00152     
00153         snprintf(message, ERROR_MSG_SIZE, "In file: \"%s\" at line %d\n",
00154                  PPath(filename).basename().c_str(), linenumber);
00155         PLASSERT(ERROR_MSG_SIZE>=strlen(message)+strlen(msg));
00156         strncat(message,msg,ERROR_MSG_SIZE);
00157         verrormsg(message, args);
00158     }
00159     va_end(args);
00160 }
00161 
00162 void  deprecationmsg(const char* msg, ...)
00163 {
00164     va_list args;
00165     va_start(args,msg);
00166     char message[ERROR_MSG_SIZE];
00167 
00168 #if !defined(ULTRIX) && !defined(_MINGW_) && !defined(WIN32)
00169     vsnprintf(message,ERROR_MSG_SIZE,msg,args);
00170 #else
00171     vsprintf(message,msg,args);
00172 #endif
00173 
00174     va_end(args);
00175 
00176     // *error_stream <<" DEPRECATION_WARNING: "<<message<<endl;
00177     NORMAL_LOG << " DEPRECATION_WARNING: " << message << endl;
00178 }
00179 
00180 void exitmsg(const char* msg, ...)
00181 {
00182     va_list args;
00183     va_start(args,msg);
00184     char message[ERROR_MSG_SIZE];
00185 
00186 #if !defined(ULTRIX) && !defined(_MINGW_) && !defined(WIN32)
00187     vsnprintf(message,ERROR_MSG_SIZE,msg,args);
00188 #else
00189     vsprintf(message,msg,args);
00190 #endif
00191 
00192     va_end(args);
00193 
00194     *error_stream << message << endl;
00195     exit(1);
00196 }
00197 
00199 string get_error_message(const char* type, const char* expr,
00200         const char* function, const char* file, unsigned line,
00201         const string& message)
00202 {
00203     // Allocate buffer.
00204     size_t size = strlen(type) + strlen(expr) + strlen(function) + strlen(file)
00205                     + message.size() + 150;
00206     char* msg = new char[size];
00207     // Format string.
00208     snprintf(msg, size, 
00209             "%s failed: %s\n"
00210             "Function: %s\n"
00211             "    File: %s\n"
00212             "    Line: %u"
00213             "%s%s",
00214             type, expr, function, file, line,
00215             (!message.empty()? "\n Message: " : ""),
00216             message.c_str());
00217     // Return as an STL string.
00218     string result(msg);
00219     delete[] msg;
00220     return result;
00221 }
00222 
00223 void pl_assert_fail(const char* expr, const char* file, unsigned line,
00224                     const char* function, const string& message)
00225 {
00226     // Note that in this function, just like in 'pl_check_fail' below, it is
00227     // important that the PLERROR statement fits on a single line. This is
00228     // because otherwise some tests may fail on some compilers, as the line
00229     // number of a multi-line statement may be ambiguous.
00230     string msg = get_error_message("Assertion",
00231             expr, function, file, line, message);
00232     PLERROR(msg.c_str());
00233 }
00234 
00235 void pl_check_fail(const char* expr, const char* file, unsigned line,
00236         const char* function, const string& message)
00237 {
00238     string msg = get_error_message("Check",
00239             expr, function, file, line, message);
00240     PLERROR(msg.c_str());
00241 }
00242 
00243 } // end of namespace PLearn
00244 
00245 
00246 /*
00247   Local Variables:
00248   mode:c++
00249   c-basic-offset:4
00250   c-file-style:"stroustrup"
00251   c-file-offsets:((innamespace . 0)(inline-open . 0))
00252   indent-tabs-mode:nil
00253   fill-column:79
00254   End:
00255 */
00256 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines