Logging cleanup (#69)

* Mac OS X 10.6 & More C++03 Support

* Fix SDL2 options.txt loading for C++03

* Output/Logging Overhaul
* Added StandardOut class
* Renamed LOGX macros to LOG_X
* Removed LogMsg macros in favor of LOG_X
* Added console window for debug Windows builds

* Updated Xcode Project
+ StandardOut.hpp
+ StandardOut.cpp

* StandardOut_windows
* Replaced the Windows #ifdefs in StandardOut with StandardOut_windows

---------

Co-authored-by: Brent Da Mage <BrentDaMage@users.noreply.github.com>
This commit is contained in:
Brent
2023-08-28 02:55:41 -05:00
committed by GitHub
parent f7915a1dab
commit 5c1ea03747
43 changed files with 326 additions and 207 deletions

View File

@@ -28,6 +28,6 @@ Texture AppPlatform_emscripten::loadTexture(const std::string& path, bool b)
}
// I don't think this logic makes any sense
LogMsg("Couldn't find file: %s", realPath.c_str());
LOG_E("Couldn't find file: %s", realPath.c_str());
return out;
}

View File

@@ -352,6 +352,7 @@
849B93022A99C3D900CF0DE0 /* SoundEngine.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 849B92B72A99C3D900CF0DE0 /* SoundEngine.cpp */; };
849B93032A99C3D900CF0DE0 /* SoundRepository.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 849B92B92A99C3D900CF0DE0 /* SoundRepository.cpp */; };
849B93042A99C3D900CF0DE0 /* SoundSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 849B92BB2A99C3D900CF0DE0 /* SoundSystem.cpp */; };
84A1072A2A9B183A00850A9C /* StandardOut.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 84A107282A9B183A00850A9C /* StandardOut.cpp */; };
84C4D86F2A872C0100323E33 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 84C4D86E2A872C0100323E33 /* OpenAL.framework */; };
/* End PBXBuildFile section */
@@ -1079,6 +1080,8 @@
849B92BB2A99C3D900CF0DE0 /* SoundSystem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SoundSystem.cpp; sourceTree = "<group>"; };
849B92BC2A99C3D900CF0DE0 /* SoundSystem.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SoundSystem.hpp; sourceTree = "<group>"; };
849B930F2A99C44A00CF0DE0 /* GameMods.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = GameMods.hpp; path = ../../../GameMods.hpp; sourceTree = SOURCE_ROOT; };
84A107282A9B183A00850A9C /* StandardOut.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StandardOut.cpp; sourceTree = "<group>"; };
84A107292A9B183A00850A9C /* StandardOut.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = StandardOut.hpp; sourceTree = "<group>"; };
84C4D86E2A872C0100323E33 /* OpenAL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenAL.framework; path = /System/Library/Frameworks/OpenAL.framework; sourceTree = "<absolute>"; };
/* End PBXFileReference section */
@@ -1442,6 +1445,8 @@
8443B6942A9865110086730C /* Options.hpp */,
8443B6952A9865110086730C /* Random.cpp */,
8443B6962A9865110086730C /* Random.hpp */,
84A107282A9B183A00850A9C /* StandardOut.cpp */,
84A107292A9B183A00850A9C /* StandardOut.hpp */,
8443B6972A9865110086730C /* Timer.cpp */,
8443B6982A9865110086730C /* Timer.hpp */,
8443B6992A9865110086730C /* Util.cpp */,
@@ -2523,6 +2528,7 @@
849B93022A99C3D900CF0DE0 /* SoundEngine.cpp in Sources */,
849B93032A99C3D900CF0DE0 /* SoundRepository.cpp in Sources */,
849B93042A99C3D900CF0DE0 /* SoundSystem.cpp in Sources */,
84A1072A2A9B183A00850A9C /* StandardOut.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -10,7 +10,7 @@ SoundSystemAL::SoundSystemAL()
device = alcOpenDevice(NULL);
if (!device)
{
LogMsg("Unable To Load Audio Engine");
LOG_E("Unable To Load Audio Engine");
return;
}
@@ -19,7 +19,7 @@ SoundSystemAL::SoundSystemAL()
ALCenum err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Open Audio Context: %s", alcGetString(device, err));
LOG_E("Unable To Open Audio Context: %s", alcGetString(device, err));
return;
}
@@ -28,7 +28,7 @@ SoundSystemAL::SoundSystemAL()
err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Select Audio Context: %s", alcGetString(device, err));
LOG_E("Unable To Select Audio Context: %s", alcGetString(device, err));
return;
}
@@ -57,7 +57,7 @@ SoundSystemAL::~SoundSystemAL()
ALCenum err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Deselect Audio Context: %s", alcGetString(device, err));
LOG_E("Unable To Deselect Audio Context: %s", alcGetString(device, err));
}
// Destroy Context
@@ -65,7 +65,7 @@ SoundSystemAL::~SoundSystemAL()
err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Destroy Audio Context: %s", alcGetString(device, err));
LOG_E("Unable To Destroy Audio Context: %s", alcGetString(device, err));
}
// Close Device
@@ -74,7 +74,7 @@ SoundSystemAL::~SoundSystemAL()
/*err = alcGetError(device);
if (err != ALC_NO_ERROR)
{
LogMsg("Unable To Close Audio Device: %s", alcGetString(device, err));
LOG_E("Unable To Close Audio Device: %s", alcGetString(device, err));
}*/
}
@@ -85,7 +85,7 @@ SoundSystemAL::~SoundSystemAL()
ALenum __err = val; \
if (__err != AL_NO_ERROR) \
{ \
LogMsg("(%s:%i) OpenAL Error: %s", __FILE__, __LINE__, alGetString(__err)); \
LOG_E("(%s:%i) OpenAL Error: %s", __FILE__, __LINE__, alGetString(__err)); \
exit(EXIT_FAILURE); \
} \
}

View File

@@ -107,7 +107,7 @@ void AppPlatform_sdl::ensureDirectoryExists(const char* path)
if (ret != 0)
{
// Unable To Create Folder
LogMsg("Error Creating Directory: %s: %s", path, strerror(errno));
LOG_E("Error Creating Directory: %s: %s", path, strerror(errno));
exit(EXIT_FAILURE);
}
}
@@ -177,11 +177,11 @@ void AppPlatform_sdl::saveScreenshot(const std::string& filename, int glWidth, i
// Save Image
if (fail || save_png(file.c_str(), pixels, line_size, width, height))
{
LogMsg("Screenshot Failed: %s", file.c_str());
LOG_E("Screenshot Failed: %s", file.c_str());
}
else
{
LogMsg("Screenshot Saved: %s", file.c_str());
LOG_I("Screenshot Saved: %s", file.c_str());
}
// Free
@@ -284,8 +284,8 @@ Texture AppPlatform_sdl::loadTexture(const std::string& path, bool b)
SDL_RWclose(io);
}
// TODO: I don't think this logic makes any sense
LogMsg("Couldn't find file: %s", path.c_str());
// TODO: I don't think this logic makes any sense
LOG_E("Couldn't find file: %s", path.c_str());
return out;
}
@@ -299,12 +299,12 @@ std::vector<std::string> AppPlatform_sdl::getOptionStrings()
// TODO: This isn't specific to SDL2. Why isn't it in an AppPlatform base class?
std::vector<std::string> o;
LogMsg("Storage dir is %s", _storageDir.c_str());
LOG_I("Storage dir is %s", _storageDir.c_str());
std::ifstream ifs(getOptionsFilePath().c_str());
if (!ifs.is_open())
{
LogMsg("Warning, options.txt doesn't exist, resetting to defaults");
LOG_W("options.txt doesn't exist, resetting to defaults");
return o;
}
@@ -340,7 +340,7 @@ void AppPlatform_sdl::setOptionStrings(const std::vector<std::string>& str)
os.open(getOptionsFilePath().c_str());
if (!os.is_open())
{
LogMsg("Error, options.txt can't be opened");
LOG_E("Failed to read options.txt");
return;
}

View File

@@ -56,7 +56,7 @@ SDL_Surface* AppPlatform_sdlbase::getSurfaceForTexture(const Texture* const text
0xFF000000
);
if (!surface)
LogMsg("Error loading SDL_Surface from Texture: %s", SDL_GetError());
LOG_E("Failed loading SDL_Surface from Texture: %s", SDL_GetError());
return surface;
}

View File

@@ -26,27 +26,6 @@ typedef AppPlatform_sdl UsedAppPlatform;
static float g_fPointToPixelScale = 1.0f;
void LogMsg(const char* fmt, ...)
{
va_list lst;
va_start(lst, fmt);
vprintf(fmt, lst);
printf("\n");
va_end(lst);
}
// I hate duplicating code, but yeah
void LogMsgNoCR(const char* fmt, ...)
{
va_list lst;
va_start(lst, fmt);
vprintf(fmt, lst);
va_end(lst);
}
UsedAppPlatform *g_pAppPlatform;
NinecraftApp *g_pApp;
@@ -262,7 +241,7 @@ int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
LOGE("Unable To Initialize SDL: %s\n", SDL_GetError());
LOG_E("Unable To Initialize SDL: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
@@ -290,7 +269,7 @@ int main(int argc, char *argv[])
window = SDL_CreateWindow("ReMinecraftPE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Minecraft::width, Minecraft::height, flags);
if (!window)
{
LOGE("Unable to create SDL window\n");
LOG_E("Unable to create SDL window\n");
exit(EXIT_FAILURE);
}
@@ -301,7 +280,7 @@ int main(int argc, char *argv[])
context = SDL_GL_CreateContext(window);
if (!context)
{
LOGE("Unable to create OpenGL context\n");
LOG_E("Unable to create OpenGL context\n");
exit(EXIT_FAILURE);
}

View File

@@ -12,6 +12,7 @@
#include <shlobj.h>
#include "AppPlatform_windows.hpp"
#include "StandardOut_windows.hpp"
#include "thirdparty/stb_image.h"
#include "thirdparty/stb_image_write.h"
@@ -31,6 +32,15 @@ AppPlatform_windows::AppPlatform_windows()
m_bShiftPressed = false;
m_MouseDiffX = 0, m_MouseDiffY = 0;
// This initializes the StandardOut singleton to use the Windows-specific variant
// If we didn't initialize it here, the Minecraft class would have our back
m_standardOut = new StandardOut_windows();
}
AppPlatform_windows::~AppPlatform_windows()
{
SAFE_DELETE(m_standardOut);
}
int AppPlatform_windows::checkLicense()
@@ -137,7 +147,7 @@ Texture AppPlatform_windows::loadTexture(const std::string& str, bool b)
FILE* f = fopen(realPath.c_str(), "rb");
if (!f)
{
LogMsg("File %s couldn't be opened", realPath.c_str());
LOG_E("File %s couldn't be opened", realPath.c_str());
_error:
const std::string msg = "Error loading " + realPath + ". Did you unzip the Minecraft assets?";
@@ -153,7 +163,7 @@ Texture AppPlatform_windows::loadTexture(const std::string& str, bool b)
stbi_uc* img = stbi_load_from_file(f, &width, &height, &channels, STBI_rgb_alpha);
if (!img)
{
LogMsg("File %s couldn't be loaded via stb_image", realPath.c_str());
LOG_E("File %s couldn't be loaded via stb_image", realPath.c_str());
goto _error;
}

View File

@@ -16,11 +16,13 @@
#include "client/player/input/Mouse.hpp"
#include "client/player/input/Keyboard.hpp"
#include "common/Utils.hpp"
#include "StandardOut_windows.hpp"
class AppPlatform_windows : public AppPlatform
{
public:
AppPlatform_windows();
~AppPlatform_windows();
void buyGame() override;
void saveScreenshot(const std::string& fileName, int width, int height) override;
int checkLicense() override;
@@ -75,5 +77,7 @@ private:
bool m_bShiftPressed;
int m_MouseDiffX, m_MouseDiffY;
StandardOut_windows *m_standardOut;
};

View File

@@ -14,7 +14,7 @@
SoundSystemWindows::SoundSystemWindows()
{
printf("Init SoundSystemWindows\n");
LOG_I("Init SoundSystemWindows");
HRESULT result;
DSBUFFERDESC bufferDesc;
@@ -24,14 +24,14 @@ SoundSystemWindows::SoundSystemWindows()
result = DirectSoundCreate8(NULL, &m_directsound, NULL);
if (FAILED(result))
{
printf("SoundSystemWindows failed to create directsound8 handle\n");
LOG_E("SoundSystemWindows failed to create directsound8 handle");
return;
}
result = m_directsound->SetCooperativeLevel(GetHWND(), DSSCL_NORMAL);
if (FAILED(result))
{
printf("SoundSystemWindows failed set cooperation level\n");
LOG_E("SoundSystemWindows failed set cooperation level");
return;
}
@@ -47,7 +47,7 @@ SoundSystemWindows::SoundSystemWindows()
result = m_directsound->CreateSoundBuffer(&bufferDesc, &primaryBuffer, NULL);
if (FAILED(result))
{
printf("SoundSystemWindows failed to create primary sound buffer\n");
LOG_E("SoundSystemWindows failed to create primary sound buffer");
return;
}
@@ -57,7 +57,7 @@ SoundSystemWindows::SoundSystemWindows()
if (FAILED(result))
{
printf("SoundSystemWindows failed to create 3D listener\n");
LOG_E("SoundSystemWindows failed to create 3D listener\n");
}
m_available = true;
@@ -67,7 +67,7 @@ SoundSystemWindows::SoundSystemWindows()
SoundSystemWindows::~SoundSystemWindows()
{
printf("Destroying SoundSystemWindows\n");
LOG_I("Destroying SoundSystemWindows");
if (!isAvailable())
{
@@ -203,7 +203,7 @@ void SoundSystemWindows::playAt(const SoundDesc& sound, float x, float y, float
result = m_directsound->CreateSoundBuffer(&bufferDesc, &tempBuffer, NULL);
if (FAILED(result))
{
printf("SoundSystemWindows CreateSoundBuffer failed\n");
LOG_E("SoundSystemWindows CreateSoundBuffer failed");
return;
}
@@ -211,7 +211,7 @@ void SoundSystemWindows::playAt(const SoundDesc& sound, float x, float y, float
result = tempBuffer->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)&soundbuffer);
if (FAILED(result))
{
printf("SoundSystemWindows tempBuffer QueryInterface failed\n");
LOG_E("SoundSystemWindows tempBuffer QueryInterface failed");
return;
}
@@ -224,7 +224,7 @@ void SoundSystemWindows::playAt(const SoundDesc& sound, float x, float y, float
result = soundbuffer->Lock(0, length, (void**)&bufferPtr, (DWORD*)&bufferSize, NULL, 0, 0);
if (FAILED(result))
{
printf("SoundSystemWindows lock failed\n");
LOG_E("SoundSystemWindows lock failed");
return;
//return false;
}
@@ -236,7 +236,7 @@ void SoundSystemWindows::playAt(const SoundDesc& sound, float x, float y, float
result = soundbuffer->Unlock((void*)bufferPtr, bufferSize, NULL, 0);
if (FAILED(result))
{
printf("SoundSystemWindows unlock failed\n");
LOG_E("SoundSystemWindows unlock failed");
return;
}
@@ -275,7 +275,7 @@ void SoundSystemWindows::playAt(const SoundDesc& sound, float x, float y, float
HRESULT hr = soundbuffer->QueryInterface(IID_IDirectSound3DBuffer8,
(LPVOID*)&object3d);
if (FAILED(hr)) {
printf("SoundSystemWindows QueryInterface failed for 3D Object\n");
LOG_E("SoundSystemWindows QueryInterface failed for 3D Object");
return;
}

View File

@@ -0,0 +1,34 @@
#include <iostream>
#include <stdarg.h>
#include <windows.h>
#include "StandardOut_windows.hpp"
#include "common/Util.hpp"
void StandardOut_windows::print(const char* const str)
{
StandardOut::print(str);
OutputDebugStringA(str);
OutputDebugStringA("\n");
}
void StandardOut_windows::print(std::string str)
{
print(str.c_str());
}
void StandardOut_windows::vprintf(const char* const fmt, va_list argPtr)
{
print(Util::vformat(fmt, argPtr));
}
void StandardOut_windows::printf(const char* const fmt, ...)
{
va_list argList;
va_start(argList, fmt);
vprintf(fmt, argList);
va_end(argList);
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <string>
#include "common/StandardOut.hpp"
class StandardOut_windows : StandardOut
{
void print(const char* const str) override;
void print(std::string str) override;
void vprintf(const char* const fmt, va_list argPtr) override;
void printf(const char* const fmt, ...) override;
};

View File

@@ -21,45 +21,6 @@ LPCTSTR g_WindowClassName = TEXT("MCPEClass");
AppPlatform_windows g_AppPlatform;
NinecraftApp* g_pApp;
void LogMsg(const char* fmt, ...)
{
va_list lst;
va_start(lst, fmt);
#ifdef _WIN32
char buf[10240];
vsnprintf(buf, sizeof buf, fmt, lst);
buf[sizeof buf - 1] = 0;
OutputDebugStringA(buf);
OutputDebugStringA("\n");
#else
vfprintf(stderr, fmt, lst);
fprintf(stderr, "\n");
#endif
va_end(lst);
}
// I hate duplicating code, but yeah
void LogMsgNoCR(const char* fmt, ...)
{
va_list lst;
va_start(lst, fmt);
#ifdef _WIN32
char buf[10240];
vsnprintf(buf, sizeof buf, fmt, lst);
buf[sizeof buf - 1] = 0;
OutputDebugStringA(buf);
#else
vfprintf(stderr, fmt, lst);
#endif
va_end(lst);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
@@ -162,6 +123,15 @@ void CheckOptionalTextureAvailability()
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
#ifdef _DEBUG
AllocConsole();
FILE* ostream;
FILE* istream;
freopen_s(&ostream, "CONOUT$", "w", stdout);
freopen_s(&istream, "CONIN$", "r", stdin);
SetConsoleTitle("Minecraft C++ Debug Console");
#endif
SetInstance(hInstance);
// register the window class:

View File

@@ -381,8 +381,9 @@
<ClInclude Include="$(MC_ROOT)\thirdparty\zlib\zconf.h" />
<ClInclude Include="$(MC_ROOT)\thirdparty\zlib\zlib.h" />
<ClInclude Include="$(MC_ROOT)\thirdparty\zlib\zutil.h" />
<ClInclude Include="..\..\..\compat\KeyCodes.hpp" />
<ClInclude Include="..\..\..\compat\SDLKeyCodes.h" />
<ClInclude Include="$(MC_ROOT)\compat\KeyCodes.hpp" />
<ClInclude Include="$(MC_ROOT)\compat\SDLKeyCodes.h" />
<ClInclude Include="$(MC_ROOT)\source\common\StandardOut.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MC_ROOT)\compat\GLExt.cpp" />
@@ -390,6 +391,7 @@
<ClCompile Include="$(MC_ROOT)\platforms\windows\AppPlatform_windows.cpp" />
<ClCompile Include="$(MC_ROOT)\platforms\windows\main.cpp" />
<ClCompile Include="$(MC_ROOT)\platforms\windows\SoundSystemWindows.cpp" />
<ClInclude Include="$(MC_ROOT)\platforms\windows\StandardOut_windows.hpp" />
<ClCompile Include="$(MC_ROOT)\source\App.cpp" />
<ClCompile Include="$(MC_ROOT)\source\AppPlatform.cpp" />
<ClCompile Include="$(MC_ROOT)\source\client\gui\components\AvailableGamesList.cpp" />
@@ -727,6 +729,8 @@
<ClCompile Include="$(MC_ROOT)\thirdparty\zlib\trees.c" />
<ClCompile Include="$(MC_ROOT)\thirdparty\zlib\uncompr.c" />
<ClCompile Include="$(MC_ROOT)\thirdparty\zlib\zutil.c" />
<ClCompile Include="..\..\..\source\common\StandardOut.cpp" />
<ClCompile Include="..\StandardOut_windows.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="$(MC_ROOT)\thirdparty\raknet\CMakeLists.txt" />

View File

@@ -103,6 +103,9 @@
<Filter Include="source\platforms\windows">
<UniqueIdentifier>{27e531b5-8c5e-4fcc-aa64-b1f364da0746}</UniqueIdentifier>
</Filter>
<Filter Include="source\platforms\base">
<UniqueIdentifier>{43e2c12c-cafd-47ac-be4b-3689b4c53d30}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(MC_ROOT)\thirdparty\raknet\ThreadsafePacketLogger.h">
@@ -1173,6 +1176,12 @@
<ClInclude Include="..\..\..\compat\SDLKeyCodes.h">
<Filter>thirdparty</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\common\StandardOut.hpp">
<Filter>source\common</Filter>
</ClInclude>
<ClInclude Include="..\StandardOut_windows.hpp">
<Filter>source\platforms\windows</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MC_ROOT)\thirdparty\raknet\TwoWayAuthentication.cpp">
@@ -2201,6 +2210,12 @@
<ClCompile Include="$(MC_ROOT)\platforms\windows\SoundSystemWindows.cpp">
<Filter>source\platforms\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\common\StandardOut.cpp">
<Filter>source\common</Filter>
</ClCompile>
<ClCompile Include="..\StandardOut_windows.cpp">
<Filter>source\platforms\windows</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="$(MC_ROOT)\thirdparty\raknet\CMakeLists.txt">