From 78a4ad9f39e98f9047a41cce9a380825cbfd18b6 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 10 Mar 2025 00:29:20 -0400 Subject: [PATCH] i think all the old logging is replaced --- CMakeLists.txt | 2 +- include/blt/fs/nbt.h | 6 +- include/blt/std/allocator.h | 12 +- src/blt/fs/loader.cpp | 4 +- src/blt/fs/nbt.cpp | 2 +- src/blt/parse/argparse.cpp | 2 +- src/blt/parse/obj_loader.cpp | 445 ++++++++++++++++++----------------- src/blt/parse/templating.cpp | 2 +- src/blt/std/assert.cpp | 8 +- src/blt/std/system.cpp | 4 +- tests/logger_tests.cpp | 1 + 11 files changed, 245 insertions(+), 243 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 700ed57..b86fef5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 5.2.6) +set(BLT_VERSION 5.2.7) set(BLT_TARGET BLT) diff --git a/include/blt/fs/nbt.h b/include/blt/fs/nbt.h index 9d59004..d94d988 100644 --- a/include/blt/fs/nbt.h +++ b/include/blt/fs/nbt.h @@ -305,7 +305,7 @@ namespace blt::nbt { auto& tag = t[i]; T t; if (tag->getType() != t.getType()) { - BLT_WARN("Expected tag of type %d but got tag of type %d", (char)t.getType(), (char)tag->getType()); + BLT_WARN("Expected tag of type {:d} but got tag of type {:d}", (char)t.getType(), (char)tag->getType()); throw std::runtime_error("Requested Tag does not match stored type!"); } return dynamic_cast(tag); @@ -342,7 +342,7 @@ namespace blt::nbt { auto& tag = t[name]; T t; if (tag->getType() != t.getType()) { - BLT_WARN("Expected tag of type %d but got tag of type %d", (char)t.getType(), (char)tag->getType()); + BLT_WARN("Expected tag of type {:d} but got tag of type {:d}", (char)t.getType(), (char)tag->getType()); throw std::runtime_error("Requested Tag does not match stored type!"); } return dynamic_cast(tag); @@ -409,7 +409,7 @@ namespace blt::nbt { auto& tag = root->get()[name]; T t; if (tag->getType() != t.getType()) { - BLT_WARN("Expected tag of type %d but got tag of type %d", (char)t.getType(), (char)tag->getType()); + BLT_WARN("Expected tag of type {:d} but got tag of type {:d}", (char)t.getType(), (char)tag->getType()); throw std::runtime_error("Requested Tag does not match stored type!"); } return dynamic_cast(tag); diff --git a/include/blt/std/allocator.h b/include/blt/std/allocator.h index 1f7b8f8..e812428 100644 --- a/include/blt/std/allocator.h +++ b/include/blt/std/allocator.h @@ -149,7 +149,7 @@ namespace blt */ inline void allocate_block() { - //BLT_INFO("Allocating a new block of size %d", BLOCK_SIZE); + //BLT_INFO("Allocating a new block of size {:d}", BLOCK_SIZE); auto* blk = new block_storage(); blk->data = static_cast(malloc(sizeof(T) * BLOCK_SIZE)); blocks.push_back(blk); @@ -572,13 +572,13 @@ namespace blt if constexpr (WARN_ON_FAIL) { if (((size_t) buffer & (HUGE_PAGE_SIZE - 1)) != 0) - BLT_ERROR("Pointer is not aligned! %p", buffer); + BLT_ERROR("Pointer is not aligned! {:#x}", buffer); } auto* ptr = static_cast(buffer); auto ptr_size = reinterpret_cast(ptr); buffer = static_cast(std::align(BLOCK_SIZE, BLOCK_SIZE, ptr, bytes)); if constexpr (WARN_ON_FAIL) - BLT_ERROR("Offset by %ld pages, resulting: %p", (reinterpret_cast(buffer) - ptr_size) / 4096, buffer); + BLT_ERROR("Offset by {} pages, resulting: {:#x}", (reinterpret_cast(buffer) - ptr_size) / 4096, buffer); } return buffer; #endif @@ -839,7 +839,7 @@ namespace blt #endif // if (deletes.contains(p)) // { - // BLT_FATAL("pointer %p has already been freed", p); + // BLT_FATAL("pointer {:#x} has already been freed", p); // throw std::bad_alloc(); // }else // deletes.insert(static_cast(p)); @@ -848,7 +848,7 @@ namespace blt blk->metadata.allocated_objects--; if (blk->metadata.allocated_objects == 0) { - //BLT_INFO("Deallocating block from %p in (1) %p current head %p, based: %p", p, blk, head, base); + //BLT_INFO("Deallocating block from {:#x} in (1) {:#x} current head {:#x}, based: {:#x}", p, blk, head, base); if (blk == base) { base = base->metadata.next; @@ -860,7 +860,7 @@ namespace blt else if (blk->metadata.prev != nullptr) // finally if it wasn't the head we need to bridge the gap in the list blk->metadata.prev->metadata.next = blk->metadata.next; - //BLT_INFO("Deallocating block from %p in (2) %p current head %p, based: %p", p, blk, head, base); + //BLT_INFO("Deallocating block from {:#x} in (2) {:#x} current head {:#x}, based: {:#x}", p, blk, head, base); delete_block(blk); } } diff --git a/src/blt/fs/loader.cpp b/src/blt/fs/loader.cpp index c078630..fd39673 100644 --- a/src/blt/fs/loader.cpp +++ b/src/blt/fs/loader.cpp @@ -82,8 +82,8 @@ std::string blt::fs::getFile(std::string_view path) file_contents = file_stream.str(); } catch (std::ifstream::failure& e) { - BLT_WARN("Unable to read file '%s'!\n", std::string(path).c_str()); - BLT_WARN("Exception: %s", e.what()); + BLT_WARN("Unable to read file '{}'!\n", std::string(path).c_str()); + BLT_WARN("Exception: {}", e.what()); BLT_THROW(std::runtime_error("Failed to read file!\n")); } return file_contents; diff --git a/src/blt/fs/nbt.cpp b/src/blt/fs/nbt.cpp index 5ec5c33..ad00e12 100644 --- a/src/blt/fs/nbt.cpp +++ b/src/blt/fs/nbt.cpp @@ -36,7 +36,7 @@ namespace blt::nbt { char t; reader.read(&t, 1); if (t != (char)nbt_tag::COMPOUND) { - BLT_WARN("Found %d", t); + BLT_WARN("Found {:d}", t); throw std::runtime_error("Incorrectly formatted NBT data! Root tag must be a compound tag!"); } root = new tag_compound; diff --git a/src/blt/parse/argparse.cpp b/src/blt/parse/argparse.cpp index 4f8477c..19c91c6 100644 --- a/src/blt/parse/argparse.cpp +++ b/src/blt/parse/argparse.cpp @@ -196,7 +196,7 @@ namespace blt printUsage(); std::cout << getProgramName() << ": error: flag '" << flag << "' expected " << properties.a_nargs.args << " argument(s) but found '" << tokenizer.get() << "' instead!\n"; - //BLT_WARN("Expected %d arguments, found flag instead!", properties.a_nargs.args); + //BLT_WARN("Expected {:d} arguments, found flag instead!", properties.a_nargs.args); return false; } // get the value and advance diff --git a/src/blt/parse/obj_loader.cpp b/src/blt/parse/obj_loader.cpp index 5433876..1449d60 100644 --- a/src/blt/parse/obj_loader.cpp +++ b/src/blt/parse/obj_loader.cpp @@ -30,226 +30,227 @@ namespace blt::parse { - class char_tokenizer - { - private: - std::string_view string; - std::size_t current_pos = 0; - public: - explicit char_tokenizer(std::string_view view): string(view) - {} - - inline char advance() - { - return string[current_pos++]; - } - - inline bool has_next(size_t offset = 0) - { - return current_pos + offset < string.size(); - } - - inline std::string_view read_fully() - { - return blt::string::trim(string.substr(current_pos)); - } - }; - - template - T get(std::string_view str) - { - T x; - // TODO: GCC version. C++17 supports from_chars but GCC8.5 doesn't have floating point. -#if __cplusplus >= BLT_CPP20 + class char_tokenizer + { + private: + std::string_view string; + std::size_t current_pos = 0; + + public: + explicit char_tokenizer(std::string_view view): string(view) + {} + + inline char advance() + { + return string[current_pos++]; + } + + inline bool has_next(size_t offset = 0) + { + return current_pos + offset < string.size(); + } + + inline std::string_view read_fully() + { + return blt::string::trim(string.substr(current_pos)); + } + }; + + template + T get(std::string_view str) + { + T x; + // TODO: GCC version. C++17 supports from_chars but GCC8.5 doesn't have floating point. + #if __cplusplus >= BLT_CPP20 const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), x); -#else - auto ec = std::errc(); - if constexpr (std::is_floating_point_v) - { - x = static_cast(std::stod(std::string(str))); - } else if constexpr (std::is_integral_v) - { - x = static_cast(std::stoll(std::string(str))); - } else - static_assert( - "You are using a c++ version which does not support the required std::from_chars, manual conversion has failed to find a type!"); -#endif - // probably not needed. - if (ec != std::errc()) - { -// int i; -// const auto [ptr2, ec2] = std::from_chars(str.data(), str.data() + str.size(), i); -// if (ec2 == std::errc()) -// { -// x = static_cast(i); -// } else -// { - BLT_WARN("Unable to parse string '%s' into number!", std::string(str).c_str()); - x = 0; -// } - } - return x; - } - - void obj_loader::parse_vertex_line(char_tokenizer& tokenizer) - { - char type = tokenizer.advance(); - - if (type == 'p') - { - BLT_WARN("Unexpected type '%c' (not supported)", type); - return; - } - - auto elements = string::split(tokenizer.read_fully(), " "); - BLT_ASSERT(elements.size() >= 2 && "Current line doesn't have enough arguments to process!"); - float x = get(elements[0]), y = get(elements[1]); - BLT_DEBUG("Loaded value of ({}, {})", x, y); - if (elements.size() < 3) - { - if (type == 't') - uvs.push_back(uv_t{x, y}); - else - BLT_ERROR("Unable to parse line '{}' type '{:c}' not recognized for arg count", tokenizer.read_fully(), type); - } else - { - float z = get(elements[2]); - BLT_DEBUG(" with z: {}", z); - if (!handle_vertex_and_normals(x, y, z, type)) - BLT_ERROR("Unable to parse line '{}' type '{:c}' not recognized", tokenizer.read_fully(), type); - } - } - - bool obj_loader::handle_vertex_and_normals(float x, float y, float z, char type) - { - if (std::isspace(type)) - { - vertices.push_back(vertex_t{x, y, z}); - } else if (type == 'n') - { - normals.push_back(normal_t{x, y, z}); - } else - return false; - return true; - } - - obj_model_t quick_load(std::string_view file) - { - return obj_loader().parseFile(file); - } - - obj_model_t obj_loader::parseFile(std::string_view file) - { - auto lines = blt::fs::getLinesFromFile(std::string(file)); - for (const auto& [index, line] : blt::enumerate(lines)) - { - current_line = index; - char_tokenizer token(line); - if (!token.has_next() || token.read_fully().empty()) - continue; - switch (token.advance()) - { - case '#': - continue; - case 'f': - parse_face(token); - break; - case 'v': - parse_vertex_line(token); - break; - case 'o': - { - current_object.object_names.emplace_back(token.read_fully()); - BLT_TRACE("Setting object '%s'", std::string(current_object.object_name).c_str()); - break; - } - case 'm': - { - while (token.has_next() && token.advance() != ' ') - {} - BLT_WARN("Material '%s' needs to be loaded!", std::string(token.read_fully()).c_str()); - break; - } - case 'u': - { - if (!current_object.indices.empty()) - data.push_back(current_object); - current_object = {}; - while (token.has_next() && token.advance() != ' ') - {} - current_object.material = token.read_fully(); - //BLT_WARN("Using material '%s'", std::string(token.read_fully()).c_str()); - break; - } - case 's': - //BLT_WARN("Using shading: %s", std::string(token.read_fully()).c_str()); - break; - } - } - data.push_back(current_object); - return {std::move(vertex_data), std::move(data), std::move(materials)}; - } - - void obj_loader::parse_face(char_tokenizer& tokenizer) - { - auto faces = blt::string::split(std::string(tokenizer.read_fully()), ' '); - if (faces.size() == 3) - { - triangle_t triangle{}; - handle_face_vertex(faces, triangle.v); - current_object.indices.push_back(triangle); - } else if (faces.size() == 4) - { - quad_t quad{}; - handle_face_vertex(faces, quad.v); - triangle_t t1{}; - triangle_t t2{}; - - for (int i = 0; i < 3; i++) - t1.v[i] = quad.v[i]; - t2.v[0] = quad.v[0]; - t2.v[1] = quad.v[2]; - t2.v[2] = quad.v[3]; - - current_object.indices.push_back(t1); - current_object.indices.push_back(t2); - } else - BLT_WARN("Unsupported face vertex count of %d on line %d!", faces.size(), current_line); - } - - void obj_loader::handle_face_vertex(const std::vector& face_list, int32_t* arr) - { - for (const auto& [e_index, value] : blt::enumerate(face_list)) - { - auto indices = blt::string::split(value, '/'); - BLT_ASSERT(indices.size() == 3 && "Must have vertex, uv, and normal indices!!"); - - auto vi = get(indices[0]) - 1; - auto ui = get(indices[1]) - 1; - auto ni = get(indices[2]) - 1; - - BLT_DEBUG("Found vertex: %d, UV: %d, and normal: %d", vi, ui, ni); - - face_t face{vi, ui, ni}; - - auto loc = vertex_map.find(face); - if (loc == vertex_map.end()) - { - BLT_DEBUG("DID NOT FIND FACE!"); - auto index = static_cast(vertex_data.size()); - vertex_data.push_back({vertices[vi], uvs[ui], normals[ni]}); - BLT_DEBUG("Vertex: (%f, %f, %f), UV: (%f, %f), Normal: (%f, %f, %f)", vertices[vi].x(), vertices[vi].y(), vertices[vi].z(), - uvs[ui].x(), uvs[ui].y(), normals[ni].x(), normals[ni].y(), normals[ni].z()); - vertex_map.insert({face, index}); - arr[e_index] = index; - } else - { - BLT_TRACE("Using cached data; %d; map size: %d", loc->second, vertex_data.size()); - //const auto& d = vertex_data[loc->second]; - BLT_TRACE("Vertex: (%f, %f, %f), UV: (%f, %f), Normal: (%f, %f, %f)", d.vertex.x(), d.vertex.y(), d.vertex.z(), - d.uv.x(), d.uv.y(), d.normal.x(), d.normal.y(), d.normal.z()); - arr[e_index] = loc->second; - } - } - } -} \ No newline at end of file + #else + auto ec = std::errc(); + if constexpr (std::is_floating_point_v) + { + x = static_cast(std::stod(std::string(str))); + } else if constexpr (std::is_integral_v) + { + x = static_cast(std::stoll(std::string(str))); + } else + static_assert( + "You are using a c++ version which does not support the required std::from_chars, manual conversion has failed to find a type!"); + #endif + // probably not needed. + if (ec != std::errc()) + { + // int i; + // const auto [ptr2, ec2] = std::from_chars(str.data(), str.data() + str.size(), i); + // if (ec2 == std::errc()) + // { + // x = static_cast(i); + // } else + // { + BLT_WARN("Unable to parse string '{}' into number!", std::string(str).c_str()); + x = 0; + // } + } + return x; + } + + void obj_loader::parse_vertex_line(char_tokenizer& tokenizer) + { + char type = tokenizer.advance(); + + if (type == 'p') + { + BLT_WARN("Unexpected type '{:c}' (not supported)", type); + return; + } + + auto elements = string::split(tokenizer.read_fully(), " "); + BLT_ASSERT(elements.size() >= 2 && "Current line doesn't have enough arguments to process!"); + float x = get(elements[0]), y = get(elements[1]); + BLT_DEBUG("Loaded value of ({}, {})", x, y); + if (elements.size() < 3) + { + if (type == 't') + uvs.push_back(uv_t{x, y}); + else + BLT_ERROR("Unable to parse line '{}' type '{:c}' not recognized for arg count", tokenizer.read_fully(), type); + } else + { + float z = get(elements[2]); + BLT_DEBUG(" with z: {}", z); + if (!handle_vertex_and_normals(x, y, z, type)) + BLT_ERROR("Unable to parse line '{}' type '{:c}' not recognized", tokenizer.read_fully(), type); + } + } + + bool obj_loader::handle_vertex_and_normals(float x, float y, float z, char type) + { + if (std::isspace(type)) + { + vertices.push_back(vertex_t{x, y, z}); + } else if (type == 'n') + { + normals.push_back(normal_t{x, y, z}); + } else + return false; + return true; + } + + obj_model_t quick_load(std::string_view file) + { + return obj_loader().parseFile(file); + } + + obj_model_t obj_loader::parseFile(std::string_view file) + { + auto lines = blt::fs::getLinesFromFile(std::string(file)); + for (const auto& [index, line] : blt::enumerate(lines)) + { + current_line = index; + char_tokenizer token(line); + if (!token.has_next() || token.read_fully().empty()) + continue; + switch (token.advance()) + { + case '#': + continue; + case 'f': + parse_face(token); + break; + case 'v': + parse_vertex_line(token); + break; + case 'o': + { + current_object.object_names.emplace_back(token.read_fully()); + BLT_TRACE("Setting object '{}'", std::string(current_object.object_name).c_str()); + break; + } + case 'm': + { + while (token.has_next() && token.advance() != ' ') + {} + BLT_WARN("Material '{}' needs to be loaded!", std::string(token.read_fully()).c_str()); + break; + } + case 'u': + { + if (!current_object.indices.empty()) + data.push_back(current_object); + current_object = {}; + while (token.has_next() && token.advance() != ' ') + {} + current_object.material = token.read_fully(); + //BLT_WARN("Using material '{}'", std::string(token.read_fully()).c_str()); + break; + } + case 's': + //BLT_WARN("Using shading: {}", std::string(token.read_fully()).c_str()); + break; + } + } + data.push_back(current_object); + return {std::move(vertex_data), std::move(data), std::move(materials)}; + } + + void obj_loader::parse_face(char_tokenizer& tokenizer) + { + auto faces = blt::string::split(std::string(tokenizer.read_fully()), ' '); + if (faces.size() == 3) + { + triangle_t triangle{}; + handle_face_vertex(faces, triangle.v); + current_object.indices.push_back(triangle); + } else if (faces.size() == 4) + { + quad_t quad{}; + handle_face_vertex(faces, quad.v); + triangle_t t1{}; + triangle_t t2{}; + + for (int i = 0; i < 3; i++) + t1.v[i] = quad.v[i]; + t2.v[0] = quad.v[0]; + t2.v[1] = quad.v[2]; + t2.v[2] = quad.v[3]; + + current_object.indices.push_back(t1); + current_object.indices.push_back(t2); + } else + BLT_WARN("Unsupported face vertex count of {:d} on line {:d}!", faces.size(), current_line); + } + + void obj_loader::handle_face_vertex(const std::vector& face_list, int32_t* arr) + { + for (const auto& [e_index, value] : blt::enumerate(face_list)) + { + auto indices = blt::string::split(value, '/'); + BLT_ASSERT(indices.size() == 3 && "Must have vertex, uv, and normal indices!!"); + + auto vi = get(indices[0]) - 1; + auto ui = get(indices[1]) - 1; + auto ni = get(indices[2]) - 1; + + BLT_DEBUG("Found vertex: {:d}, UV: {:d}, and normal: {:d}", vi, ui, ni); + + face_t face{vi, ui, ni}; + + auto loc = vertex_map.find(face); + if (loc == vertex_map.end()) + { + BLT_DEBUG("DID NOT FIND FACE!"); + auto index = static_cast(vertex_data.size()); + vertex_data.push_back({vertices[vi], uvs[ui], normals[ni]}); + BLT_DEBUG("Vertex: ({.4f}, {.4f}, {.4f}), UV: ({.4f}, {.4f}), Normal: ({.4f}, {.4f}, {:.4f})", vertices[vi].x(), vertices[vi].y(), + vertices[vi].z(), uvs[ui].x(), uvs[ui].y(), normals[ni].x(), normals[ni].y(), normals[ni].z()); + vertex_map.insert({face, index}); + arr[e_index] = index; + } else + { + BLT_TRACE("Using cached data; {:d}; map size: {:d}", loc->second, vertex_data.size()); + //const auto& d = vertex_data[loc->second]; + BLT_TRACE("Vertex: ({.4f}, {.4f}, {.4f}), UV: ({.4f}, {.4f}), Normal: ({.4f}, {.4f}, {:.4f})", d.vertex.x(), d.vertex.y(), + d.vertex.z(), d.uv.x(), d.uv.y(), d.normal.x(), d.normal.y(), d.normal.z()); + arr[e_index] = loc->second; + } + } + } +} diff --git a/src/blt/parse/templating.cpp b/src/blt/parse/templating.cpp index 6d0f962..f2221c1 100644 --- a/src/blt/parse/templating.cpp +++ b/src/blt/parse/templating.cpp @@ -275,7 +275,7 @@ namespace blt values.push_back(b1 ^ b2); break; default: - BLT_WARN("Unexpected token '%s'", std::string(next.token).c_str()); + BLT_WARN("Unexpected token '{}'", std::string(next.token).c_str()); return blt::unexpected(template_parser_failure_t::BOOL_TYPE_NOT_FOUND); } } diff --git a/src/blt/std/assert.cpp b/src/blt/std/assert.cpp index ebc99e5..68db4df 100644 --- a/src/blt/std/assert.cpp +++ b/src/blt/std/assert.cpp @@ -86,7 +86,7 @@ namespace blt #ifdef IS_GNU_BACKTRACE BLT_STACK_TRACE(50); - BLT_ERROR("An exception '%s' has occurred in file '%s:%d'", what, path, line); + BLT_ERROR("An exception '{}' has occurred in file '{}:{:d}'", what, path, line); BLT_ERROR("Stack Trace:"); printStacktrace(messages, size, path, line); @@ -103,7 +103,7 @@ namespace blt #ifdef IS_GNU_BACKTRACE BLT_STACK_TRACE(50); - BLT_ERROR("The assertion '%s' has failed in file '%s:%d'", expression, path, line); + BLT_ERROR("The assertion '{}' has failed in file '{}:{:d}'", expression, path, line); if (msg != nullptr) BLT_ERROR(msg); BLT_ERROR("Stack Trace:"); @@ -175,8 +175,8 @@ namespace blt BLT_STACK_TRACE(50); #endif BLT_FATAL("----{BLT ABORT}----"); - BLT_FATAL("\tWhat: %s", what); - BLT_FATAL("\tCalled from %s:%d", path, line); + BLT_FATAL("\tWhat: {}", what); + BLT_FATAL("\tCalled from {}:{:d}", path, line); #ifdef IS_GNU_BACKTRACE printStacktrace(messages, size, path, line); diff --git a/src/blt/std/system.cpp b/src/blt/std/system.cpp index 3dbed6e..9147178 100644 --- a/src/blt/std/system.cpp +++ b/src/blt/std/system.cpp @@ -94,7 +94,7 @@ namespace blt if (GetProcessTimes(GetCurrentProcess(), &starttime, &exittime, &kerneltime, &usertime) == 0) { - BLT_WARN("Unable to get process resource usage, error: %d", GetLastError()); + BLT_WARN("Unable to get process resource usage, error: {:d}", GetLastError()); return {}; } @@ -111,7 +111,7 @@ namespace blt #else if (getrusage(who, (struct rusage*) &usage) != 0) { - BLT_ERROR("Failed to get rusage %d", errno); + BLT_ERROR("Failed to get rusage {:d}", errno); return {}; } #endif diff --git a/tests/logger_tests.cpp b/tests/logger_tests.cpp index a9f8bb6..6741770 100644 --- a/tests/logger_tests.cpp +++ b/tests/logger_tests.cpp @@ -181,6 +181,7 @@ int main() BLT_WARN("This is a warning!"); BLT_ERROR("This is an error!"); BLT_FATAL("This is a fatal error!"); + BLT_TRACE("This is a pointer {:f}", &charCount); /*std::cout << "\033[2J"; constexpr int totalRows = 24;