MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
myassert.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/myassert.cc,v 1.29 2017/01/12 14:44:05 masarati Exp $ */
2 /*
3  * This library comes with MBDyn (C), a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 /******************************************************************************
32 
33 Macro di assert personalizzate
34 
35 Uso: ASSERT(<expr>);
36  - se <expr> e' vera ( != 0 ) non fa nulla;
37  - se <expr> e' falsa, scrive sul flusso di errore std::cerr il file e la riga
38  solo se DEBUG e' definita.
39 
40 Uso: ASSERTMSG(<expr>, <msg>);
41  - se <expr> e' vera ( != 0 ) non fa nulla;
42  - se <expr> e' falsa, scrive sul flusso di errore std::cerr il file e la riga,
43  seguiti dal messaggio <msg>, solo se DEBUG e' definita.
44 
45 Entrambe chiamano la funzione _Assert(file, line, msg = NULL);
46 se msg e' definito, viene aggiunto in coda al messaggio di default
47 
48 ******************************************************************************/
49 
50 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
51 #include <cassert>
52 #include <cstring>
53 #include <cstdlib>
54 
55 #include "myassert.h"
56 
57 /* flag di silent run (no output su stdout) */
58 int fSilent = 0;
59 int fPedantic = 0;
60 
61 #ifdef DEBUG
62 
63 long int debug_level = MYDEBUG_ANY;
64 long int DEFAULT_DEBUG_LEVEL = MYDEBUG_ANY;
65 
66 void _Assert(const char* file, const int line, const char* msg)
67 {
68  std::cout.flush();
69 
70  std::cerr << std::endl << "ASSERT fault in file " << file
71  << " at line " << line;
72  if (msg) {
73  std::cerr << ':' << std::endl << msg;
74  }
75  std::cerr << std::endl;
76 
77 #ifdef DEBUG_STOP
78  throw MyAssert::ErrGeneric(MBDYN_EXCEPT_ARGS);
79 #endif
80 
81  return;
82 }
83 
84 std::ostream& _Out(std::ostream& out, const char* file, const int line)
85 {
86  std::cout.flush();
87 
88  // out << "File <" << file << ">, line [" << line << "]: ";
89  out << "[" << file << "," << line << "]: ";
90  return out;
91 }
92 
93 int get_debug_options(const char *const s, const debug_array da[])
94 {
95  if (s == NULL || s[0] == '\0') {
96  ::debug_level = DEFAULT_DEBUG_LEVEL;
97  return 0;
98  }
99 
100  const char* p = s;
101  while (true) {
102  const char* sep = std::strchr(p, ':');
103  unsigned int l;
104  if (sep != NULL) {
105  l = int(sep-p);
106  } else {
107  l = strlen(p);
108  }
109  debug_array* w = (debug_array*)da;
110  while (w->s != NULL) {
111  if (l == strlen(w->s) && strncmp(w->s, p, l) == 0) {
112  break;
113  }
114  w++;
115  }
116  if (w->s == NULL) {
117  if (l == 4 && strncmp("none", p, 4) == 0) {
118  ::debug_level = MYDEBUG_NONE;
119  } else if (l == 3 && strncmp("any", p, 3) == 0) {
120  ::debug_level = MYDEBUG_ANY;
121  } else {
122  silent_cerr("Unknown debug level \"");
123  for (unsigned int i = 0; i < l; i++) {
124  silent_cerr(p[i]);
125  }
126  silent_cerr("\"" << std::endl);
127  }
128  } else {
129  ::debug_level |= w->l;
130  silent_cerr("debug level: " << w->s << std::endl);
131  }
132  if (sep == NULL) {
133  break;
134  }
135  p = sep+1;
136  }
137 
138  return 0;
139 }
140 
141 #endif /* DEBUG */
142 
143 #if defined(__GNUC__) && (defined(_M_IX86) || defined(__x86_64)) && !defined(NDEBUG) && (defined(__CYGWIN__) || defined(_WIN32))
144 extern "C" void __assert_func (const char* file, int line, const char* func, const char* expr)
145 {
146  std::cerr << "assertion " << expr << " failed: file " << file << ":" << line << ":" << func << std::endl;
147 
148  // debug break interrupt on x86 and x86_64 makes debugging easier on Windows
149  __asm__ volatile ("int $3");
150 
151  abort();
152 }
153 #endif
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
int fSilent
Definition: myassert.cc:58
void func(const T &u, const T &v, const T &w, doublereal e, T &f)
static char * file
Definition: ann_in.c:101
int fPedantic
Definition: myassert.cc:59
struct mbrtai_msg_t msg