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
|
#include "Common.h"
#include "Output.h"
#ifdef WIN32
#include <Windows.h>
#include <winsock.h>
#pragma comment(lib, "Ws2_32.lib")
#endif
[[noreturn]] void PrintErrorMessageAndExit(StringView message,
bool print_last_error) {
SendOutput(CRUT("{}\n"), message);
if (print_last_error) {
#ifdef WIN32
auto error_code = WSAGetLastError();
SendOutput(OutputType::Error, CRUT("Error code is {}.\n"), error_code);
wchar_t buffer[500];
if (!FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ARGUMENT_ARRAY |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, 0, buffer, 500, nullptr)) {
SendOutput(OutputType::Error, CRUT("Failed to format error message.\n"));
} else {
SendOutput(OutputType::Error, CRUT("{}\n"), buffer);
}
#else
#endif
}
#ifdef WIN32
WSACleanup();
#endif
std::exit(1);
}
#ifdef WIN32
void InitWSA() {
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data)) { // initialize wsa
PrintErrorMessageAndExit(CRUT("Failed to initialize wsa."));
}
}
#endif
|