]> git.tdb.fi Git - libs/core.git/blob - source/core/systemerror.cpp
1ccee74ed4a80139c77e1e013567465f4325906e
[libs/core.git] / source / core / systemerror.cpp
1 #ifdef WIN32
2 #include <windows.h>
3 #include <msp/strings/lexicalcast.h>
4 #else
5 #include <cerrno>
6 #include <cstring>
7 #endif
8 #include <limits>
9 #include "systemerror.h"
10
11 using namespace std;
12
13 namespace Msp {
14
15 system_error::system_error(const string &w, int c):
16         runtime_error(w+": "+get_message(c)),
17         code_(c)
18 { }
19
20 system_error::system_error(const string &w, const string &e):
21         runtime_error(w+": "+e),
22         code_(numeric_limits<int>::min())
23 { }
24
25 string system_error::get_message(int c)
26 {
27 #ifdef WIN32
28         if(c==-1)
29                 c = GetLastError();
30
31         char msg[1024];
32         if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, c, 0, msg, sizeof(msg), 0))
33                 return msg;
34         else
35                 return lexical_cast<string>(c, Fmt().hex());
36 #else
37         if(c==-1)
38                 c = errno;
39
40         return strerror(c);
41 #endif
42 }
43
44 } // namespace Msp