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-2012 Smithsonian Astrophysical Observatory
00007 //
00008 // This file is part of tracefctxx
00009 //
00010 // tracefctxx 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, either version 3 of the License, or (at
00013 // your option) any later version.
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 // You should have received a copy of the GNU General Public License
00021 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 // -->8-->8-->8-->8--
00024 
00025 #include <iosfwd>
00026 #include <list>
00027 #include <cstdlib>
00028 
00029 #include <Exception/Exception.h>
00030 
00042 # define _tf_assert(level,ex)    \
00043   do                                                                    \
00044   {                                                                     \
00045     if (!(ex))                                                          \
00046       TraceFct::exit( level, "Assertion failed: file\"%s\", line %d\n%s\n",     \
00047             __FILE__, __LINE__, #ex);                                   \
00048   } while(0)
00049 
00060 #ifdef DOXYGEN
00061 #  define TF_ASSERT_LEVEL
00062 #endif
00063 
00064 
00071 # define tf_assert(ex)    _tf_assert(990,ex)
00072 
00073 #ifdef TF_ASSERT_LEVEL
00074 #  if TF_ASSERT_LEVEL > 0
00075 
00082 #    define tf_assert1(ex)     _tf_assert(991,ex)
00083 #    if TF_ASSERT_LEVEL > 1
00084 
00091 #      define tf_assert2(ex)     _tf_assert(992,ex)
00092 #      if TF_ASSERT_LEVEL > 2
00093 
00100 #        define tf_assert3(ex)     _tf_assert(993,ex)
00101 #        if TF_ASSERT_LEVEL > 3
00102 
00109 #          define tf_assert4(ex)     _tf_assert(994,ex)
00110 #          if TF_ASSERT_LEVEL > 4
00111 
00119 #            define tf_assert5(ex)    _tf_assert(995,ex)
00120 #          endif  /* TF_ASSERT_LEVEL > 4 */
00121 #        endif  /* TF_ASSERT_LEVEL > 3 */
00122 #      endif  /* TF_ASSERT_LEVEL > 2 */
00123 #    endif  /* TF_ASSERT_LEVEL > 1 */
00124 #  endif  /* TF_ASSERT_LEVEL > 0 */
00125 #endif  /* TF_ASSERT_LEVEL     */
00126 
00127 #ifndef tf_assert1
00128 #  define tf_assert1(ex)
00129 #endif
00130 
00131 #ifndef tf_assert2
00132 #  define tf_assert2(ex)
00133 #endif
00134 
00135 #ifndef tf_assert3
00136 #  define tf_assert3(ex)
00137 #endif
00138 
00139 #ifndef tf_assert4
00140 #  define tf_assert4(ex)
00141 #endif
00142 
00143 #ifndef tf_assert5
00144 #  define tf_assert5(ex)
00145 #endif
00146 
00147 
00152 class TraceFct {
00153 
00154  public:
00155   TraceFct( const char* name );
00156   TraceFct( string& name );
00157   TraceFct( string name, bool print_it, int num_fct_to_print );
00158   ~TraceFct( );
00159 
00160   static void exit (int exit_code, const char *format, ...);
00161   static void exit (int exit_code, Exception& ex );
00162 
00163   static void exit (int exit_code, const string& msg)
00164     { exit( exit_code, msg.c_str() ); };
00165 
00166 
00167   static void die (const char *format, ...);
00168 
00169   static void die ( Exception& ex )
00170     { exit( EXIT_FAILURE, ex ); }
00171 
00172   static void die ( const string& msg)
00173     { exit( EXIT_FAILURE, msg ); };
00174 
00175   static void message (const char *format, ...);
00176   static void message ( Exception& ex )
00177     { print( false, ex ); }
00178   static void message (const string& msg)
00179     { message( msg.c_str() ); }
00180 
00181   static void vmessage (const char *format, va_list args);
00182 
00183   static void println ( Exception& ex )
00184     { print( true, ex ); }
00185   static void println (const string& msg)
00186     { print( true, msg.c_str() ); }
00187   static void println (const char* msg)
00188     { print( true, msg ); }
00189 
00190   static void dump_stack (void);
00191   static void open( const string& filename );
00192   static void close(void);
00193 
00194  private:
00195   static bool print_upon_enter_and_exit;
00196   static int stack_level_to_print;
00197   static string prefix;
00198 
00199   // a scratch buffer; need this preallocated if have run out
00200   // memory later on
00201   static char outbuf[8192];
00202   static string progname;
00203   static ostream* ostr;
00204   static list<string> function_stack;
00205   
00206   static char*  init_prefix( void );
00207 
00208   static void vexit_print(const char* format, va_list args );
00209   static void vprint(bool print_nl, const char *format, va_list args);
00210   static void print(bool print_nl, const char *format, ... );
00211   static void print(bool print_nl, Exception& ex );
00212 
00213 };
00214 
00215 
00216 
00217 
00218 #endif  /* ! tracefct_h_INCLUDED */