]> git.tdb.fi Git - libs/core.git/blob - source/core/systemerror.cpp
6d1d876c1f3e00fb94886a1612454af7443bf01a
[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 "systemerror.h"
9
10 using namespace std;
11
12 namespace Msp {
13
14 system_error::system_error(const string &w, int c):
15         runtime_error(w+": "+get_message(c)),
16         code_(c)
17 { }
18
19 string system_error::get_message(int c)
20 {
21 #ifdef WIN32
22         if(c==-1)
23                 c = GetLastError();
24
25         char msg[1024];
26         if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, c, 0, msg, sizeof(msg), 0))
27                 return msg;
28         else
29                 return lexical_cast(c, Fmt().hex());
30 #else
31         if(c==-1)
32                 c = errno;
33
34         return strerror(c);
35 #endif
36 }
37
38 } // namespace Msp