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 <memory>
#include <stdio.h>
#include <format>
#include "logger.h"
#include "objects/part.h"
#include "rendering/renderer.h"
#include "physics/simulation.h"
@ -29,6 +31,8 @@ void resizeCallback(GLFWwindow* window, int width, int height);
std::shared_ptr<Part> lastPart;
int main() {
Logger::init();
glfwSetErrorCallback(errorCatcher);
glfwInit();
@ -85,7 +89,7 @@ int main() {
}
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;

View file

@ -1,6 +1,7 @@
#include "meta.h"
#include "datatypes/base.h"
#include "datatypes/cframe.h"
#include "logger.h"
#include <variant>
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) {
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();
}

View file

@ -23,7 +23,7 @@ void Logger::finish() {
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" :
logLevel == Logger::LogLevel::DEBUG ? "DEBUG" :
logLevel == Logger::LogLevel::WARNING ? "WARN" :

View file

@ -1,5 +1,6 @@
#pragma once
#include <format>
#include <string>
namespace Logger {
@ -20,4 +21,17 @@ namespace Logger {
inline void warning(std::string message) { log(message, LogLevel::WARNING); }
inline void error(std::string message) { log(message, LogLevel::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 "../../common.h"
#include "../../datatypes/meta.h"
#include "common.h"
#include "datatypes/meta.h"
#include "datatypes/base.h"
#include "objects/base/member.h"
#include "objects/meta.h"
#include "logger.h"
#include <algorithm>
#include <cstddef>
#include <cstdio>
@ -169,7 +170,7 @@ void Instance::Serialize(pugi::xml_node* parent) {
InstanceRef Instance::Deserialize(pugi::xml_node* node) {
std::string className = node->attribute("class").value();
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();
}
// 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();
auto meta_ = object->GetPropertyMeta(propertyName);
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;
}
Data::Variant value = Data::Variant::Deserialize(&propertyNode);

View file

@ -3,6 +3,7 @@
#include "objects/base/instance.h"
#include "objects/base/service.h"
#include "workspace.h"
#include "logger.h"
#include <cstdio>
#include <fstream>
#include <memory>
@ -42,7 +43,7 @@ void DataModel::Init() {
void DataModel::SaveToFile(std::optional<std::string> path) {
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();
}
@ -60,18 +61,18 @@ void DataModel::SaveToFile(std::optional<std::string> path) {
doc.save(outStream);
currentFile = target;
name = target;
printf("Place saved succesfully\n");
Logger::info("Place saved succesfully");
}
void DataModel::DeserializeService(pugi::xml_node* node) {
std::string className = node->attribute("class").value();
if (SERVICE_CONSTRUCTORS.count(className) == 0) {
fprintf(stderr, "Unknown service: '%s'\n", className.c_str());
Logger::fatalErrorf("Unknown service: '%s'", className.c_str());
abort();
}
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;
}
@ -84,7 +85,7 @@ void DataModel::DeserializeService(pugi::xml_node* node) {
std::string propertyName = propertyNode.attribute("name").value();
auto meta_ = object->GetPropertyMeta(propertyName);
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;
}
Data::Variant value = Data::Variant::Deserialize(&propertyNode);

View file

@ -3,6 +3,7 @@
#include <GL/gl.h>
#include <glm/gtc/type_ptr.hpp>
#include "logger.h"
#include "shader.h"
std::string getContents(std::string filePath) {
@ -25,7 +26,7 @@ unsigned int compileShader(std::string path, GLenum type) {
if(success != 1) {
char infoLog[256];
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();
}
@ -46,7 +47,7 @@ Shader::Shader(std::string vertexShaderPath, std::string fragmentShaderPath) {
if(success != 1) {
char infoLog[256];
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();
}

View file

@ -2,6 +2,7 @@
#include <GL/gl.h>
#include <stb_image.h>
#include "logger.h"
#include "skybox.h"
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);
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();
}

View file

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

View file

@ -4,6 +4,8 @@
#include <GL/gl.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) {
glGenTextures(1, &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);
if (!data) {
printf("Failed to load texture '%s'\n", texturePath);
Logger::fatalErrorf("Failed to load texture '%s'", texturePath);
abort();
}

View file

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

View file

@ -9,6 +9,7 @@
#include "qwidget.h"
#include "qmimedata.h"
#include "common.h"
#include "logger.h"
#include <algorithm>
#include <cstdio>
#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* 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()) {
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;
}