CPN
Computational Process Networks
ThrowingAssert.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
24 #include "common_priv.h"
26 #include <cpn/utils/StackTrace.h>
27 #include <sstream>
28 #include <vector>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 
33 
34 std::string CreateMessage(const char *exp, const char *file,
35  int line, const char *func, const char *msg) {
36  std::ostringstream oss;
37  oss << "Assert failed in " << file << ":" << line;
38  oss << " in " << func;
39  oss << " : " << exp << '\n' << msg << '\n';
40  return oss.str();
41 }
42 
43 AssertException::AssertException(const std::string &msg) throw()
44  : Exception(4)
45 {
46  std::ostringstream oss;
47  oss << msg << "\nBacktrace:\n" << GetStackTrace() << "\n";
48  message = oss.str();
49 }
50 
51 
53 
54 const char *AssertException::what() const throw() {
55  return message.c_str();
56 }
57 
58 
59 bool __ASSERT(const char *exp, const char *file, int line, const char *func, bool die) {
60  if (die) {
61  fprintf(stderr, "%s\nBacktrace:\n%s\n",
62  CreateMessage(exp, file, line, func, "").c_str(), GetStack(2).c_str());
63  abort();
64  } else {
65  throw AssertException(CreateMessage(exp, file, line, func, ""));
66  }
67 }
68 
69 bool __ASSERT(const char *exp, const char *file, int line, const char *func, bool die, const std::string &msg) {
70  if (die) {
71  fprintf(stderr, "%s\nBacktrace:\n%s\n",
72  CreateMessage(exp, file, line, func, "").c_str(), GetStack(2).c_str());
73  abort();
74  } else {
75  throw AssertException(CreateMessage(exp, file, line, func, msg.c_str()));
76  }
77 }
78 
79 bool __ASSERT(const char *exp, const char *file, int line, const char *func, bool die, const char *fmt, ...) {
80  std::vector<char> buff(128, '\0');
81  // Note this is based on the exmaple in the man page for vsnprintf.
82  while (1) {
83  /* Try to print in the allocated space. */
84  va_list ap;
85  va_start(ap, fmt);
86  int n = vsnprintf(&buff[0], buff.size(), fmt, ap);
87  va_end(ap);
88  /* If that worked, return the string. */
89  if (n > -1 && unsigned(n) < buff.size()) {
90  break;
91  }
92  /* Else try again with more space. */
93  if (n > -1) { /* glibc 2.1 */ /* precisely what is needed */
94  buff.resize(n+1, '\0');
95  }
96  else { /* glibc 2.0 */ /* twice the old size */
97  buff.resize(buff.size()*2, '\0');
98  }
99  }
100  std::string msg = CreateMessage(exp, file, line, func, &buff[0]);
101  if (die) {
102  fprintf(stderr, "%s\nBacktrace:\n%s\n", msg.c_str(), GetStack(2).c_str());
103  abort();
104  } else {
105  throw AssertException(msg);
106  }
107 }
108 
109 
bool __ASSERT(const char *exp, const char *file, int line, const char *func, bool die) __attribute__((noreturn))
std::string CreateMessage(const char *exp, const char *file, int line, const char *func, const char *msg)
The exception thrown by the ASSERT macro.
std::string GetStack(unsigned ignore=0)
Definition: StackTrace.cc:39
function for printing out a stack trace Note that we need to pass -rdynamic to the linker to be able ...
std::string message
AssertException(const std::string &msg)
virtual ~AssertException()
virtual const char * what() const