]> git.tdb.fi Git - libs/core.git/blob - source/core/except.cpp
bdcdd7df3da312b5f613630da9a67ec11ab8d0c5
[libs/core.git] / source / core / except.cpp
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <sstream>
9 #include <cstring>
10 #ifdef WIN32
11 #include <windows.h>
12 #endif
13 #include "except.h"
14
15 using namespace std;
16
17 namespace Msp {
18
19 Exception::Exception(const string &w):
20         wot(w)
21 {
22 #ifdef WITH_EXCEPTION_BACKTRACE
23         bt = Debug::Backtrace::create();
24 #endif
25 }
26
27 Exception &Exception::at(const std::string &w) throw()
28 {
29         wer = w;
30         wot = wer+": "+wot;
31         return *this;
32 }
33
34
35 SystemError::SystemError(const string &w_, int e):
36         Exception(build_what(w_, e)),
37         err(e)
38 { }
39
40 string SystemError::build_what(const string &w, int e)
41 {
42         ostringstream buf;
43         buf<<w<<": ";
44 #ifdef WIN32
45         char msg[1024];
46         if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, msg, sizeof(msg), 0))
47                 buf<<msg;
48         else
49                 buf<<e;
50 #else
51         buf<<strerror(e);
52 #endif
53         return buf.str();
54 }
55
56
57 KeyError::KeyError(const string &w_, const string &k):
58         Exception(w_+" ("+k+")"),
59         key(k)
60 { }
61
62 } // namespace Msp