Skip to content

Commit

Permalink
Improve error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
neico committed Jan 11, 2025
1 parent 970f57b commit 17ddc30
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 55 deletions.
36 changes: 36 additions & 0 deletions BrowEdit3.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<assemblyIdentity
type="win32"
name="borf.BrowEdit3"
version="3.0.0.0"
processorArchitecture="*"
/>
<description>BrowEdit 3</description>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 and Windows 11 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
</application>
</compatibility>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<asmv3:application>
<asmv3:windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
<!-- <ws2:longPathAware>true</ws2:longPathAware> -->
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
5 changes: 5 additions & 0 deletions BrowEdit3.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<ClCompile Include="browedit\ModelEditor.cpp" />
<ClCompile Include="browedit\Node.cpp" />
<ClCompile Include="browedit\NodeRenderer.cpp" />
<ClCompile Include="browedit\util\Console.cpp" />
<ClCompile Include="browedit\util\FileIO.cpp" />
<ClCompile Include="browedit\util\Util.cpp" />
<ClCompile Include="browedit\windows\CinematicModeWindow.cpp" />
Expand Down Expand Up @@ -204,6 +205,7 @@
<ClInclude Include="browedit\shaders\RsmShader.h" />
<ClInclude Include="browedit\shaders\SimpleShader.h" />
<ClInclude Include="browedit\shaders\WaterShader.h" />
<ClInclude Include="browedit\util\Console.h" />
<ClInclude Include="browedit\util\FileIO.h" />
<ClInclude Include="browedit\util\glfw_keycodes_to_string.h" />
<ClInclude Include="browedit\util\ResourceManager.h" />
Expand Down Expand Up @@ -271,6 +273,9 @@
<None Include="docs\ObjectEdit.md" />
<None Include="docs\Readme.md" />
</ItemGroup>
<ItemGroup>
<Manifest Include="BrowEdit3.manifest" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
Expand Down
11 changes: 11 additions & 0 deletions BrowEdit3.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@
<ClCompile Include="browedit\actions\LightmapChangeAction.cpp">
<Filter>browedit\actions</Filter>
</ClCompile>
<ClCompile Include="browedit\util\Console.cpp">
<Filter>browedit\util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="lib\imgui\imgui.h">
Expand Down Expand Up @@ -747,6 +750,9 @@
<ClInclude Include="browedit\actions\LightmapChangeAction.h">
<Filter>browedit\actions</Filter>
</ClInclude>
<ClInclude Include="browedit\util\Console.h">
<Filter>browedit\util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="BrowEdit3.rc">
Expand Down Expand Up @@ -821,4 +827,9 @@
<Filter>docs\formats</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Manifest Include="BrowEdit3.manifest">
<Filter>Resource Files</Filter>
</Manifest>
</ItemGroup>
</Project>
111 changes: 60 additions & 51 deletions browedit/BrowEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <browedit/components/RsmRenderer.h>
#include <browedit/util/FileIO.h>
#include <browedit/util/Util.h>
#include <browedit/util/Console.h>
#include <browedit/util/ResourceManager.h>
#include <browedit/util/glfw_keycodes_to_string.h>

Expand Down Expand Up @@ -62,6 +63,8 @@ int main()
SetupExceptionHandler();
#endif

ConsoleInject consoleInjector;

std::cout << R"V0G0N( ';cllllllc:,
,lodddddddddddoc,
cdddddddddddoddddo:
Expand Down Expand Up @@ -1019,65 +1022,71 @@ void BrowEdit::ShowNewMapPopup()

void fixEffectPreviews()
{
for (auto entry : std::filesystem::directory_iterator("data\\texture\\effect"))
try
{
if (entry.path().string().find(".png") != std::string::npos)
continue;
if (entry.path().string().find(".gif") == std::string::npos)
continue;
std::istream* is = util::FileIO::open(entry.path().string());
std::cout << entry.path().string() << std::endl;
if (!is)
for (auto entry : std::filesystem::directory_iterator("data\\texture\\effect", std::filesystem::directory_options::follow_directory_symlink | std::filesystem::directory_options::skip_permission_denied))
{
std::cerr << "Texture: Could not open " <<entry.path().string() << std::endl;
return;
}
is->seekg(0, std::ios_base::end);
std::size_t len = is->tellg();
if (len <= 0 || len > 100 * 1024 * 1024)
{
std::cerr << "Texture: Error opening texture " << entry.path().string() << ", file is either empty or too large" << std::endl;
if (entry.path().string().find(".png") != std::string::npos)
continue;
if (entry.path().string().find(".gif") == std::string::npos)
continue;
std::istream* is = util::FileIO::open(entry.path().string());
std::cout << entry.path().string() << std::endl;
if (!is)
{
std::cerr << "Texture: Could not open " <<entry.path().string() << std::endl;
return;
}
is->seekg(0, std::ios_base::end);
std::size_t len = is->tellg();
if (len <= 0 || len > 100 * 1024 * 1024)
{
std::cerr << "Texture: Error opening texture " << entry.path().string() << ", file is either empty or too large" << std::endl;
delete is;
return;
}
char* buffer = new char[len];
is->seekg(0, std::ios_base::beg);
is->read(buffer, len);
delete is;
return;
}
char* buffer = new char[len];
is->seekg(0, std::ios_base::beg);
is->read(buffer, len);
delete is;

int width, height, frameCount, comp;
int* delays;
auto data = stbi_load_gif_from_memory((stbi_uc*)buffer, (int)len, &delays, &width, &height, &frameCount, &comp, 4);
if (!data)
continue;
int bestFrame = 0;
int bestFrameCount = 0;
for (int f = 0; f < frameCount; f++)
{
auto d = data + (width * height * 4) * f;
int frameIntensity = 0;
for (int x = 0; x < width; x++)

int width, height, frameCount, comp;
int* delays;
auto data = stbi_load_gif_from_memory((stbi_uc*)buffer, (int)len, &delays, &width, &height, &frameCount, &comp, 4);
if (!data)
continue;
int bestFrame = 0;
int bestFrameCount = 0;
for (int f = 0; f < frameCount; f++)
{
for (int y = 0; y < height; y++)
auto d = data + (width * height * 4) * f;
int frameIntensity = 0;
for (int x = 0; x < width; x++)
{
frameIntensity += d[(x + width * y) * 4 + 0];
frameIntensity += d[(x + width * y) * 4 + 1];
frameIntensity += d[(x + width * y) * 4 + 2];
for (int y = 0; y < height; y++)
{
frameIntensity += d[(x + width * y) * 4 + 0];
frameIntensity += d[(x + width * y) * 4 + 1];
frameIntensity += d[(x + width * y) * 4 + 2];
}
}
if (frameIntensity > bestFrameCount)
{
bestFrameCount = frameIntensity;
bestFrame = f;
}
}
if (frameIntensity > bestFrameCount)
{
bestFrameCount = frameIntensity;
bestFrame = f;
}
}
stbi_write_png((entry.path().string() + ".png").c_str(), width, height, 4, data + bestFrame * width * height*4, 0);
stbi_write_png((entry.path().string() + ".png").c_str(), width, height, 4, data + bestFrame * width * height*4, 0);

std::cout << "Best frame is " << bestFrame << std::endl;
std::cout << "Best frame is " << bestFrame << std::endl;


stbi_image_free(data);
stbi_image_free(data);
}
std::cout << "Done" << std::endl;
}
std::cout << "Done" << std::endl;

}
catch (const std::system_error& exception)
{
std::cerr << "data\\texture\\effect: " << exception.what() << " (" << exception.code() << ")" << std::endl;
}
}
144 changes: 144 additions & 0 deletions browedit/util/Console.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include "Console.h"
#include <iostream>
#include <VersionHelpers.h>

ConsoleInject::ConsoleInject() :
#ifdef _WIN32
#ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING
ansiEscape(IsWindows10OrGreater()),
#else
ansiEscape(false),
#endif

errorHandle(nullptr),

consoleInputCP(CP_NONE),
consoleOutputCP(CP_NONE),
#else
ansiEscape(true),
#endif
errorSink(ansiEscape)
{
#ifdef _WIN32
errorHandle = GetStdHandle(STD_ERROR_HANDLE);

consoleInputCP = GetConsoleCP();
consoleOutputCP = GetConsoleOutputCP();

SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);

if (ansiEscape)
{
#ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING
DWORD mode = 0;
/*
auto input = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(input, &mode);
SetConsoleMode(input, mode | ENABLE_VIRTUAL_TERMINAL_INPUT);
*/

auto output = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(output, &mode);
SetConsoleMode(output, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

GetConsoleMode(errorHandle, &mode);
SetConsoleMode(errorHandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#else
// ToDo: check for ANSICON
// #error ANSI escape sequences are only supported on Windows 10+, please install ANSICON (ANSI.sys)
#endif
}
#endif
}

ConsoleInject::~ConsoleInject()
{
#ifdef _WIN32
SetConsoleCP(consoleInputCP);
SetConsoleOutputCP(consoleOutputCP);
#endif
}

ConsoleInject::Sink::Sink(bool ansiEscape) :
ansiEscape(ansiEscape),
newLine(true),

sink(),

#ifdef _WIN32
consoleAttributes(0),
consoleForgroundMask(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY),
consoleBackgroundMask(BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY)
#endif
{
#ifdef _WIN32
errorHandle = GetStdHandle(STD_ERROR_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info = {};
GetConsoleScreenBufferInfo(errorHandle, &info);

consoleAttributes = info.wAttributes;
#endif
}

ConsoleInject::ErrorSink::ErrorSink(bool ansiEscape) : Sink(ansiEscape)
{
sink = std::cerr.rdbuf(this);
}

ConsoleInject::ErrorSink::~ErrorSink()
{
std::cerr.rdbuf(sink);
}

auto ConsoleInject::ErrorSink::overflow(int_type ch) -> int_type
{
if (traits_type::eq_int_type( ch, traits_type::eof()))
return sink->pubsync() == -1 ? ch : traits_type::not_eof(ch);

if (newLine)
{
if (ansiEscape)
{
std::ostream str(sink);

if (!(str << "\x1b[31;1m"))
return traits_type::eof();
}
else
{
#ifdef _WIN32
auto attributes = consoleAttributes & ~(consoleForgroundMask | consoleBackgroundMask);
SetConsoleTextAttribute(errorHandle, attributes | FOREGROUND_RED | FOREGROUND_INTENSITY);
#endif
}
}

newLine = traits_type::to_char_type(ch) == '\n';

if (newLine)
{
if (ansiEscape)
{
std::ostream str(sink);

if (!(str << "\x1b[0m"))
return traits_type::eof();
}
else
{
#ifdef _WIN32
SetConsoleTextAttribute(errorHandle, consoleAttributes);
#endif
}
}

auto out = sink->sputc(ch);
if (out == traits_type::eof())
{
// ToDo: fix up unknown characters
return sink->sputc('_');
}

return out;
}
Loading

0 comments on commit 17ddc30

Please sign in to comment.