lassert.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #include <lestes/msg/logger.hh>
00029 #include <lestes/std/backtrace.hh>
00030 #include <lestes/std/event.hh>
00031 #include <lestes/std/lassert.hh>
00032 #include <lestes/std/lassert.m.hh>
00033 #include <iostream>
00034 #include <cstdlib>
00035 #include <cstring>
00036 
00037 package(lestes);
00038 package(std);
00039 
00040 initialize_event( lassert_event );
00041 
00042 static bool lassert_failing;
00043 
00044 /*!
00045  This function can ignore the condition provided that the <code>LESTES_LASSERT</code>
00046  environment variable is set to the string "continue", "0", or "nil".
00047  */
00048 void lassert_fail( lstring assertion,
00049                 lstring filename, ulint line, lstring function,
00050                 lstring message, ptr<source_location> loc )
00051 {
00052         if (loc) {
00053                 if (message.empty())
00054                         report << lassert_msg_default << loc;
00055                 else
00056                         report << lassert_msg << message << loc;
00057         }
00058         ::std::cerr << filename << ": In function '" << function << "': " << ::std::endl;
00059         if (!message.empty())
00060                 ::std::cerr << filename << ":   " << message << ::std::endl;
00061         ::std::cerr << filename << ':' << line << ":"
00062                 " lassertion failed: \"" << assertion << "\"." << ::std::endl;
00063 
00064 #ifndef NO_BACKTRACE_IN_LASSERT
00065         ::std::cerr << assemble_backtrace() << ::std::endl;
00066 #endif
00067 
00068         // do not trigger the event when "double-failing"
00069         //   it can happen when lassertion fails inside an action run by the event
00070         // bool flag is enough, as in the second invocation of this function
00071         //   there is nothing that can fail
00072         if (!lassert_failing) {
00073                 lassert_failing = true;
00074                 lassert_event->trigger();
00075         } else
00076                 ::std::cerr << filename << ": Double-fail!" << ::std::endl;
00077 
00078 #ifdef CONTINUABLE_ASSERTIONS
00079         const char * lestes_lassert = ::std::getenv("LESTES_LASSERT");
00080         if (lestes_lassert) {
00081                 lstring value(lestes_lassert);
00082                 if (value == "continue" || value == "0" || value == "nil") {
00083                         ::std::cerr << filename << ':' << line <<
00084                                 ": warning: lassertion continued, i.e. ignored." << ::std::endl;
00085                         lassert_failing = false;
00086                 } else {
00087                         // close log files, if any
00088 			::lestes::msg::logger::finish();
00089 
00090                         abort();
00091                 }
00092         } else
00093 #endif
00094         {
00095                 // close log files, if any
00096 		::lestes::msg::logger::finish();
00097 
00098                 abort();
00099         }
00100 }
00101 
00102 end_package(std);
00103 end_package(lestes);
00104 
00105 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:22:36 2007 for lestes by doxygen 1.5.1-20070107