-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSystemError.cpp
78 lines (63 loc) · 2.31 KB
/
SystemError.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "StdAfx.h"
#include "SystemError.h"
#include "encoding.h"
#include <cstdio>
#include <system_error>
#include <comdef.h>
#include <winbase.h>
using namespace Linter;
SystemError::SystemError(const SourceLocationCurrent &location) : SystemError(GetLastError(), location)
{
}
SystemError::SystemError(std::string const &info, const SourceLocationCurrent &location) : SystemError(GetLastError(), info, location)
{
}
SystemError::SystemError(DWORD err, const SourceLocationCurrent &location)
{
// Note: Technically, the message function could fail.
std::snprintf(m_buff, sizeof(m_buff), "%s", std::system_category().message(err).c_str());
addLocationToMessage(location);
}
SystemError::SystemError(DWORD err, std::string const &info, const SourceLocationCurrent &location)
{
// Note: Technically, the message function could fail.
std::snprintf(m_buff, sizeof(m_buff), "%s - %s", info.c_str(), std::system_category().message(err).c_str());
addLocationToMessage(location);
}
SystemError::SystemError(HRESULT err, const SourceLocationCurrent &location)
{
IErrorInfo *err_info{nullptr};
(void)GetErrorInfo(0, &err_info);
_com_error error{err, err_info};
std::snprintf(m_buff, sizeof(m_buff), "%s", Encoding::toUTF(std::wstring(error.ErrorMessage())).c_str());
addLocationToMessage(location);
}
SystemError::SystemError(HRESULT err, std::string const &info, const SourceLocationCurrent &location)
{
IErrorInfo *err_info{nullptr};
(void)GetErrorInfo(0, &err_info);
_com_error error{err, err_info};
std::snprintf(m_buff, sizeof(m_buff), "%s - %s", info.c_str(), Encoding::toUTF(std::wstring(error.ErrorMessage())).c_str());
addLocationToMessage(location);
}
SystemError::~SystemError() = default;
char const *Linter::SystemError::what() const noexcept
{
return &m_buff[0];
}
void SystemError::addLocationToMessage(const SourceLocationCurrent &location)
{
const char *fullPath = location.file_name();
if (fullPath == nullptr || fullPath[0] == 0)
{
return;
}
const char *fileName = std::strrchr(fullPath, '\\');
const std::size_t used{std::strlen(m_buff)};
std::snprintf(m_buff + used,
sizeof(m_buff) - used,
" at %s:%d %s",
(fileName ? fileName + 1 : fullPath),
location.line(),
location.function_name());
}