Create helper function for printing the latest error on Windows

This commit is contained in:
Aaro Altonen 2019-07-16 11:42:33 +03:00
parent b919574be9
commit 4e54a0a8d2
4 changed files with 27 additions and 11 deletions

View File

@ -18,6 +18,20 @@ inline const char *className(const std::string& prettyFunction)
}
#ifdef _WIN32
static inline void win_get_last_error(void)
{
wchar_t *s = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&s, 0, NULL
);
fprintf(stderr, "%S %d\n", s, WSAGetLastError());
LocalFree(s);
}
#define LOG_LEVEL_ERROR "ERROR"
#define LOG_LEVEL_WARN "WARNING"
#define LOG_LEVEL_INFO "INFO"

View File

@ -19,9 +19,8 @@ std::string kvz_rtp::hostname::get_hostname()
char buffer[NAME_MAXLEN];
DWORD bufCharCount = NAME_MAXLEN;
if(!GetComputerName((TCHAR *)buffer, &bufCharCount)) {
LOG_ERROR("Failed to get computer name!");
return "";
if (!GetComputerName((TCHAR *)buffer, &bufCharCount)) {
win_get_last_error();
}
return std::string(buffer);
@ -43,8 +42,8 @@ std::string kvz_rtp::hostname::get_username()
char buffer[NAME_MAXLEN];
DWORD bufCharCount = NAME_MAXLEN;
if(!GetUserName((TCHAR *)buffer, &bufCharCount)) {
LOG_ERROR("Failed to get user name!");
if (!GetUserName((TCHAR *)buffer, &bufCharCount)) {
win_get_last_error();
return "";
}

View File

@ -16,8 +16,7 @@ kvz_rtp::context::context()
int rc;
if ((rc = WSAStartup(MAKEWORD(2, 2), &wsd)) != 0) {
LOG_ERROR("Unable to load Winsock: %d\n", rc);
/* TODO: how to stop everything?? */
win_get_last_error();
}
#endif
}

View File

@ -39,7 +39,7 @@ rtp_error_t kvz_rtp::socket::init(short family, int type, int protocol)
#ifdef _WIN32
if ((socket_ = ::socket(family, type, protocol)) == INVALID_SOCKET) {
LOG_ERROR("todo windows specific error message");
win_get_last_error();
#else
if ((socket_ = ::socket(family, type, protocol)) < 0) {
LOG_ERROR("Failed to create socket: %s", strerror(errno));
@ -67,6 +67,11 @@ rtp_error_t kvz_rtp::socket::bind(short family, unsigned host, short port)
sockaddr_in addr = create_sockaddr(family, host, port);
if (::bind(socket_, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
#ifdef _WIN32
win_get_last_error();
#else
fprintf(stderr, "%s\n", strerror(errno));
#endif
LOG_ERROR("Biding to port %u failed!", port);
return RTP_BIND_ERROR;
}
@ -134,8 +139,7 @@ rtp_error_t kvz_rtp::socket::__sendto(sockaddr_in& addr, uint8_t *buf, size_t bu
data_buf.len = buf_len;
if (WSASendTo(socket_, &data_buf, 1, &sent_bytes, flags, (const struct sockaddr *)&addr, sizeof(addr_), NULL, NULL) == -1) {
/* TODO: winsock specific error message */
LOG_ERROR("Failed to send data!");
win_get_last_error();
if (bytes_sent)
*bytes_sent = -1;
@ -192,7 +196,7 @@ rtp_error_t kvz_rtp::socket::__recvfrom(uint8_t *buf, size_t buf_len, int flags,
int32_t ret = ::recvfrom(socket_, (char *)buf, buf_len, flags, (SOCKADDR *)sender, (int *)len_ptr);
if (ret == -1) {
LOG_ERROR("recvfrom failed: %d", WSAGetLastError());
win_get_last_error();
if (bytes_read)
*bytes_read = -1;