TraceFct.h

Go to the documentation of this file.
00001 #ifndef TraceFct_h_INCLUDED
00002 #define TraceFct_h_INCLUDED
00003 
00004 // --8<--8<--8<--8<--
00005 //
00006 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00007 //
00008 // This file is part of tracefctxx
00009 //
00010 // tracefctxx is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // tracefctxx 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 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the 
00022 //       Free Software Foundation, Inc. 
00023 //       51 Franklin Street, Fifth Floor
00024 //       Boston, MA  02110-1301, USA
00025 //
00026 // -->8-->8-->8-->8--
00027 
00028 #include <iosfwd>
00029 #include <list>
00030 #include <cstdlib>
00031 
00032 #include <Exception/Exception.h>
00033 
00045 # define _tf_assert(level,ex)    \
00046   do                                                                    \
00047   {                                                                     \
00048     if (!(ex))                                                          \
00049       TraceFct::exit( level, "Assertion failed: file\"%s\", line %d\n%s\n",     \
00050             __FILE__, __LINE__, #ex);                                   \
00051   } while(0)
00052 
00063 #ifdef DOXYGEN
00064 #  define TF_ASSERT_LEVEL
00065 #endif
00066 
00067 
00074 # define tf_assert(ex)    _tf_assert(990,ex)
00075 
00076 #ifdef TF_ASSERT_LEVEL
00077 #  if TF_ASSERT_LEVEL > 0
00078 
00085 #    define tf_assert1(ex)     _tf_assert(991,ex)
00086 #    if TF_ASSERT_LEVEL > 1
00087 
00094 #      define tf_assert2(ex)     _tf_assert(992,ex)
00095 #      if TF_ASSERT_LEVEL > 2
00096 
00103 #        define tf_assert3(ex)     _tf_assert(993,ex)
00104 #        if TF_ASSERT_LEVEL > 3
00105 
00112 #          define tf_assert4(ex)     _tf_assert(994,ex)
00113 #          if TF_ASSERT_LEVEL > 4
00114 
00122 #            define tf_assert5(ex)    _tf_assert(995,ex)
00123 #          endif  /* TF_ASSERT_LEVEL > 4 */
00124 #        endif  /* TF_ASSERT_LEVEL > 3 */
00125 #      endif  /* TF_ASSERT_LEVEL > 2 */
00126 #    endif  /* TF_ASSERT_LEVEL > 1 */
00127 #  endif  /* TF_ASSERT_LEVEL > 0 */
00128 #endif  /* TF_ASSERT_LEVEL     */
00129 
00130 #ifndef tf_assert1
00131 #  define tf_assert1(ex)
00132 #endif
00133 
00134 #ifndef tf_assert2
00135 #  define tf_assert2(ex)
00136 #endif
00137 
00138 #ifndef tf_assert3
00139 #  define tf_assert3(ex)
00140 #endif
00141 
00142 #ifndef tf_assert4
00143 #  define tf_assert4(ex)
00144 #endif
00145 
00146 #ifndef tf_assert5
00147 #  define tf_assert5(ex)
00148 #endif
00149 
00150 
00155 class TraceFct {
00156 
00157  public:
00158   TraceFct( const char* name );
00159   TraceFct( string& name );
00160   TraceFct( string name, bool print_it, int num_fct_to_print );
00161   ~TraceFct( );
00162 
00163   static void exit (int exit_code, const char *format, ...);
00164   static void exit (int exit_code, Exception& ex );
00165 
00166   static void exit (int exit_code, const string& msg)
00167     { exit( exit_code, msg.c_str() ); };
00168 
00169 
00170   static void die (const char *format, ...);
00171 
00172   static void die ( Exception& ex )
00173     { exit( EXIT_FAILURE, ex ); }
00174 
00175   static void die ( const string& msg)
00176     { exit( EXIT_FAILURE, msg ); };
00177 
00178   static void message (const char *format, ...);
00179   static void message ( Exception& ex )
00180     { print( false, ex ); }
00181   static void message (const string& msg)
00182     { message( msg.c_str() ); }
00183 
00184   static void vmessage (const char *format, va_list args);
00185 
00186   static void println ( Exception& ex )
00187     { print( true, ex ); }
00188   static void println (const string& msg)
00189     { print( true, msg.c_str() ); }
00190   static void println (const char* msg)
00191     { print( true, msg ); }
00192 
00193   static void dump_stack (void);
00194   static void open( const string& filename );
00195   static void close(void);
00196 
00197  private:
00198   static bool print_upon_enter_and_exit;
00199   static int stack_level_to_print;
00200   static string prefix;
00201 
00202   // a scratch buffer; need this preallocated if have run out
00203   // memory later on
00204   static char outbuf[8192];
00205   static string progname;
00206   static ostream* ostr;
00207   static list<string> function_stack;
00208   
00209   static char*  init_prefix( void );
00210 
00211   static void vexit_print(const char* format, va_list args );
00212   static void vprint(bool print_nl, const char *format, va_list args);
00213   static void print(bool print_nl, const char *format, ... );
00214   static void print(bool print_nl, Exception& ex );
00215 
00216 };
00217 
00218 
00219 
00220 
00221 #endif  /* ! tracefct_h_INCLUDED */

Generated on Mon Oct 20 13:55:17 2008 for tracefctxx by  doxygen 1.5.6