HPCToolkit
diagnostics.h
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //****************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 // Author:
59 // Nathan Tallent
60 //
61 //****************************************************************************
62 
63 #ifndef support_diagnostics_h
64 #define support_diagnostics_h
65 
66 //************************** System Include Files ****************************
67 
68 #if defined(__cplusplus)
69 # include <cstdlib>
70 #else
71 # include <stdlib.h>
72 #endif
73 
74 //*************************** User Include Files *****************************
75 
76 //************************** Forward Declarations ****************************
77 
78 #if defined(__cplusplus)
79 #define DIAG_EXTERN extern "C"
80 #else
81 #define DIAG_EXTERN extern
82 #endif
83 
84 // Debug and verbosity levels: higher level --> more info; 0 turns
85 // respective messages off
86 
87 // Private debugging level: messages for in-house debugging [0-9]
88 #define DIAG_DBG_LVL 0
89 
90 // Public debugging level: stuff that a few users may find interesting [0-9]
91 extern int DIAG_DBG_LVL_PUB; // default: 0
92 
93 DIAG_EXTERN void
95 
96 DIAG_EXTERN int
98 
99 // This routine is called before an error that stops execution. It is
100 // especially useful for use with debuggers that do not have good
101 // exception support.
102 DIAG_EXTERN void
104  unsigned int lineno);
105 
106 
107 //****************************************************************************
108 // Diagnostic macros
109 //****************************************************************************
110 
111 // DIAG_MsgIf: Print a message if the expression is true
112 
113 // DIAG_Msg: Print a message if level satisfies the diagnostic filter
114 
115 // DIAG_DevMsgIf: Print a message if the expression is true
116 
117 // DIAG_DevMsg: Print a message if private level satisfies the
118 // private diagnostic filter
119 
120 // DIAG_EMsg: Print an error message and continue.
121 
122 // DIAG_Assert: Throw an assertion (die) if 'expr' evaluates to
123 // false. Stops at 'Diagnostics_TheMostVisitedBreakpointInHistory'.
124 
125 // DIAG_AssertWarn: Print a warning if 'expr' evaluates to false.
126 
127 // DIAG_Die: Print an error message and die. Stops at
128 // 'Diagnostics_TheMostVisitedBreakpointInHistory'.
129 
130 // DIAG_Throw: (C++ only) Throw a fatal exception. Stops at
131 // 'Diagnostics_TheMostVisitedBreakpointInHistory'.
132 
133 // DIAG_If: If public diagnostic level is at least 'level' ...
134 #define DIAG_If(level) if (level <= DIAG_DBG_LVL_PUB)
135 
136 // DIAG_DevIf: If development diagnostic level is at least 'level' ...
137 #define DIAG_DevIf(level) if (level <= DIAG_DBG_LVL)
138 
139 // ---------------------------------------------------------------------------
140 // C++ diagnostics
141 // ---------------------------------------------------------------------------
142 
143 #if defined(__cplusplus)
144 
145 #include <sstream>
146 
147 #include "Exception.hpp"
148 
149 #define DIAG_CERR std::cout
150 #define DIAG_ENDL std::endl /*<< std::flush*/
151 
152 // All of these macros have a parameter named 'streamArgs' for one or
153 // more ostream arguments. These macros use these arguments to create
154 // a message string. Example:
155 // if (...) DIAG_EMsg("bad val: '" << v << "'")
156 
157 #define DIAG_MsgIf_GENERIC(tag, ifexpr, streamArgs) \
158  if (ifexpr) { \
159  DIAG_CERR << tag << streamArgs << DIAG_ENDL; }
160 
161 
162 #define DIAG_MsgIf(ifexpr, streamArgs) \
163  DIAG_MsgIf_GENERIC("msg: ", ifexpr, streamArgs)
164 
165 #define DIAG_MsgIfCtd(ifexpr, streamArgs) \
166  DIAG_MsgIf_GENERIC("", ifexpr, streamArgs)
167 
168 #define DIAG_Msg(level, streamArgs) \
169  DIAG_MsgIf((level <= DIAG_DBG_LVL_PUB), streamArgs)
170 
171 #define DIAG_MsgCtd(level, streamArgs) \
172  DIAG_MsgIfCtd((level <= DIAG_DBG_LVL_PUB), streamArgs)
173 
174 
175 #define DIAG_DevMsgIf(ifexpr, streamArgs) \
176  DIAG_MsgIf_GENERIC("msg*: ", ifexpr, streamArgs)
177 
178 #define DIAG_DevMsgIfCtd(ifexpr, streamArgs) \
179  DIAG_MsgIf_GENERIC("", ifexpr, streamArgs)
180 
181 #define DIAG_DevMsg(level, streamArgs) \
182  if (level <= DIAG_DBG_LVL) { \
183  DIAG_CERR << "msg* [" << level << "]: " << streamArgs << DIAG_ENDL; }
184 
185 
186 #define DIAG_WMsgIf(ifexpr, streamArgs) \
187  DIAG_MsgIf_GENERIC("WARNING: ", ifexpr, streamArgs)
188 
189 #define DIAG_WMsgIfCtd(ifexpr, streamArgs) \
190  DIAG_MsgIf_GENERIC("", ifexpr, streamArgs)
191 
192 #define DIAG_WMsg(level, streamArgs) \
193  DIAG_WMsgIf((level <= DIAG_DBG_LVL_PUB), streamArgs)
194 
195 #define DIAG_EMsg(streamArgs) \
196  DIAG_CERR << "ERROR: " << streamArgs << DIAG_ENDL
197 
198 #define DIAG_DevEMsg(streamArgs) \
199  { DIAG_CERR << "ERROR: " << streamArgs << DIAG_ENDL; \
200  if (DIAG_DBG_LVL_PUB) { \
201  DIAG_CERR << "\t[" << __FILE__ << ":" << __LINE__ << "]" << DIAG_ENDL; } \
202  }
203 
204 #define DIAG_Assert(expr, streamArgs) \
205  if (!(expr)) DIAG_Throw(streamArgs)
206 
207 #define DIAG_AssertWarn(expr, streamArgs) \
208  if (!(expr)) DIAG_EMsg(streamArgs)
209 
210 // (Equivalent to DIAG_Throw.)
211 #define DIAG_Die(streamArgs) \
212  DIAG_Throw(streamArgs)
213 
214 // (Equivalent to DIAG_Die.) Based on Jean Utke's code in xaifBooster.
215 #define DIAG_Throw(streamArgs) \
216  { std::ostringstream WeIrDnAmE; \
217  WeIrDnAmE << streamArgs /*<< std::ends*/; \
218  throw Diagnostics::FatalException(WeIrDnAmE.str(), __FILE__, __LINE__); }
219 
220 #define DIAG_ThrowX(ExceptionClass, streamArgs) \
221  { std::ostringstream WeIrDnAmE; \
222  WeIrDnAmE << streamArgs /*<< std::ends*/; \
223  throw ExceptionClass(WeIrDnAmE.str(), __FILE__, __LINE__); }
224 
225 #endif
226 
227 
228 // ---------------------------------------------------------------------------
229 // C diagnostics
230 // ---------------------------------------------------------------------------
231 
232 #if !defined(__cplusplus)
233 
234 #include <stdio.h>
235 
236 #define DIAG_MsgIf(ifexpr, ...) \
237  if (ifexpr) { \
238  fputs("msg: ", stdout); \
239  fprintf(stdout, __VA_ARGS__); fputs("\n", stdout); }
240 
241 #define DIAG_Msg(level, ...) \
242  if (level <= DIAG_DBG_LVL_PUB) { \
243  fprintf(stdout, "msg [%d]: ", level); \
244  fprintf(stdout, __VA_ARGS__); fputs("\n", stdout); }
245 
246 #define DIAG_DevMsg(level, ...) \
247  if (level <= DIAG_DBG_LVL) { \
248  fprintf(stdout, "msg* [%d]: ", level); \
249  fprintf(stdout, __VA_ARGS__); fputs("\n", stdout); }
250 
251 #define DIAG_EMsg(...) \
252  { fputs("ERROR: ", stdout); \
253  fprintf(stdout, __VA_ARGS__); fputs("\n", stdout); \
254  }
255 
256 #define DIAG_DevEMsg(...) \
257  { fputs("ERROR: ", stdout); \
258  fprintf(stdout, __VA_ARGS__); fputs("\n", stdout); \
259  if (DIAG_DBG_LVL_PUB) { \
260  fprintf(stdout, "\t[%s:%d]\n", __FILE__, __LINE__); } \
261  }
262 
263 //#define DIAG_Assert(expr, ...) // cf. Open64's FmtAssert
264 
265 //#define DIAG_AssertWarn(expr, ...)
266 
267 #define DIAG_Die(...) \
268  DIAG_EMsg(__VA_ARGS__); \
269  { Diagnostics_TheMostVisitedBreakpointInHistory(__FILE__, __LINE__); exit(1); }
270 
271 #endif
272 
273 
274 //****************************************************************************
275 //
276 //****************************************************************************
277 
278 extern const char* DIAG_Unimplemented;
279 extern const char* DIAG_UnexpectedInput;
280 extern const char* DIAG_UnexpectedOpr;
281 
282 
283 #endif /* support_diagnostics_h */
284 
DIAG_EXTERN void Diagnostics_TheMostVisitedBreakpointInHistory(const char *filenm, unsigned int lineno)
const char * DIAG_Unimplemented
const char * DIAG_UnexpectedInput
#define DIAG_EXTERN
Definition: diagnostics.h:81
DIAG_EXTERN void Diagnostics_SetDiagnosticFilterLevel(int lvl)
Definition: diagnostics.cpp:80
DIAG_EXTERN int Diagnostics_GetDiagnosticFilterLevel()
Definition: diagnostics.cpp:87
const char * DIAG_UnexpectedOpr
int DIAG_DBG_LVL_PUB
Definition: diagnostics.cpp:77