xoreos  0.0.5
error.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cstdarg>
26 #include <cstdio>
27 #include <cstdlib>
28 
29 #include "src/common/error.h"
30 #include "src/common/util.h"
31 
32 namespace Common {
33 
35 }
36 
37 StackException::StackException(const char *s, ...) {
38  char buf[STRINGBUFLEN];
39  va_list va;
40 
41  va_start(va, s);
42  vsnprintf(buf, STRINGBUFLEN, s, va);
43  va_end(va);
44 
45  _stack.push(buf);
46 }
47 
48 StackException::StackException(const StackException &e) : _stack(e._stack) {
49 }
50 
51 StackException::StackException(const std::exception &e) {
52  add(e);
53 }
54 
56 }
57 
58 void StackException::add(const char *s, ...) {
59  char buf[STRINGBUFLEN];
60  va_list va;
61 
62  va_start(va, s);
63  vsnprintf(buf, STRINGBUFLEN, s, va);
64  va_end(va);
65 
66  _stack.push(buf);
67 }
68 
69 void StackException::add(const std::exception &e) {
70  add("%s", e.what());
71 }
72 
73 const char *StackException::what() const throw() {
74  if (_stack.empty())
75  return "";
76 
77  return _stack.top().c_str();
78 }
79 
80 bool StackException::empty() const {
81  return _stack.empty();
82 }
83 
85  return _stack;
86 }
87 
88 
89 const Exception kOpenError("Can't open file");
90 const Exception kReadError("Read error");
91 const Exception kSeekError("Seek error");
92 const Exception kWriteError("Write error");
93 
94 
95 void printException(Exception &e, const UString &prefix) {
96  try {
97  Exception::Stack &stack = e.getStack();
98 
99  if (stack.empty()) {
100  status("FATAL ERROR");
101  return;
102  }
103 
104  status("%s%s", prefix.c_str(), stack.top().c_str());
105 
106  stack.pop();
107 
108  while (!stack.empty()) {
109  status(" Because: %s", stack.top().c_str());
110  stack.pop();
111  }
112  } catch (...) {
113  status("FATAL ERROR: Exception while printing exception stack");
114  std::exit(1);
115  }
116 }
117 
118 static void exceptionDispatcher(const char *prefix, const char *reason = "") {
119  try {
120  try {
121  throw;
122  } catch (Exception &e) {
123  if (reason[0] != 0)
124  e.add("%s", reason);
125 
126  printException(e, prefix);
127  } catch (std::exception &e) {
128  Exception se(e);
129  if (reason[0] != 0)
130  se.add("%s", reason);
131 
132  printException(se, prefix);
133  } catch (...) {
134  if (reason[0] != 0) {
135  Exception se("%s", reason);
136  printException(se, prefix);
137  }
138  }
139  } catch (...) {
140  }
141 }
142 
143 void exceptionDispatcherError(const char *s, ...) {
144  char buf[STRINGBUFLEN];
145  va_list va;
146 
147  va_start(va, s);
148  vsnprintf(buf, STRINGBUFLEN, s, va);
149  va_end(va);
150 
151  exceptionDispatcher("ERROR: ", buf);
152 }
153 
155  exceptionDispatcher("ERROR: ");
156 }
157 
158 void exceptionDispatcherWarning(const char *s, ...) {
159  char buf[STRINGBUFLEN];
160  va_list va;
161 
162  va_start(va, s);
163  vsnprintf(buf, STRINGBUFLEN, s, va);
164  va_end(va);
165 
166  exceptionDispatcher("WARNING: ", buf);
167 }
168 
170  exceptionDispatcher("WARNING: ");
171 }
172 
173 } // End of namespace Common
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
Definition: 2dafile.h:39
static void exceptionDispatcher(const char *prefix, const char *reason="")
Definition: error.cpp:118
A class holding an UTF-8 string.
Definition: ustring.h:48
Stack & getStack()
Definition: error.cpp:84
Exception that provides a stack of explanations.
Definition: error.h:36
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
std::stack< UString > Stack
Definition: error.h:38
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Utility templates and functions.
const Exception kWriteError("Write error")
Exception when writing to a stream failed.
Definition: error.h:64
const Exception kReadError("Read error")
Exception when reading from a stream failed.
Definition: error.h:62
void exceptionDispatcherError(const char *s,...)
Exception dispatcher that prints the exception as an error, and adds another reason on top...
Definition: error.cpp:143
#define STRINGBUFLEN
Definition: system.h:415
const Exception kSeekError("Seek error")
Exception when seeking a stream failed.
Definition: error.h:63
void status(const char *s,...)
Definition: util.cpp:52
const Exception kOpenError("Can't open file")
Exception when a file couldn&#39;t be opened.
Definition: error.h:61
const char * what() const
Definition: error.cpp:73
void printException(Exception &e, const UString &prefix)
Print a whole exception stack to stderr and the log.
Definition: error.cpp:95
bool empty() const
Definition: error.cpp:80