CPN
Computational Process Networks
ErrnoException.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 <errno.h>
27 #include <string.h>
28 
29 static const char UNKNOWN_ERROR[] = "Unknown error";
30 
32  : Exception(3), error(errno)
33 {
34  Fill();
35  errorstring += "\nBacktrace:\n" + GetStackTrace();
36 }
37 
39  : Exception(3), error(err)
40 {
41  Fill();
42  errorstring += "\nBacktrace:\n" + GetStackTrace();
43 }
44 
45 ErrnoException::ErrnoException(const char *msg, int err) throw()
46  : Exception(3), error(err)
47 {
48  Fill();
49  errorstring += ": ";
50  errorstring += msg;
51  errorstring += "\nBacktrace:\n" + GetStackTrace();
52 }
53 
55 }
56 
57 const char* ErrnoException::what() const throw() {
58  return errorstring.c_str();
59 }
60 
61 void ErrnoException::Fill() throw() {
62  std::vector<char> errstr(256, '\0');
63  do {
64 #if defined(__APPLE__)
65  // if __APPLE__ strerror_r is declared to return an int
66  int err = strerror_r(error, &errstr[0], errstr.size());
67  if (err == 0) {
68  errorstring = &errstr[0];
69  break;
70  } else {
71  if (errno == ERANGE) {
72  errstr.resize(2*errstr.size(), '\0');
73  } else {
75  break;
76  }
77  }
78 #else
79  char *str = strerror_r(error, &errstr[0], errstr.size());
80 
81  // Wierdness with different versions of strerror... From the man page:
82  //
83  // The strerror() and strerror_r() functions return the appropriate
84  // error description string, or an "Unknown error nnn" message if the
85  // error number is unknown.
86  //
87  // The XSI-compliant strerror_r() function returns 0 on success; on
88  // error, -1 is returned and errno is set to indicate the error.
89  //
90  // So str can be ether an error code OR a pointer to the error
91  // string... wierdness.
92 
93  if (str == (char*)-1) {
94  if (errno == ERANGE) {
95  errstr.resize(2*errstr.size(), '\0');
96  } else {
98  break;
99  }
100  } else if (str == 0) {
101  errorstring = &errstr[0];
102  break;
103  } else {
104  errorstring = str;
105  break;
106  }
107 #endif
108  } while (true);
109 }
110 
std::string errorstring
virtual ~ErrnoException()
static const char UNKNOWN_ERROR[]
std::string GetStackTrace() const
Definition: Exception.h:40
virtual const char * what() const