Skip to content

Commit

Permalink
Adde platform::windows::GetLastErrorToString()
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRichards-Code committed Sep 24, 2024
1 parent c062190 commit f85ce31
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Windows/VisualStudioDebugOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class vsBufferedStringStreamBuf : public std::streambuf
class VisualStudioDebugOutput : public vsBufferedStringStreamBuf
{
public:
VisualStudioDebugOutput(bool send_to_output_window=true,
VisualStudioDebugOutput(bool send_to_output_window=true,
const char *logfilename=NULL,size_t bufsize=(size_t)16
,DebugOutputCallback c=NULL)
:vsBufferedStringStreamBuf((int)bufsize)
Expand Down Expand Up @@ -150,10 +150,10 @@ class VisualStudioDebugOutput : public vsBufferedStringStreamBuf
std::copy(str.begin(), str.end(), wstr.begin());
OutputDebugString(wstr.c_str());
#else
OutputDebugString(str.c_str());
OutputDebugString(str.c_str());
#endif
}
}
}
protected:
std::ofstream logFile;
bool to_output_window;
Expand Down
38 changes: 38 additions & 0 deletions Windows/WindowsErrorHandling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <string>

#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#endif


namespace platform::windows
{
std::string GetLastErrorToString(DWORD error)
{
#if defined(_WIN32)
if (error != 0)
{
char *formatedMessage = nullptr;
DWORD formatedMessageSize = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error, 0, (char *)&formatedMessage, 0, nullptr);

if (formatedMessage != nullptr && formatedMessageSize > 0)
{
std::string result(formatedMessage, formatedMessageSize);
LocalFree(formatedMessage);
return result;
}
}
#endif
return std::string();
}
}

0 comments on commit f85ce31

Please sign in to comment.