tracefctxx  1.1.8
TraceFct.h
Go to the documentation of this file.
1 #ifndef TraceFct_h_INCLUDED
2 #define TraceFct_h_INCLUDED
3 
4 // --8<--8<--8<--8<--
5 //
6 // Copyright (C) 2006-2012 Smithsonian Astrophysical Observatory
7 //
8 // This file is part of tracefctxx
9 //
10 // tracefctxx is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or (at
13 // your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 //
23 // -->8-->8-->8-->8--
24 
25 #include <iosfwd>
26 #include <list>
27 #include <cstdlib>
28 
29 #include <Exception/Exception.h>
30 
42 # define _tf_assert(level,ex) \
43  do \
44  { \
45  if (!(ex)) \
46  TraceFct::exit( level, "Assertion failed: file\"%s\", line %d\n%s\n", \
47  __FILE__, __LINE__, #ex); \
48  } while(0)
49 
60 #ifdef DOXYGEN
61 # define TF_ASSERT_LEVEL
62 #endif
63 
64 
71 # define tf_assert(ex) _tf_assert(990,ex)
72 
73 #ifdef TF_ASSERT_LEVEL
74 # if TF_ASSERT_LEVEL > 0
75 
82 # define tf_assert1(ex) _tf_assert(991,ex)
83 # if TF_ASSERT_LEVEL > 1
84 
91 # define tf_assert2(ex) _tf_assert(992,ex)
92 # if TF_ASSERT_LEVEL > 2
93 
100 # define tf_assert3(ex) _tf_assert(993,ex)
101 # if TF_ASSERT_LEVEL > 3
102 
109 # define tf_assert4(ex) _tf_assert(994,ex)
110 # if TF_ASSERT_LEVEL > 4
111 
119 # define tf_assert5(ex) _tf_assert(995,ex)
120 # endif /* TF_ASSERT_LEVEL > 4 */
121 # endif /* TF_ASSERT_LEVEL > 3 */
122 # endif /* TF_ASSERT_LEVEL > 2 */
123 # endif /* TF_ASSERT_LEVEL > 1 */
124 # endif /* TF_ASSERT_LEVEL > 0 */
125 #endif /* TF_ASSERT_LEVEL */
126 
127 #ifndef tf_assert1
128 # define tf_assert1(ex)
129 #endif
130 
131 #ifndef tf_assert2
132 # define tf_assert2(ex)
133 #endif
134 
135 #ifndef tf_assert3
136 # define tf_assert3(ex)
137 #endif
138 
139 #ifndef tf_assert4
140 # define tf_assert4(ex)
141 #endif
142 
143 #ifndef tf_assert5
144 # define tf_assert5(ex)
145 #endif
146 
147 
152 class TraceFct {
153 
154  public:
155  TraceFct( const char* name );
156  TraceFct( string& name );
157  TraceFct( string name, bool print_it, int num_fct_to_print );
158  ~TraceFct( );
159 
160  static void exit (int exit_code, const char *format, ...);
161  static void exit (int exit_code, Exception& ex );
162 
163  static void exit (int exit_code, const string& msg)
164  { exit( exit_code, msg.c_str() ); };
165 
166 
167  static void die (const char *format, ...);
168 
169  static void die ( Exception& ex )
170  { exit( EXIT_FAILURE, ex ); }
171 
172  static void die ( const string& msg)
173  { exit( EXIT_FAILURE, msg ); };
174 
175  static void message (const char *format, ...);
176  static void message ( Exception& ex )
177  { print( false, ex ); }
178  static void message (const string& msg)
179  { message( msg.c_str() ); }
180 
181  static void vmessage (const char *format, va_list args);
182 
183  static void println ( Exception& ex )
184  { print( true, ex ); }
185  static void println (const string& msg)
186  { print( true, msg.c_str() ); }
187  static void println (const char* msg)
188  { print( true, msg ); }
189 
190  static void dump_stack (void);
191  static void open( const string& filename );
192  static void close(void);
193 
194  private:
195  static bool print_upon_enter_and_exit;
196  static int stack_level_to_print;
197  static string prefix;
198 
199  // a scratch buffer; need this preallocated if have run out
200  // memory later on
201  static char outbuf[8192];
202  static string progname;
203  static ostream* ostr;
204  static list<string> function_stack;
205 
206  static char* init_prefix( void );
207 
208  static void vexit_print(const char* format, va_list args );
209  static void vprint(bool print_nl, const char *format, va_list args);
210  static void print(bool print_nl, const char *format, ... );
211  static void print(bool print_nl, Exception& ex );
212 
213 };
214 
215 
216 
217 
218 #endif /* ! tracefct_h_INCLUDED */
static void die(const char *format,...)
Exit a program with error EXIT_FAILURE, dumping the function stack.
Definition: TraceFct.cc:452
static void message(Exception &ex)
Print a message to the tracefct output stream.
Definition: TraceFct.h:176
Definition: TraceFct.h:152
static void message(const char *format,...)
Print a formatted message to the tracefct output stream.
Definition: TraceFct.cc:521
TraceFct(const char *name)
Function entry constructor.
Definition: TraceFct.cc:119
static void die(Exception &ex)
Exit a program with error EXIT_FAILURE, dumping the function stack.
Definition: TraceFct.h:169
~TraceFct()
Definition: TraceFct.cc:135
static void dump_stack(void)
Print the current function stack.
Definition: TraceFct.cc:278
static void open(const string &filename)
redirect the tracefct output stream
Definition: TraceFct.cc:604
static void exit(int exit_code, const char *format,...)
Exit a program, dumping the function stack.
Definition: TraceFct.cc:367
static void println(const char *msg)
Print a message to the tracefct output stream.
Definition: TraceFct.h:187
static void vmessage(const char *format, va_list args)
Print a formatted message to the tracefct output stream using a stdargs argument list.
Definition: TraceFct.cc:549
static void println(Exception &ex)
Print a message to the tracefct output stream.
Definition: TraceFct.h:183
static void close(void)
reset the tracefct output stream to stderr.
Definition: TraceFct.cc:652