From b30611da353194eee9b670b2b382a6c436ddd69c Mon Sep 17 00:00:00 2001 From: /nick haya <74699483+The-SGPT@users.noreply.github.com> Date: Thu, 10 Feb 2022 13:50:46 +0800 Subject: [PATCH] Redo logging No, im not gonna use std::source_location. It's useful, but not that good Might as well use __FILE__ and __LINE__ and pass them on as parameters....for now. --- src/Render.cpp | 78 ++++++++++++++------------------------------------ src/Render.hpp | 39 ++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/Render.cpp b/src/Render.cpp index 926d410..3f6bf3b 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -216,41 +216,41 @@ bool Render::Init(string window_name) { } SOUNDFONT = tomlParse("conf.toml", "config", "soundfont"); if (se.init() > 0) { - log("Render.cpp", 202, "SoLoud", " has failed to load. Is your dll broken?", ERROR_); + log("SoLoud", " has failed to load. Is your dll broken?", ERROR_, __FILENAME__, __LINE__); return false; } if (music.init() > 0) { - log("Render.cpp", 206, "SoLoud", " has failed to load. Is your dll broken?", ERROR_); + log("SoLoud", " has failed to load. Is your dll broken?", ERROR_, __FILENAME__, __LINE__); return false; } - log("Render.cpp", 209, "SoLoud", " has been successfully initialized.", NORMAL); + log("SoLoud", " has been successfully initialized.", NORMAL, __FILENAME__, __LINE__); if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { - log("Render.cpp", 211, "SDL", " has failed to load. Is your dll broken? " + string(SDL_GetError()), ERROR_); + log("SDL", " has failed to load. Is your dll broken? " + string(SDL_GetError()), ERROR_, __FILENAME__, __LINE__); return false; } - log("Render.cpp", 214, "SDL", " has been successfully initialized.", NORMAL); + log("SDL", " has been successfully initialized.", NORMAL, __FILENAME__, __LINE__); if (IMG_Init(IMG_INIT_PNG) == 0) { - log("Render.cpp", 216, "SDL_image", " has failed to load. Is your dll broken? " + string(SDL_GetError()), ERROR_); + log("SDL_image", " has failed to load. Is your dll broken? " + string(SDL_GetError()), ERROR_, __FILENAME__, __LINE__); return false; } - log("Render.cpp", 219, "SDL_image", " has been successfully initialized.", NORMAL); + log("SDL_image", " has been successfully initialized.", NORMAL, __FILENAME__, __LINE__); if (TTF_Init() < 0) { - log("Render.cpp", 221, "SDL_ttf", " has failed to load. Is your dll broken? " + string(SDL_GetError()), ERROR_); + log("SDL_ttf", " has failed to load. Is your dll broken? " + string(SDL_GetError()), ERROR_, __FILENAME__, __LINE__); return false; } - log("Render.cpp", 224, "SDL_ttf", " has been successfully initialized.", NORMAL); + log("SDL_ttf", " has been successfully initialized.", NORMAL, __FILENAME__, __LINE__); window = SDL_CreateWindow(window_name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN); if (window == nullptr) { - log("Render.cpp", 227, "A window", " failed to be created. " + string(SDL_GetError()), ERROR_); + log("A window", " failed to be created. " + string(SDL_GetError()), ERROR_, __FILENAME__, __LINE__); return false; } - log("Render.cpp", 230, "A window", " has been created.", NORMAL); + log("A window", " has been created.", NORMAL, __FILENAME__, __LINE__); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == nullptr) { - log("Render.cpp", 223, "A renderer", " failed to be created. " + string(SDL_GetError()), ERROR_); + log("A renderer", " failed to be created. " + string(SDL_GetError()), ERROR_, __FILENAME__, __LINE__); return false; } - log("Render.cpp", 236, "A renderer", " has been created.", NORMAL); + log("A renderer", " has been created.", NORMAL, __FILENAME__, __LINE__); current_sf.load(SOUNDFONT.c_str()); @@ -259,7 +259,7 @@ bool Render::Init(string window_name) { SDL_GetWindowWMInfo(window, &wmInfo); hwnd = wmInfo.info.win.window; - log("Render.cpp", 245, "", "Finalized initialization. Command over.", NORMAL); + log("", "Finalized initialization. Command over.", NORMAL, __FILENAME__, __LINE__); return true; } @@ -352,7 +352,7 @@ bool Render::Update() { void Render::SwitchState(State* state) { string state_name = typeid(*state).name(); - log("Render.cpp", 337, "", "Switching current state to " + state_name); + log("", "Switching current state to " + state_name, NORMAL, __FILENAME__, __LINE__); if (current_state != nullptr) { _ticks = {}; _call = {}; @@ -363,12 +363,12 @@ void Render::SwitchState(State* state) { } } current_state = state; - log("Render.cpp", 349, "Success!", " Calling Create()...."); + log("Success!", " Calling Create()....", NORMAL, __FILENAME__, __LINE__); current_state->Create(); } bool Render::playSound(string path, bool override) { - log("Render.cpp", 354, "", "Played sound from " + path); + log("", "Played sound from " + path, NORMAL, __FILENAME__, __LINE__); waveLoader.setLooping(false); if (override) { se.stop(seIndex); @@ -381,11 +381,11 @@ bool Render::playSound(string path, bool override) { bool Render::playMusic(string path) { if (path == "") { - log("Render.cpp", 367, "", "Silence." + path); + log("", "Silence." + path, NORMAL, __FILENAME__, __LINE__); music.stopAll(); return true; } - log("Render.cpp", 371, "", "Played music from " + path); + log("", "Played music from " + path, NORMAL, __FILENAME__, __LINE__); if (currentMusic == path) { music.stopAll(); } @@ -399,7 +399,7 @@ bool Render::playMusic(string path) { bool Render::playModPlug(string path) { if (path == "") { - log("Render.cpp", 385, "", "Silence." + path); + log("", "Silence." + path, NORMAL, __FILENAME__, __LINE__); music.stopAll(); return true; } @@ -408,7 +408,7 @@ bool Render::playModPlug(string path) { } // midis if (path.find(".mid") != string::npos) { - log("Render.cpp", 394, "", "Played midi from " + path + " with soundfont " + SOUNDFONT); + log("", "Played midi from " + path + " with soundfont " + SOUNDFONT, NORMAL, __FILENAME__, __LINE__); midiLoader.load(path.c_str(), current_sf); midiLoader.setLooping(true); music.play(midiLoader); @@ -416,7 +416,7 @@ bool Render::playModPlug(string path) { return true; } - log("Render.cpp", 402, "", "Played mod tracker from " + path); + log("", "Played mod tracker from " + path, NORMAL, __FILENAME__, __LINE__); modLoader.load(path.c_str()); modLoader.setLooping(true); music.play(modLoader); @@ -445,38 +445,4 @@ void Render::pointTo(SDL_Rect* camera, Object object) { { camera->y = camera->h; } -} - -// shoutouts to gedehari for doing this -void Render::log(string file, int line, string prefix, string msg, LOG_TYPE type) { - clock_t now = std::clock(); - - double now_n = (double)now / CLOCKS_PER_SEC; - - string typeName = "LOG"; - - switch (type) { - case NORMAL: - break; - case WARNING: - typeName = "WARNING"; - break; - case ERROR_: - typeName = "ERROR"; - break; - } - - std::stringstream buf; - - buf << (int)(now_n/60) << ":" - << std::setfill('0') << std::setw(2) << (int)((int)now_n % 60) << "." - << std::setfill('0') << std::setw(3) << (int)((now_n - (int)now_n) * 1000) << " " - << typeName << ": (" << file << ":" << line << ") " << prefix << msg << endl; - - std::ofstream logFile; - logFile.open("log.txt", std::ios::app); - logFile << buf.str(); - logFile.close(); - - cout << buf.str(); } \ No newline at end of file diff --git a/src/Render.hpp b/src/Render.hpp index 13fb525..f6071af 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -21,6 +21,12 @@ #include "toml.hpp" #include +#ifdef _WIN32 || WIN32 +#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) +#else +#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) +#endif + using namespace std; #define WINDOW_WIDTH 640 @@ -273,7 +279,38 @@ namespace Render { */ void pointTo(SDL_Rect* camera, Object object); - void log(string file, int line, string prefix, string msg, LOG_TYPE type = NORMAL); + inline void log(string prefix, string msg, LOG_TYPE type = NORMAL, string file = "???.cpp", int line = 0) { + clock_t now = std::clock(); + + double now_n = (double)now / CLOCKS_PER_SEC; + + string typeName = "LOG"; + + switch (type) { + case NORMAL: + break; + case WARNING: + typeName = "WARNING"; + break; + case ERROR_: + typeName = "ERROR"; + break; + } + + std::stringstream buf; + + buf << (int)(now_n/60) << ":" + << std::setfill('0') << std::setw(2) << (int)((int)now_n % 60) << "." + << std::setfill('0') << std::setw(3) << (int)((now_n - (int)now_n) * 1000) << " " + << typeName << ": (" << file << ":" << line << ") " << prefix << msg << endl; + + std::ofstream logFile; + logFile.open("log.txt", std::ios::app); + logFile << buf.str(); + logFile.close(); + + cout << buf.str(); + } inline int Sec2Tick(float time) { return FRAMERATE*time;