Added more robust logging. Put garbage collection on 3 minute interval

This commit is contained in:
Ethan Adams 2025-01-28 20:02:40 -05:00
parent b11a99b0d4
commit 169cc6d617
8 changed files with 51 additions and 36 deletions

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"files.associations": {
"memory": "cpp"
}
}

View file

@ -41,6 +41,9 @@ private:
sol::function idleFunc; sol::function idleFunc;
sol::function patrolFunc; sol::function patrolFunc;
sol::function alertFunc; sol::function alertFunc;
std::chrono::high_resolution_clock::time_point lastGCTime;
std::chrono::minutes GCTimeout;
}; };
#endif #endif

View file

@ -55,7 +55,7 @@ public:
private: private:
bool adjustCanvasSizes(std::vector<SDL_Surface*>& surfaces); bool adjustCanvasSizes(std::vector<SDL_Surface*>& surfaces);
int numOfLayers; size_t numOfLayers;
unsigned ID = 0; unsigned ID = 0;
std::vector<TextureData*> textures; std::vector<TextureData*> textures;
int canvasWidth = 0; int canvasWidth = 0;

View file

@ -25,6 +25,9 @@ public:
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
auto msg = formatString(message, args...); auto msg = formatString(message, args...);
std::cout std::cout
<< "["
<< logTag(level)
<< "] "
<< "[" << "["
<< std::format("{:%m-%d-%Y %X}", std::chrono::current_zone()->to_local(std::chrono::system_clock::now())) << std::format("{:%m-%d-%Y %X}", std::chrono::current_zone()->to_local(std::chrono::system_clock::now()))
<< "] " << "] "
@ -52,6 +55,22 @@ protected:
return std::vformat(str, std::make_format_args(args...)); return std::vformat(str, std::make_format_args(args...));
} }
inline const std::string logTag(LogLevel lvl) const
{
switch (lvl)
{
case DEBUG:
return "DEBUG";
case INFO:
return "INFO";
case WARN:
return "WARN";
case ERROR:
return "ERROR";
}
return "INFO";
};
private: private:
static Logger* instance; static Logger* instance;
static std::mutex mutex; static std::mutex mutex;

View file

@ -6,7 +6,8 @@
AI::AI(const std::shared_ptr<GameActor>& actor, const std::shared_ptr<Raycaster>& raycaster) AI::AI(const std::shared_ptr<GameActor>& actor, const std::shared_ptr<Raycaster>& raycaster)
: actor(actor), raycaster(raycaster), state(AIState::Idle) : actor(actor), raycaster(raycaster), state(AIState::Idle),
lastGCTime(std::chrono::high_resolution_clock::now()), GCTimeout(3)
{} {}
void AI::attachBehaviourScript(const std::shared_ptr<AIScript>& behaviour) void AI::attachBehaviourScript(const std::shared_ptr<AIScript>& behaviour)
@ -64,7 +65,12 @@ void AI::update()
} }
break; break;
} }
//behaviour->lua.collect_gc(); std::chrono::high_resolution_clock::time_point curTime = std::chrono::high_resolution_clock::now();
if (curTime - lastGCTime >= GCTimeout)
{
behaviour->lua.collect_gc();
lastGCTime = curTime;
}
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error during AI update: " << e.what() << std::endl; std::cerr << "Error during AI update: " << e.what() << std::endl;

View file

@ -21,18 +21,12 @@ bool Game::init()
window = std::make_shared<GLWindow>("Yupple Mayham", 800, 600); window = std::make_shared<GLWindow>("Yupple Mayham", 800, 600);
if (!window->Init()) if (!window->Init())
{ ERROR_LOG("Failed to init GLWindow: {}", SDL_GetError());
std::cout << "Failed to init GLWindow: \n" << SDL_GetError() << std::endl;
return false;
}
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
{ ERROR_LOG("Failed to load GLLoader");
std::cout << "Failed to load GLLoader" << std::endl;
return false;
}
#if _DEBUG 1 #if _DEBUG
LOG_LEVEL(DEBUG); LOG_LEVEL(DEBUG);
#elif #elif
LOG_LEVEL(INFO); LOG_LEVEL(INFO);

View file

@ -10,6 +10,7 @@
#include "utility/events.h" #include "utility/events.h"
#include "utility/resourcemanager.h" #include "utility/resourcemanager.h"
#include "utility/script.h" #include "utility/script.h"
#include "utility/logger.h"
// TODO: Regular clean up, make this mess readable! // TODO: Regular clean up, make this mess readable!
@ -132,7 +133,7 @@ void Weapon::attachScript(const std::shared_ptr<WeaponScript>& script)
{ {
weaponScript = script; weaponScript = script;
weaponScript->lua["weapon"] = shared_from_this(); weaponScript->lua["weapon"] = shared_from_this();
std::cout << "weapon state bound" << std::endl; LOG(DEBUG, "Weapon state bound");
} }
void Weapon::onHitCallback(std::shared_ptr<GameActor> target, std::shared_ptr<PhysicsComponent> bullet, const glm::vec2& normal) void Weapon::onHitCallback(std::shared_ptr<GameActor> target, std::shared_ptr<PhysicsComponent> bullet, const glm::vec2& normal)

View file

@ -1,25 +1,24 @@
#include "graphics/texture.h" #include "graphics/texture.h"
#include "utility/logger.h"
#include "util.h" #include "util.h"
#include <SDL_image.h> #include <SDL_image.h>
#include <iostream>
#include <glad/glad.h> #include <glad/glad.h>
bool Texture::loadTexture(const char* imagePath) bool Texture::loadTexture(const char* imagePath)
{ {
SDL_Surface* buffer = IMG_Load(imagePath); SDL_Surface* buffer = IMG_Load(imagePath);
if (!buffer) if (!buffer)
{ ERROR_LOG("Failed to load image file: {}", imagePath);
std::cout << "Failed to load image file: " << imagePath << std::endl;
return false;
}
//UTIL::flip_surface(buffer); //UTIL::flip_surface(buffer);
glGenTextures(1, &ID); glGenTextures(1, &ID);
/*
GLenum error = glGetError(); GLenum error = glGetError();
if(error != GL_NO_ERROR) { if(error != GL_NO_ERROR) {
std::cout << "OpenGL error: " << error << std::endl; std::cout << "OpenGL error: " << error << std::endl;
} }
*/
glBindTexture(GL_TEXTURE_2D, ID); glBindTexture(GL_TEXTURE_2D, ID);
@ -60,22 +59,12 @@ bool TextureArray::loadTextures(std::vector<const char*> imagePaths)
{ {
surfaces[i] = IMG_Load(imagePaths[i]); surfaces[i] = IMG_Load(imagePaths[i]);
if (!surfaces[i]) if (!surfaces[i])
{ ERROR_LOG("Failed to load image file: {}", imagePaths[i]);
std::cout << "Failed to load image file: " << imagePaths[i] << std::endl;
return false;
}
} }
if (!adjustCanvasSizes(surfaces)) if (!adjustCanvasSizes(surfaces))
{ ERROR_LOG("Failed to adjust canvas size of images! \n Make sure to check that every tileset has square dimensions! (512x512, 756x756 ... etc)");
std::cout << "ERROR: Failed to adjust canvas size of images!" << std::endl
<< "Make sure to check that every tileset has square dimensions! (512x512, 756x756 ... etc)" << std::endl;
return false;
}
if (surfaces.empty()) if (surfaces.empty())
{ ERROR_LOG("No surfaces created!");
std::cout << "ERROR: No surfaces created" << std::endl;
return false;
}
numOfLayers = imagePaths.size(); numOfLayers = imagePaths.size();
glGenTextures(1, &ID); glGenTextures(1, &ID);
@ -89,7 +78,7 @@ bool TextureArray::loadTextures(std::vector<const char*> imagePaths)
GL_RGBA, GL_RGBA,
surfaces[0]->w, surfaces[0]->w,
surfaces[0]->h, surfaces[0]->h,
numOfLayers, (GLsizei)numOfLayers,
0, 0,
GL_RGBA, GL_RGBA,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
@ -134,10 +123,8 @@ bool TextureArray::adjustCanvasSizes(std::vector<SDL_Surface*>& surfaces)
for (auto& surface : surfaces) for (auto& surface : surfaces)
{ {
if (surface->w != surface->h) if (surface->w != surface->h)
{ ERROR_LOG("Image must be a square!");
std::cout << "Image must be a square!" << std::endl;
return false;
}
if (surface->w > maxWidth) maxWidth = surface->w; if (surface->w > maxWidth) maxWidth = surface->w;
if (surface->h > maxHeight) maxHeight = surface->h; if (surface->h > maxHeight) maxHeight = surface->h;
textures.push_back(new TextureData({ surface->w, surface->h })); textures.push_back(new TextureData({ surface->w, surface->h }));