From 0adc8de683f644fefa567ddfa044c2976f0b635e Mon Sep 17 00:00:00 2001 From: PoneyUHC Date: Mon, 2 Sep 2024 23:09:39 +0200 Subject: [PATCH] add logging system --- Makefile | 4 +-- TODO.md | 3 +- src/assets/image_asset.cpp | 13 ++++---- src/export/png_exporter.cpp | 4 +-- src/export/ppm_exporter.cpp | 4 +-- src/logger.cpp | 35 +++++++++++++++++++ src/logger.hpp | 48 +++++++++++++++++++++++++++ src/main.cpp | 15 +++++---- src/math/math_utils.cpp | 18 ---------- src/math/math_utils.hpp | 4 --- src/renderer/pathtracing_renderer.cpp | 21 +++++------- src/utils.cpp | 4 +-- 12 files changed, 115 insertions(+), 58 deletions(-) create mode 100644 src/logger.cpp create mode 100644 src/logger.hpp diff --git a/Makefile b/Makefile index 604b2c7..9707de2 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CXX = g++ RM = rm -f -CXXFLAGS = -O3 -Wall -Wpedantic -std=c++17 -MMD -MP -fopenmp +CXXFLAGS = -O3 -Wall -Wpedantic -std=c++20 -MMD -MP -fopenmp EXEC = render.exe OUTPUT_DIR = output @@ -37,7 +37,7 @@ $(EXEC): $(OBJS_LOCATION) $(BUILD_OBJS_DIR)/%.o: %.cpp Makefile @$(eval CURRENT_TARGET=$(shell expr $(CURRENT_TARGET) + 1)) - @printf "\rCompiling file $(CURRENT_TARGET) / $(NB_TARGETS)" + @printf "\rCompiling file $(CURRENT_TARGET) / $(NB_TARGETS) " @if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi @$(CXX) $(CXXFLAGS) -I $(SRC_DIR) -c $< -o $@ diff --git a/TODO.md b/TODO.md index 5979742..1b021bb 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,8 @@ -- Book 2 section 4 : texture mapping +- Book 2 section 8 : instances - make buildchain crossplatform with CMake - test to replace RNG for grid sampling in antialiasing - centralize asset loading in a resource manager - find a good name for the project - have a format describing scenes instead of code description -- add log system - add render queue \ No newline at end of file diff --git a/src/assets/image_asset.cpp b/src/assets/image_asset.cpp index a0113b5..4873bc7 100644 --- a/src/assets/image_asset.cpp +++ b/src/assets/image_asset.cpp @@ -2,6 +2,7 @@ #include "image_asset.hpp" #include "utils.hpp" +#include "logger.hpp" #define STB_IMAGE_IMPLEMENTATION #include "third_party/stb_image.h" @@ -17,11 +18,11 @@ namespace fs = std::filesystem; int ImageAsset::Load() { - clog << "Loading " << m_filepath << " ..." << endl; - fs::path path(m_filepath); + Logger::LogInfo("Loading " + m_filepath + " ..."); + fs::path path(m_filepath); if(!fs::exists(m_filepath)){ - cerr << "\t" << m_filepath << ": file not found" << endl; + Logger::LogError("\t" + m_filepath + ": file not found"); return false; } @@ -30,8 +31,8 @@ int ImageAsset::Load() float* fdata; fdata = stbi_loadf(path.c_str(), &m_width, &m_height, &n, m_bytes_per_pixel); if(fdata == NULL){ - cerr << "\t" << m_filepath << ": error while loading file" << endl; - cerr << "\t" << stbi_failure_reason() << endl; + Logger::LogError("\t" + m_filepath + ": error while loading file"); + Logger::LogError("\t" + string(stbi_failure_reason())); return false; } @@ -45,7 +46,7 @@ int ImageAsset::Load() m_bdata[i] = rgb_normalized_to_8bits(m_fdata[i]); } - clog << "Loaded " << m_filepath << " successfully" << endl; + Logger::LogInfo("Loaded " + m_filepath + " successfully"); return true; } diff --git a/src/export/png_exporter.cpp b/src/export/png_exporter.cpp index 471ac58..3101946 100644 --- a/src/export/png_exporter.cpp +++ b/src/export/png_exporter.cpp @@ -2,11 +2,11 @@ #include "png_exporter.hpp" #include "utils.hpp" +#include "logger.hpp" #include #include #include -#include using namespace std; @@ -220,7 +220,7 @@ int PngExporter::Export(int width, int height, shared_ptr buffer) m_output_fs = ofstream(m_filepath, std::ios::out | std::ios::binary); if ( !m_output_fs.is_open()) { - cerr << __FUNCTION__ << " : failed to open " << m_filepath << endl; + Logger::LogError(string(__FUNCTION__) + " : failed to open " + m_filepath); return 1; } diff --git a/src/export/ppm_exporter.cpp b/src/export/ppm_exporter.cpp index 19cc959..eaf1363 100644 --- a/src/export/ppm_exporter.cpp +++ b/src/export/ppm_exporter.cpp @@ -3,9 +3,9 @@ #include "renderer/camera.hpp" #include "math/interval.hpp" #include "utils.hpp" +#include "logger.hpp" #include -#include #include @@ -23,7 +23,7 @@ int PpmExporter::Export(int width, int height, std::shared_ptr buffe ofstream file(m_filepath); if ( !file.is_open()) { - cerr << __FUNCTION__ << " : failed to open " << m_filepath << endl; + Logger::LogError(string(__FUNCTION__) + " : failed to open " + m_filepath); return 1; } diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..8754e5e --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,35 @@ + +#include "logger.hpp" + +#include +#include +#include + + +using namespace std; +using namespace std::chrono; + + +string Logger::GetCurrentTimeFormatted() +{ + auto now = std::chrono::system_clock::now(); + return format("[{:%T}]", zoned_time{ current_zone(), floor(now)}); +} + + +void Logger::Log(const std::string &log, const LogLevel& level) +{ + if(level < s_level){ + return; + } + + ostringstream string_builder; + string_builder << GetCurrentTimeFormatted() << " "; + string_builder << s_colored_name_lookup.at(level) << " "; + string_builder << log << endl; + + const string& formatted_log = string_builder.str(); + for(ostream* stream : s_outputs){ + *stream << formatted_log; + } +} diff --git a/src/logger.hpp b/src/logger.hpp new file mode 100644 index 0000000..9bbcb42 --- /dev/null +++ b/src/logger.hpp @@ -0,0 +1,48 @@ + +#pragma once + +#include +#include + + +enum class LogLevel { + DEBUG = 1, + INFO = 2, + WARNING = 3, + ERROR = 4, + FATAL = 5 +}; + + +class Logger { + +private: + + static inline std::vector s_outputs = { &std::cout }; + static inline LogLevel s_level = LogLevel::DEBUG; + + static inline const std::unordered_map s_colored_name_lookup + { + { LogLevel::DEBUG, "[\x1b[94mDEBUG\x1b[0m] " }, + { LogLevel::INFO, "[INFO] " }, + { LogLevel::WARNING, "[\x1b[93mWARNING\x1b[0m]" }, + { LogLevel::ERROR, "[\x1b[91mERROR\x1b[0m] " }, + { LogLevel::FATAL, "[\x1b[31mFATAL\x1b[0m] " } + }; + + static void Log(const std::string& log, const LogLevel& level); + static std::string GetCurrentTimeFormatted(); + + +public: + + static void SetLogLevel(const LogLevel& level) { s_level = level; } + static void AddOutput(std::ostream& stream) { s_outputs.push_back(&stream); } + + static void LogDebug(const std::string& log) { Log(log, LogLevel::DEBUG); } + static void LogInfo(const std::string& log) { Log(log, LogLevel::INFO); } + static void LogWarning(const std::string& log) { Log(log, LogLevel::WARNING); } + static void LogError(const std::string& log) { Log(log, LogLevel::ERROR); } + static void LogFatal(const std::string& log) { Log(log, LogLevel::FATAL); } + +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7bc8aa7..e16336b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,8 +7,9 @@ #include "scenes/cornell_box_scene.hpp" #include "renderer/camera.hpp" #include "export/png_exporter.hpp" +#include "logger.hpp" -#include +#include #include @@ -18,11 +19,11 @@ using namespace std; int main(int argc, char *argv[]){ if (argc != 2){ - cout << "Usage : " << argv[0] << " width" << endl; + Logger::LogError(format("Usage : {} width", argv[1])); return 1; } - cout << "Initializing scene" << endl; + Logger::LogInfo("Initializing scene"); SceneParams scene_params; scene_params.render_width = atoi(argv[1]); @@ -30,7 +31,7 @@ int main(int argc, char *argv[]){ unique_ptr scene; - switch(5){ + switch(3){ case 1: scene = make_unique(); break; @@ -47,14 +48,14 @@ int main(int argc, char *argv[]){ scene = make_unique(); break; default: - cout << "Problem in scene selection" << endl; + Logger::LogFatal("Problem in scene selection"); return 1; } - cout << "Building scene " << typeid(*scene.get()).name() << endl; + Logger::LogInfo("Building scene " + string(typeid(*scene.get()).name())); scene->Build(std::move(scene_params)); - cout << "Starting rendering" << endl; + Logger::LogInfo("Starting rendering"); shared_ptr color_buffer = scene->Render(); PngExporter png_exporter("output/render.png"); diff --git a/src/math/math_utils.cpp b/src/math/math_utils.cpp index 4ba0483..c936344 100644 --- a/src/math/math_utils.cpp +++ b/src/math/math_utils.cpp @@ -6,24 +6,6 @@ #include -int lerp(int start, int stop, double value) -{ - return int((1-value) * start + value * stop); -} - - -double lerp(double start, double stop, double value) -{ - return (1-value) * start + value * stop; -} - - -Vec3 lerp(const Vec3& start, const Vec3& stop, double value) -{ - return start * (1-value) + stop * value; -} - - double degrees_to_radians(double degrees) { return degrees * M_PI / 180.0; diff --git a/src/math/math_utils.hpp b/src/math/math_utils.hpp index 9590da6..fdef486 100644 --- a/src/math/math_utils.hpp +++ b/src/math/math_utils.hpp @@ -4,10 +4,6 @@ class Vec3; - -int lerp(int start, int stop, double value); -double lerp(double start, double stop, double value); -Vec3 lerp(const Vec3& start, const Vec3& stop, double value); double degrees_to_radians(double degrees); double random_double(); double random_double(double min, double max); diff --git a/src/renderer/pathtracing_renderer.cpp b/src/renderer/pathtracing_renderer.cpp index 21747f5..aeb896f 100644 --- a/src/renderer/pathtracing_renderer.cpp +++ b/src/renderer/pathtracing_renderer.cpp @@ -8,10 +8,11 @@ #include "geometry/hittable_list.hpp" #include "material/IMaterial.hpp" #include "utils.hpp" +#include "logger.hpp" #include -#include #include +#include using namespace std; @@ -43,11 +44,11 @@ void PathTracingRenderer::Render() { } catch (const std::exception& e){ // keep default value - cout << "OMP_NUM_THREAD is undefined" << endl; + Logger::LogWarning("OMP_NUM_THREAD is undefined"); } omp_set_num_threads(num_threads); - cout << "Using " << num_threads << " CPU threads to render" << endl; + Logger::LogInfo(format("Using {} CPU threads to render", num_threads)); int progress = 0; #pragma omp parallel for num_threads(num_threads) shared(progress) @@ -58,10 +59,11 @@ void PathTracingRenderer::Render() { #ifdef _OPENMP #pragma omp critical { - clog << "\rLines remaining: " << (height - progress) << ' ' << flush; + Logger::LogInfo(format("Lines remaining: {} ", height - progress)); + ++progress; } #else - clog << "\rLines remaining: " << (height - j) << ' ' << flush; + Logger::LogInfo(format("Lines remaining: {} ", height - j)); #endif for (int i = 0; i < width; i++) { @@ -76,16 +78,9 @@ void PathTracingRenderer::Render() { m_buffer[j * width + i] = accumulator; } - - #ifdef _OPENMP - #pragma omp critical - { - ++progress; - } - #endif } - clog << "\rDone. \n"; + Logger::LogInfo("Done"); } diff --git a/src/utils.cpp b/src/utils.cpp index 0088dba..95d37c8 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -2,8 +2,8 @@ #include "utils.hpp" #include "math/interval.hpp" +#include "logger.hpp" -#include #include @@ -17,7 +17,7 @@ string get_env_var(const string& key) { char* val = getenv(key.c_str()); if(val == NULL){ - clog << "Could not retrieve environment variable with key '" << key << "'" << endl; + Logger::LogError("Could not retrieve environment variable with key '" + key + "'"); } return val == NULL ? string("") : string(val); }