Skip to content

Commit

Permalink
add logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Sep 2, 2024
1 parent 4f3c0e3 commit 0adc8de
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 58 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 $@

3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions src/assets/image_asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "image_asset.hpp"

#include "utils.hpp"
#include "logger.hpp"

#define STB_IMAGE_IMPLEMENTATION
#include "third_party/stb_image.h"
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/export/png_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "png_exporter.hpp"

#include "utils.hpp"
#include "logger.hpp"

#include <cstring>
#include <fstream>
#include <filesystem>
#include <iostream>


using namespace std;
Expand Down Expand Up @@ -220,7 +220,7 @@ int PngExporter::Export(int width, int height, shared_ptr<RGBColor[]> 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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/export/ppm_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "renderer/camera.hpp"
#include "math/interval.hpp"
#include "utils.hpp"
#include "logger.hpp"

#include <filesystem>
#include <iostream>
#include <fstream>


Expand All @@ -23,7 +23,7 @@ int PpmExporter::Export(int width, int height, std::shared_ptr<RGBColor[]> 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;
}

Expand Down
35 changes: 35 additions & 0 deletions src/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#include "logger.hpp"

#include <sstream>
#include <chrono>
#include <iostream>


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<milliseconds>(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;
}
}
48 changes: 48 additions & 0 deletions src/logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#pragma once

#include <vector>
#include <unordered_map>


enum class LogLevel {
DEBUG = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
FATAL = 5
};


class Logger {

private:

static inline std::vector<std::ostream*> s_outputs = { &std::cout };
static inline LogLevel s_level = LogLevel::DEBUG;

static inline const std::unordered_map<LogLevel, std::string> 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); }

};
15 changes: 8 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include "scenes/cornell_box_scene.hpp"
#include "renderer/camera.hpp"
#include "export/png_exporter.hpp"
#include "logger.hpp"

#include <iostream>
#include <format>
#include <typeinfo>


Expand All @@ -18,19 +19,19 @@ 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]);
scene_params.enable_bvh = true;

unique_ptr<IScene> scene;

switch(5){
switch(3){
case 1:
scene = make_unique<SparsedSpheresScene>();
break;
Expand All @@ -47,14 +48,14 @@ int main(int argc, char *argv[]){
scene = make_unique<CornellBoxScene>();
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<RGBColor[]> color_buffer = scene->Render();

PngExporter png_exporter("output/render.png");
Expand Down
18 changes: 0 additions & 18 deletions src/math/math_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@
#include <random>


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;
Expand Down
4 changes: 0 additions & 4 deletions src/math/math_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 8 additions & 13 deletions src/renderer/pathtracing_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include "geometry/hittable_list.hpp"
#include "material/IMaterial.hpp"
#include "utils.hpp"
#include "logger.hpp"

#include <omp.h>
#include <iostream>
#include <string>
#include <format>


using namespace std;
Expand Down Expand Up @@ -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)
Expand All @@ -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++) {
Expand All @@ -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");
}


Expand Down
4 changes: 2 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "utils.hpp"

#include "math/interval.hpp"
#include "logger.hpp"

#include <iostream>
#include <algorithm>


Expand All @@ -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);
}
Expand Down

0 comments on commit 0adc8de

Please sign in to comment.