feat(logging): replaced printfs with logger calls

This commit is contained in:
maelstrom 2025-03-17 21:22:53 +01:00
parent 6d4dc844b3
commit 92a68925d9
12 changed files with 55 additions and 25 deletions

View file

@ -7,7 +7,9 @@
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>
#include <memory> #include <memory>
#include <stdio.h> #include <stdio.h>
#include <format>
#include "logger.h"
#include "objects/part.h" #include "objects/part.h"
#include "rendering/renderer.h" #include "rendering/renderer.h"
#include "physics/simulation.h" #include "physics/simulation.h"
@ -29,6 +31,8 @@ void resizeCallback(GLFWwindow* window, int width, int height);
std::shared_ptr<Part> lastPart; std::shared_ptr<Part> lastPart;
int main() { int main() {
Logger::init();
glfwSetErrorCallback(errorCatcher); glfwSetErrorCallback(errorCatcher);
glfwInit(); glfwInit();
@ -85,7 +89,7 @@ int main() {
} }
void errorCatcher(int id, const char* str) { void errorCatcher(int id, const char* str) {
printf("Something *terrible* happened. Here's the briefing: [%d] %s\n", id, str); Logger::fatalError(std::format("GLFW Error: [{}] {}", id, str));
} }
float lastTime; float lastTime;

View file

@ -1,6 +1,7 @@
#include "meta.h" #include "meta.h"
#include "datatypes/base.h" #include "datatypes/base.h"
#include "datatypes/cframe.h" #include "datatypes/cframe.h"
#include "logger.h"
#include <variant> #include <variant>
Data::String Data::Variant::ToString() const { Data::String Data::Variant::ToString() const {
@ -17,7 +18,7 @@ void Data::Variant::Serialize(pugi::xml_node* node) const {
Data::Variant Data::Variant::Deserialize(pugi::xml_node* node) { Data::Variant Data::Variant::Deserialize(pugi::xml_node* node) {
if (Data::TYPE_MAP.count(node->name()) == 0) { if (Data::TYPE_MAP.count(node->name()) == 0) {
fprintf(stderr, "Unknown type for instance: '%s'\n", node->name()); Logger::fatalErrorf("Unknown type for instance: '%s'", node->name());
abort(); abort();
} }

View file

@ -23,7 +23,7 @@ void Logger::finish() {
logStream.close(); logStream.close();
} }
void log(std::string message, Logger::LogLevel logLevel) { void Logger::log(std::string message, Logger::LogLevel logLevel) {
std::string logLevelStr = logLevel == Logger::LogLevel::INFO ? "INFO" : std::string logLevelStr = logLevel == Logger::LogLevel::INFO ? "INFO" :
logLevel == Logger::LogLevel::DEBUG ? "DEBUG" : logLevel == Logger::LogLevel::DEBUG ? "DEBUG" :
logLevel == Logger::LogLevel::WARNING ? "WARN" : logLevel == Logger::LogLevel::WARNING ? "WARN" :

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <format>
#include <string> #include <string>
namespace Logger { namespace Logger {
@ -20,4 +21,17 @@ namespace Logger {
inline void warning(std::string message) { log(message, LogLevel::WARNING); } inline void warning(std::string message) { log(message, LogLevel::WARNING); }
inline void error(std::string message) { log(message, LogLevel::ERROR); } inline void error(std::string message) { log(message, LogLevel::ERROR); }
inline void fatalError(std::string message) { log(message, LogLevel::FATAL_ERROR); } inline void fatalError(std::string message) { log(message, LogLevel::FATAL_ERROR); }
template <typename ...Args>
void logf(std::string format, LogLevel logLevel, Args&&... args) {
char message[200];
sprintf(message, format.c_str(), args...);
log(message, logLevel);
}
template <typename ...Args> inline void infof(std::string format, Args&&... args) { logf(format, LogLevel::INFO, args...); }
template <typename ...Args> inline void debugf(std::string format, Args&&... args) { logf(format, LogLevel::DEBUG, args...); }
template <typename ...Args> inline void warningf(std::string format, Args&&... args) { logf(format, LogLevel::WARNING, args...); }
template <typename ...Args> inline void errorf(std::string format, Args&&... args) { logf(format, LogLevel::ERROR, args...); }
template <typename ...Args> inline void fatalErrorf(std::string format, Args&&... args) { logf(format, LogLevel::FATAL_ERROR, args...);}
}; };

View file

@ -1,9 +1,10 @@
#include "instance.h" #include "instance.h"
#include "../../common.h" #include "common.h"
#include "../../datatypes/meta.h" #include "datatypes/meta.h"
#include "datatypes/base.h" #include "datatypes/base.h"
#include "objects/base/member.h" #include "objects/base/member.h"
#include "objects/meta.h" #include "objects/meta.h"
#include "logger.h"
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
@ -169,7 +170,7 @@ void Instance::Serialize(pugi::xml_node* parent) {
InstanceRef Instance::Deserialize(pugi::xml_node* node) { InstanceRef Instance::Deserialize(pugi::xml_node* node) {
std::string className = node->attribute("class").value(); std::string className = node->attribute("class").value();
if (INSTANCE_MAP.count(className) == 0) { if (INSTANCE_MAP.count(className) == 0) {
fprintf(stderr, "Unknown type for instance: '%s'\n", className.c_str()); Logger::fatalErrorf("Unknown type for instance: '%s'", className.c_str());
abort(); abort();
} }
// This will error if an abstract instance is used in the file. Oh well, not my prob rn. // This will error if an abstract instance is used in the file. Oh well, not my prob rn.
@ -185,7 +186,7 @@ InstanceRef Instance::Deserialize(pugi::xml_node* node) {
std::string propertyName = propertyNode.attribute("name").value(); std::string propertyName = propertyNode.attribute("name").value();
auto meta_ = object->GetPropertyMeta(propertyName); auto meta_ = object->GetPropertyMeta(propertyName);
if (!meta_.has_value()) { if (!meta_.has_value()) {
fprintf(stderr, "Attempt to set unknown property '%s' of %s\n", propertyName.c_str(), object->GetClass()->className.c_str()); Logger::fatalErrorf("Attempt to set unknown property '%s' of %s", propertyName.c_str(), object->GetClass()->className.c_str());
continue; continue;
} }
Data::Variant value = Data::Variant::Deserialize(&propertyNode); Data::Variant value = Data::Variant::Deserialize(&propertyNode);

View file

@ -3,6 +3,7 @@
#include "objects/base/instance.h" #include "objects/base/instance.h"
#include "objects/base/service.h" #include "objects/base/service.h"
#include "workspace.h" #include "workspace.h"
#include "logger.h"
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
#include <memory> #include <memory>
@ -42,7 +43,7 @@ void DataModel::Init() {
void DataModel::SaveToFile(std::optional<std::string> path) { void DataModel::SaveToFile(std::optional<std::string> path) {
if (!path.has_value() && !this->currentFile.has_value()) { if (!path.has_value() && !this->currentFile.has_value()) {
fprintf(stderr, "Cannot save DataModel because no path was provided.\n"); Logger::fatalError("Cannot save DataModel because no path was provided.");
abort(); abort();
} }
@ -60,18 +61,18 @@ void DataModel::SaveToFile(std::optional<std::string> path) {
doc.save(outStream); doc.save(outStream);
currentFile = target; currentFile = target;
name = target; name = target;
printf("Place saved succesfully\n"); Logger::info("Place saved succesfully");
} }
void DataModel::DeserializeService(pugi::xml_node* node) { void DataModel::DeserializeService(pugi::xml_node* node) {
std::string className = node->attribute("class").value(); std::string className = node->attribute("class").value();
if (SERVICE_CONSTRUCTORS.count(className) == 0) { if (SERVICE_CONSTRUCTORS.count(className) == 0) {
fprintf(stderr, "Unknown service: '%s'\n", className.c_str()); Logger::fatalErrorf("Unknown service: '%s'", className.c_str());
abort(); abort();
} }
if (services.count(className) != 0) { if (services.count(className) != 0) {
fprintf(stderr, "Service %s defined multiple times in file\n", className.c_str()); Logger::fatalErrorf("Service %s defined multiple times in file", className.c_str());
return; return;
} }
@ -84,7 +85,7 @@ void DataModel::DeserializeService(pugi::xml_node* node) {
std::string propertyName = propertyNode.attribute("name").value(); std::string propertyName = propertyNode.attribute("name").value();
auto meta_ = object->GetPropertyMeta(propertyName); auto meta_ = object->GetPropertyMeta(propertyName);
if (!meta_.has_value()) { if (!meta_.has_value()) {
fprintf(stderr, "Attempt to set unknown property '%s' of %s\n", propertyName.c_str(), object->GetClass()->className.c_str()); Logger::fatalErrorf("Attempt to set unknown property '%s' of %s", propertyName.c_str(), object->GetClass()->className.c_str());
continue; continue;
} }
Data::Variant value = Data::Variant::Deserialize(&propertyNode); Data::Variant value = Data::Variant::Deserialize(&propertyNode);

View file

@ -3,6 +3,7 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include "logger.h"
#include "shader.h" #include "shader.h"
std::string getContents(std::string filePath) { std::string getContents(std::string filePath) {
@ -25,7 +26,7 @@ unsigned int compileShader(std::string path, GLenum type) {
if(success != 1) { if(success != 1) {
char infoLog[256]; char infoLog[256];
glGetShaderInfoLog(shader, 512, NULL, infoLog); glGetShaderInfoLog(shader, 512, NULL, infoLog);
printf("Fragment shader %s failed to compile: [%d]: %s\n", path.c_str(), success, infoLog); Logger::fatalErrorf("Fragment shader %s failed to compile: [%d]: %s", path.c_str(), success, infoLog);
abort(); abort();
} }
@ -46,7 +47,7 @@ Shader::Shader(std::string vertexShaderPath, std::string fragmentShaderPath) {
if(success != 1) { if(success != 1) {
char infoLog[256]; char infoLog[256];
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
printf("Shader program failed to link: [%d]: %s\n", success, infoLog); Logger::fatalErrorf("Shader program failed to link: [%d]: %s", success, infoLog);
abort(); abort();
} }

View file

@ -2,6 +2,7 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <stb_image.h> #include <stb_image.h>
#include "logger.h"
#include "skybox.h" #include "skybox.h"
Skybox::Skybox(std::array<std::string, 6> faces, unsigned int format) { Skybox::Skybox(std::array<std::string, 6> faces, unsigned int format) {
@ -15,7 +16,7 @@ Skybox::Skybox(std::array<std::string, 6> faces, unsigned int format) {
&nrChannels, 0); &nrChannels, 0);
if (!data) { if (!data) {
printf("Failed to load texture '%s'\n", faces[i].c_str()); Logger::fatalErrorf("Failed to load texture '%s'", faces[i].c_str());
abort(); abort();
} }

View file

@ -4,6 +4,8 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <stb_image.h> #include <stb_image.h>
#include "logger.h"
Texture::Texture(const char* texturePath, unsigned int format, bool noMipMaps) { Texture::Texture(const char* texturePath, unsigned int format, bool noMipMaps) {
glGenTextures(1, &this->ID); glGenTextures(1, &this->ID);
glBindTexture(GL_TEXTURE_2D, this->ID); glBindTexture(GL_TEXTURE_2D, this->ID);
@ -22,7 +24,7 @@ Texture::Texture(const char* texturePath, unsigned int format, bool noMipMaps) {
&nrChannels, 0); &nrChannels, 0);
if (!data) { if (!data) {
printf("Failed to load texture '%s'\n", texturePath); Logger::fatalErrorf("Failed to load texture '%s'", texturePath);
abort(); abort();
} }

View file

@ -4,6 +4,8 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <stb_image.h> #include <stb_image.h>
#include "logger.h"
Texture3D::Texture3D(const char* texturePath, unsigned int tileWidth, unsigned int tileHeight, unsigned int tileCount, unsigned int format) { Texture3D::Texture3D(const char* texturePath, unsigned int tileWidth, unsigned int tileHeight, unsigned int tileCount, unsigned int format) {
glGenTextures(1, &this->ID); glGenTextures(1, &this->ID);
glBindTexture(GL_TEXTURE_2D_ARRAY, this->ID); glBindTexture(GL_TEXTURE_2D_ARRAY, this->ID);
@ -23,7 +25,7 @@ Texture3D::Texture3D(const char* texturePath, unsigned int tileWidth, unsigned i
&nrChannels, 0); &nrChannels, 0);
if (!data) { if (!data) {
printf("Failed to load texture '%s'\n", texturePath); Logger::fatalErrorf("Failed to load texture '%s'", texturePath);
abort(); abort();
} }

View file

@ -1,20 +1,22 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "logger.h"
#include <QApplication> #include <QApplication>
#include <QLocale> #include <QLocale>
#include <QTranslator> #include <QTranslator>
#include <QStyleFactory> #include <QStyleFactory>
#include <QBasicTimer> #include <QBasicTimer>
#include "physics/simulation.h"
#include "qcoreevent.h"
#include "qobject.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
Logger::init();
MainWindow w; MainWindow w;
w.show(); w.show();
return a.exec(); int result = a.exec();
Logger::finish();
return result;
} }

View file

@ -9,6 +9,7 @@
#include "qwidget.h" #include "qwidget.h"
#include "qmimedata.h" #include "qmimedata.h"
#include "common.h" #include "common.h"
#include "logger.h"
#include <algorithm> #include <algorithm>
#include <cstdio> #include <cstdio>
#include <optional> #include <optional>
@ -163,10 +164,10 @@ bool ExplorerModel::moveRows(const QModelIndex &sourceParentIdx, int sourceRow,
Instance* sourceParent = sourceParentIdx.isValid() ? static_cast<Instance*>(sourceParentIdx.internalPointer()) : rootItem.get(); Instance* sourceParent = sourceParentIdx.isValid() ? static_cast<Instance*>(sourceParentIdx.internalPointer()) : rootItem.get();
Instance* destinationParent = destinationParentIdx.isValid() ? static_cast<Instance*>(destinationParentIdx.internalPointer()) : rootItem.get(); Instance* destinationParent = destinationParentIdx.isValid() ? static_cast<Instance*>(destinationParentIdx.internalPointer()) : rootItem.get();
printf("Moved %d from %s\n", count, sourceParent->name.c_str()); Logger::infof("Moved %d from %s", count, sourceParent->name.c_str());
if ((sourceRow + count) >= sourceParent->GetChildren().size()) { if ((sourceRow + count) >= sourceParent->GetChildren().size()) {
fprintf(stderr, "Attempt to move rows %d-%d from %s (%s) while it only has %zu children.\n", sourceRow, sourceRow + count, sourceParent->name.c_str(), sourceParent->GetClass()->className.c_str(), sourceParent->GetChildren().size()); Logger::fatalErrorf("Attempt to move rows %d-%d from %s (%s) while it only has %zu children.", sourceRow, sourceRow + count, sourceParent->name.c_str(), sourceParent->GetClass()->className.c_str(), sourceParent->GetChildren().size());
return false; return false;
} }