-
Notifications
You must be signed in to change notification settings - Fork 1
/
DebugOut.h
69 lines (58 loc) · 2.17 KB
/
DebugOut.h
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
#pragma once
#include <memory>
#include <string>
#include <stdexcept>
#include <windows.h>
#include <debugapi.h>
#include <comdef.h>
#include "../Engine/ThirdParty/spdlog/spdlog.h"
#include "../Engine/ThirdParty/spdlog/sinks/basic_file_sink.h"
#include "../Engine/ThirdParty/spdlog/sinks/msvc_sink.h"
namespace dout {
// refer to https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
template<typename ... Args>
std::string string_format(const std::string& format, Args ... args)
{
int size_s = std::snprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0'
if (size_s <= 0) {
throw std::runtime_error("Error during formatting.");
}
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[size]);
std::snprintf(buf.get(), size, format.c_str(), args ...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
static auto sink = std::make_shared<spdlog::sinks::msvc_sink_mt>();
static auto msvc_logger = std::make_shared<spdlog::logger>("msvc_logger", sink);
template<typename ... Args>
void printf(const std::string& format, Args ... args)
{
std::string ret = string_format(format, args...);
msvc_logger->info(ret);
}
class DxException
{
public:
DxException() = default;
DxException(HRESULT hr, const std::wstring& functionName, const std::wstring& filename, int lineNumber);
std::wstring ToString()const;
HRESULT ErrorCode = S_OK;
std::wstring FunctionName;
std::wstring Filename;
int LineNumber = -1;
};
inline std::wstring AnsiToWString(const std::string& str)
{
WCHAR buffer[512];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, buffer, 512);
return std::wstring(buffer);
}
#ifndef ThrowIfFailed
#define ThrowIfFailed(x) \
{ \
HRESULT hr__ = (x); \
std::wstring wfn = dout::AnsiToWString(__FILE__); \
if(FAILED(hr__)) { throw dout::DxException(hr__, L#x, wfn, __LINE__); } \
}
#endif
}