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.
This commit is contained in:
/nick haya 2022-02-10 13:50:46 +08:00
parent 865708fa75
commit b30611da35
2 changed files with 60 additions and 57 deletions

View file

@ -216,41 +216,41 @@ bool Render::Init(string window_name) {
} }
SOUNDFONT = tomlParse<string>("conf.toml", "config", "soundfont"); SOUNDFONT = tomlParse<string>("conf.toml", "config", "soundfont");
if (se.init() > 0) { 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; return false;
} }
if (music.init() > 0) { 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; 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) { 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; 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) { 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; 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) { 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; 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); 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) { 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; 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); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) { 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; 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()); current_sf.load(SOUNDFONT.c_str());
@ -259,7 +259,7 @@ bool Render::Init(string window_name) {
SDL_GetWindowWMInfo(window, &wmInfo); SDL_GetWindowWMInfo(window, &wmInfo);
hwnd = wmInfo.info.win.window; hwnd = wmInfo.info.win.window;
log("Render.cpp", 245, "", "Finalized initialization. Command over.", NORMAL); log("", "Finalized initialization. Command over.", NORMAL, __FILENAME__, __LINE__);
return true; return true;
} }
@ -352,7 +352,7 @@ bool Render::Update() {
void Render::SwitchState(State* state) { void Render::SwitchState(State* state) {
string state_name = typeid(*state).name(); 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) { if (current_state != nullptr) {
_ticks = {}; _ticks = {};
_call = {}; _call = {};
@ -363,12 +363,12 @@ void Render::SwitchState(State* state) {
} }
} }
current_state = state; current_state = state;
log("Render.cpp", 349, "Success!", " Calling Create()...."); log("Success!", " Calling Create()....", NORMAL, __FILENAME__, __LINE__);
current_state->Create(); current_state->Create();
} }
bool Render::playSound(string path, bool override) { 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); waveLoader.setLooping(false);
if (override) { if (override) {
se.stop(seIndex); se.stop(seIndex);
@ -381,11 +381,11 @@ bool Render::playSound(string path, bool override) {
bool Render::playMusic(string path) { bool Render::playMusic(string path) {
if (path == "") { if (path == "") {
log("Render.cpp", 367, "", "Silence." + path); log("", "Silence." + path, NORMAL, __FILENAME__, __LINE__);
music.stopAll(); music.stopAll();
return true; return true;
} }
log("Render.cpp", 371, "", "Played music from " + path); log("", "Played music from " + path, NORMAL, __FILENAME__, __LINE__);
if (currentMusic == path) { if (currentMusic == path) {
music.stopAll(); music.stopAll();
} }
@ -399,7 +399,7 @@ bool Render::playMusic(string path) {
bool Render::playModPlug(string path) { bool Render::playModPlug(string path) {
if (path == "") { if (path == "") {
log("Render.cpp", 385, "", "Silence." + path); log("", "Silence." + path, NORMAL, __FILENAME__, __LINE__);
music.stopAll(); music.stopAll();
return true; return true;
} }
@ -408,7 +408,7 @@ bool Render::playModPlug(string path) {
} }
// midis // midis
if (path.find(".mid") != string::npos) { 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.load(path.c_str(), current_sf);
midiLoader.setLooping(true); midiLoader.setLooping(true);
music.play(midiLoader); music.play(midiLoader);
@ -416,7 +416,7 @@ bool Render::playModPlug(string path) {
return true; 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.load(path.c_str());
modLoader.setLooping(true); modLoader.setLooping(true);
music.play(modLoader); music.play(modLoader);
@ -446,37 +446,3 @@ void Render::pointTo(SDL_Rect* camera, Object object) {
camera->y = camera->h; 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();
}

View file

@ -21,6 +21,12 @@
#include "toml.hpp" #include "toml.hpp"
#include <fstream> #include <fstream>
#ifdef _WIN32 || WIN32
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
using namespace std; using namespace std;
#define WINDOW_WIDTH 640 #define WINDOW_WIDTH 640
@ -273,7 +279,38 @@ namespace Render {
*/ */
void pointTo(SDL_Rect* camera, Object object); 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) { inline int Sec2Tick(float time) {
return FRAMERATE*time; return FRAMERATE*time;