diff --git a/client/src/main.cpp b/client/src/main.cpp index 38e80d0..367fb17 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -7,7 +7,9 @@ #include #include #include +#include +#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 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; diff --git a/core/src/datatypes/meta.cpp b/core/src/datatypes/meta.cpp index 1b45299..f959d2e 100644 --- a/core/src/datatypes/meta.cpp +++ b/core/src/datatypes/meta.cpp @@ -1,6 +1,7 @@ #include "meta.h" #include "datatypes/base.h" #include "datatypes/cframe.h" +#include "logger.h" #include 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(); } diff --git a/core/src/logger.cpp b/core/src/logger.cpp index f1f82a7..335910c 100644 --- a/core/src/logger.cpp +++ b/core/src/logger.cpp @@ -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" : diff --git a/core/src/logger.h b/core/src/logger.h index f4701f6..4298ac3 100644 --- a/core/src/logger.h +++ b/core/src/logger.h @@ -1,5 +1,6 @@ #pragma once +#include #include 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 + void logf(std::string format, LogLevel logLevel, Args&&... args) { + char message[200]; + sprintf(message, format.c_str(), args...); + log(message, logLevel); + } + + template inline void infof(std::string format, Args&&... args) { logf(format, LogLevel::INFO, args...); } + template inline void debugf(std::string format, Args&&... args) { logf(format, LogLevel::DEBUG, args...); } + template inline void warningf(std::string format, Args&&... args) { logf(format, LogLevel::WARNING, args...); } + template inline void errorf(std::string format, Args&&... args) { logf(format, LogLevel::ERROR, args...); } + template inline void fatalErrorf(std::string format, Args&&... args) { logf(format, LogLevel::FATAL_ERROR, args...);} }; \ No newline at end of file diff --git a/core/src/objects/base/instance.cpp b/core/src/objects/base/instance.cpp index 9d2dea4..f5e3794 100644 --- a/core/src/objects/base/instance.cpp +++ b/core/src/objects/base/instance.cpp @@ -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 #include #include @@ -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); diff --git a/core/src/objects/datamodel.cpp b/core/src/objects/datamodel.cpp index d898d6e..9b55e6b 100644 --- a/core/src/objects/datamodel.cpp +++ b/core/src/objects/datamodel.cpp @@ -3,6 +3,7 @@ #include "objects/base/instance.h" #include "objects/base/service.h" #include "workspace.h" +#include "logger.h" #include #include #include @@ -42,7 +43,7 @@ void DataModel::Init() { void DataModel::SaveToFile(std::optional 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 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); diff --git a/core/src/rendering/shader.cpp b/core/src/rendering/shader.cpp index 8d25072..6784614 100644 --- a/core/src/rendering/shader.cpp +++ b/core/src/rendering/shader.cpp @@ -3,6 +3,7 @@ #include #include +#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(); } diff --git a/core/src/rendering/skybox.cpp b/core/src/rendering/skybox.cpp index e18315d..6aad73c 100644 --- a/core/src/rendering/skybox.cpp +++ b/core/src/rendering/skybox.cpp @@ -2,6 +2,7 @@ #include #include +#include "logger.h" #include "skybox.h" Skybox::Skybox(std::array faces, unsigned int format) { @@ -15,7 +16,7 @@ Skybox::Skybox(std::array 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(); } diff --git a/core/src/rendering/texture.cpp b/core/src/rendering/texture.cpp index 908d779..ea6bb15 100644 --- a/core/src/rendering/texture.cpp +++ b/core/src/rendering/texture.cpp @@ -4,6 +4,8 @@ #include #include +#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(); } diff --git a/core/src/rendering/texture3d.cpp b/core/src/rendering/texture3d.cpp index ed15e54..8c92828 100644 --- a/core/src/rendering/texture3d.cpp +++ b/core/src/rendering/texture3d.cpp @@ -4,6 +4,8 @@ #include #include +#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(); } diff --git a/editor/main.cpp b/editor/main.cpp index 765f5c2..08d2743 100644 --- a/editor/main.cpp +++ b/editor/main.cpp @@ -1,20 +1,22 @@ #include "mainwindow.h" +#include "logger.h" #include #include #include #include #include -#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; } diff --git a/editor/panes/explorermodel.cpp b/editor/panes/explorermodel.cpp index a064fe3..5dbbd17 100644 --- a/editor/panes/explorermodel.cpp +++ b/editor/panes/explorermodel.cpp @@ -9,6 +9,7 @@ #include "qwidget.h" #include "qmimedata.h" #include "common.h" +#include "logger.h" #include #include #include @@ -163,10 +164,10 @@ bool ExplorerModel::moveRows(const QModelIndex &sourceParentIdx, int sourceRow, Instance* sourceParent = sourceParentIdx.isValid() ? static_cast(sourceParentIdx.internalPointer()) : rootItem.get(); Instance* destinationParent = destinationParentIdx.isValid() ? static_cast(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; }