CPN
Computational Process Networks
Functions
ThrowingAssert.cc File Reference

Some implementation for assert. More...

#include "common_priv.h"
#include <cpn/utils/ThrowingAssert.h>
#include <cpn/utils/StackTrace.h>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
+ Include dependency graph for ThrowingAssert.cc:

Go to the source code of this file.

Functions

std::string CreateMessage (const char *exp, const char *file, int line, const char *func, const char *msg)
 
bool __ASSERT (const char *exp, const char *file, int line, const char *func, bool die)
 
bool __ASSERT (const char *exp, const char *file, int line, const char *func, bool die, const std::string &msg)
 
bool __ASSERT (const char *exp, const char *file, int line, const char *func, bool die, const char *fmt,...)
 

Detailed Description

Some implementation for assert.

Author
John Bridgman

Definition in file ThrowingAssert.cc.

Function Documentation

bool __ASSERT ( const char *  exp,
const char *  file,
int  line,
const char *  func,
bool  die 
)

Definition at line 59 of file ThrowingAssert.cc.

References CreateMessage(), and GetStack().

59  {
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 }
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

+ Here is the call graph for this function:

bool __ASSERT ( const char *  exp,
const char *  file,
int  line,
const char *  func,
bool  die,
const std::string &  msg 
)

Definition at line 69 of file ThrowingAssert.cc.

References CreateMessage(), and GetStack().

69  {
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 }
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

+ Here is the call graph for this function:

bool __ASSERT ( const char *  exp,
const char *  file,
int  line,
const char *  func,
bool  die,
const char *  fmt,
  ... 
)

Definition at line 79 of file ThrowingAssert.cc.

References CreateMessage(), and GetStack().

79  {
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 }
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

+ Here is the call graph for this function:

std::string CreateMessage ( const char *  exp,
const char *  file,
int  line,
const char *  func,
const char *  msg 
)

Definition at line 34 of file ThrowingAssert.cc.

Referenced by __ASSERT().

35  {
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 }

+ Here is the caller graph for this function: