From 66efccf095be845452e0b49793745eb0d82a1b58 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 7 Mar 2025 16:42:26 -0500 Subject: [PATCH] silly cable --- CMakeLists.txt | 2 +- include/blt/fs/file_writers.h | 1 + include/blt/fs/nbt.h | 11 ++-- include/blt/iterator/enumerate.h | 2 +- include/blt/logging/ansi.h | 3 +- include/blt/logging/logging_config.h | 80 +++++++++++++++++++++++++++ src/blt/fs/file_writers.cpp | 9 ++- src/blt/fs/nbt.cpp | 3 +- src/blt/logging/logging_config.cpp | 83 ++++++++++++++++++++++++++-- tests/iterator_tests.cpp | 2 +- 10 files changed, 180 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f30080a..a0effd4 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.0) +set(BLT_VERSION 5.2.1) set(BLT_TARGET BLT) diff --git a/include/blt/fs/file_writers.h b/include/blt/fs/file_writers.h index db25ed6..52ef476 100644 --- a/include/blt/fs/file_writers.h +++ b/include/blt/fs/file_writers.h @@ -65,6 +65,7 @@ namespace blt::fs { public: explicit buffered_writer(const std::string& name, size_t buffer_size = 1024 * 128); + explicit buffered_writer(size_t buffer_size = 1024 * 128); i64 write(const char* buffer, size_t bytes) override; diff --git a/include/blt/fs/nbt.h b/include/blt/fs/nbt.h index 2efa0b7..2353c42 100644 --- a/include/blt/fs/nbt.h +++ b/include/blt/fs/nbt.h @@ -359,15 +359,17 @@ namespace blt::nbt { void writePayload(blt::fs::writer_t& out) final { for (const auto& v : t){ auto tag = v.second; - out.put((char) tag->getType()); + auto c = (char) tag->getType(); + out.write(&c, 1); tag->writeName(out); tag->writePayload(out); } - out.put('\0'); + const char c = '\0'; + out.write(&c, 1); } void readPayload(blt::fs::reader_t& in) final { char type; - while ((type = in.get()) != (char)nbt_tag::END){ + while ((in.read(&type, 1), type) != (char)nbt_tag::END){ auto* v = _internal_::toType(type); v->readName(in); v->readPayload(in); @@ -431,7 +433,8 @@ namespace blt::nbt { delete root; } void write(tag_compound& root){ - writer.put((char)nbt_tag::COMPOUND); + auto c = (char)nbt_tag::COMPOUND; + writer.write(&c, 1); root.writeName(writer); root.writePayload(writer); } diff --git a/include/blt/iterator/enumerate.h b/include/blt/iterator/enumerate.h index 2f63cc2..71f3687 100644 --- a/include/blt/iterator/enumerate.h +++ b/include/blt/iterator/enumerate.h @@ -34,7 +34,7 @@ namespace blt } using iterator_category = typename std::iterator_traits::iterator_category; - using value_type = enumerate_item; + using value_type = enumerate_item>; using difference_type = blt::ptrdiff_t; using pointer = value_type; using reference = value_type; diff --git a/include/blt/logging/ansi.h b/include/blt/logging/ansi.h index 1113332..d07c7f1 100644 --- a/include/blt/logging/ansi.h +++ b/include/blt/logging/ansi.h @@ -71,7 +71,8 @@ namespace blt::logging::ansi BLUE = 4, MAGENTA = 5, CYAN = 6, - WHITE = 7, }; + WHITE = 7 + }; struct rgb_t { diff --git a/include/blt/logging/logging_config.h b/include/blt/logging/logging_config.h index c5a843c..5ae7b93 100644 --- a/include/blt/logging/logging_config.h +++ b/include/blt/logging/logging_config.h @@ -28,6 +28,82 @@ namespace blt::logging { + namespace tags + { + // Current Year + inline constexpr auto YEAR = "{YEAR}"; + // Current Month + inline constexpr auto MONTH = "{MONTH}"; + // Current Day + inline constexpr auto DAY = "{DAY}"; + // Current Hour + inline constexpr auto HOUR = "{HOUR}"; + // Current Minute + inline constexpr auto MINUTE = "{MINUTE}"; + // Current Second + inline constexpr auto SECOND = "{SECOND}"; + // Current Millisecond + inline constexpr auto MILLISECOND = "{MS}"; + // Current Nanosecond time, This is direct output of nanotime + inline constexpr auto NANOSECOND = "{NS}"; + // Current Unix time in milliseconds + inline constexpr auto UNIX_TIME = "{UNIX}"; + // Formatted ISO year-month-day in a single variable + inline constexpr auto ISO_YEAR = "{ISO_YEAR}"; + // Formatted hour:minute:second in a single variable + inline constexpr auto TIME = "{TIME}"; + // Formatted year-month-day hour:minute:second in a single variable + inline constexpr auto FULL_TIME = "{FULL_TIME}"; + // Color of the current log level, empty string if use_color = false + inline constexpr auto LOG_COLOR = "{LC}"; + // Color of the error color, empty string if use_color = false + inline constexpr auto ERROR_COLOR = "{EC}"; + // Empty is use_color = false or if log level is not an error. Otherwise, {EC} + inline constexpr auto CONDITIONAL_ERROR_COLOR = "{CEC}"; + // Resets all ANSI sequences + inline constexpr auto RESET = "{RESET}"; + // Current log level + inline constexpr auto LOG_LEVEL = "{LL}"; + // Current thread name. Requires you to manually set the thread name using blt::logging::set_thread_name() from that thread. + inline constexpr auto THREAD_NAME = "{TN}"; + // Current file from where the log call was invoked. + inline constexpr auto FILE = "{FILE}"; + // Current line from where the log call was invoked + inline constexpr auto LINE = "{LINE}"; + // User string input, formatted with provided args + inline constexpr auto STR = "{STR}"; + + namespace detail + { + enum class log_tag_token_t : u8 + { + YEAR, + MONTH, + DAY, + HOUR, + MINUTE, + SECOND, + MS, + NS, + UNIX, + ISO_YEAR, + TIME, + FULL_TIME, + LC, + EC, + CEC, + RESET, + LL, + TN, + FILE, + LINE, + STR, + // token used to describe that a non-format token should be consumed. aka a normal string from the file. + CONTENT + }; + } + } + enum class log_level_t : u8 { TRACE, @@ -43,10 +119,12 @@ namespace blt::logging class logging_config_t { friend logger_t; + public: // wrappers for streams exist in blt/fs/stream_wrappers.h std::vector log_outputs = get_default_log_outputs(); std::string log_format = get_default_log_format(); + std::string error_color = get_default_error_color(); std::array log_level_colors = get_default_log_level_colors(); std::array log_level_names = get_default_log_level_names(); log_level_t level = log_level_t::TRACE; @@ -56,11 +134,13 @@ namespace blt::logging // this will attempt to use the maximum possible size for each printed element, then align to that. // This creates output where the user message always starts at the same column. bool ensure_alignment = true; + private: static std::string get_default_log_format(); static std::vector get_default_log_outputs(); static std::array get_default_log_level_colors(); static std::array get_default_log_level_names(); + static std::string get_default_error_color(); }; } diff --git a/src/blt/fs/file_writers.cpp b/src/blt/fs/file_writers.cpp index 4c47b7c..2bc5b94 100644 --- a/src/blt/fs/file_writers.cpp +++ b/src/blt/fs/file_writers.cpp @@ -44,7 +44,12 @@ namespace blt::fs throw std::runtime_error("Failed to open file for writing"); } - buffered_writer::buffered_writer(const std::string& name, const size_t buffer_size): fwriter_t{name} + buffered_writer::buffered_writer(const std::string& name, const size_t buffer_size): fwriter_t{name, "ab"} + { + m_buffer.resize(buffer_size); + } + + buffered_writer::buffered_writer(const size_t buffer_size) { m_buffer.resize(buffer_size); } @@ -148,6 +153,6 @@ namespace blt::fs { const std::time_t time = std::time(nullptr); const auto current_time = std::localtime(&time); - return {current_time->tm_year, current_time->tm_mon, current_time->tm_mday, current_time->tm_hour}; + return {current_time->tm_year + 1900, current_time->tm_mon + 1, current_time->tm_mday, current_time->tm_hour}; } } diff --git a/src/blt/fs/nbt.cpp b/src/blt/fs/nbt.cpp index 2c0dc1a..1679977 100644 --- a/src/blt/fs/nbt.cpp +++ b/src/blt/fs/nbt.cpp @@ -33,7 +33,8 @@ namespace blt::nbt { } void NBTReader::read() { - char t = reader.get(); + char t; + reader.read(&t, 1); if (t != (char)nbt_tag::COMPOUND) { BLT_WARN("Found %d", t); throw std::runtime_error("Incorrectly formatted NBT data! Root tag must be a compound tag!"); diff --git a/src/blt/logging/logging_config.cpp b/src/blt/logging/logging_config.cpp index 10bd0f6..2d1295c 100644 --- a/src/blt/logging/logging_config.cpp +++ b/src/blt/logging/logging_config.cpp @@ -15,20 +15,93 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include +#include +#include #include +#include +#include namespace blt::logging { + namespace tags::detail + { + hashmap_t make_map() + { + hashmap_t map{}; + map[YEAR] = log_tag_token_t::YEAR; + map[MONTH] = log_tag_token_t::MONTH; + map[DAY] = log_tag_token_t::DAY; + map[HOUR] = log_tag_token_t::HOUR; + map[MINUTE] = log_tag_token_t::MINUTE; + map[SECOND] = log_tag_token_t::SECOND; + map[MILLISECOND] = log_tag_token_t::MS; + map[NANOSECOND] = log_tag_token_t::NS; + map[UNIX_TIME] = log_tag_token_t::UNIX; + map[ISO_YEAR] = log_tag_token_t::ISO_YEAR; + map[TIME] = log_tag_token_t::TIME; + map[FULL_TIME] = log_tag_token_t::FULL_TIME; + map[LOG_COLOR] = log_tag_token_t::LC; + map[ERROR_COLOR] = log_tag_token_t::EC; + map[CONDITIONAL_ERROR_COLOR] = log_tag_token_t::CEC; + map[RESET] = log_tag_token_t::RESET; + map[LOG_LEVEL] = log_tag_token_t::LL; + map[THREAD_NAME] = log_tag_token_t::TN; + map[FILE] = log_tag_token_t::FILE; + map[LINE] = log_tag_token_t::LINE; + map[STR] = log_tag_token_t::STR; + return map; + } + + hashmap_t tag_map = make_map(); + } + std::string logging_config_t::get_default_log_format() - {} + { + return build(fg(ansi::color::color8_bright::CYAN)) + "[" + tags::FULL_TIME + "]" + tags::RESET + " " + tags::LOG_COLOR + "[" + tags::LOG_LEVEL + + "]" + tags::RESET + " " + build(fg(ansi::color::color8::MAGENTA)) + "(" + tags::FILE + ":" + tags::LINE + ")" + tags::RESET + " " + + tags::CONDITIONAL_ERROR_COLOR + tags::STR + tags::RESET + "\n"; + } std::vector logging_config_t::get_default_log_outputs() - {} + { + static fs::fstream_writer_t cout_writer{std::cout}; + std::vector outputs{}; + outputs.push_back(&cout_writer); + return outputs; + } std::array logging_config_t::get_default_log_level_colors() - {} + { + return { + // TRACE + build(fg(ansi::color::color8_bright::WHITE)), + // DEBUG + build(fg(ansi::color::color8::CYAN)), + // INFO + build(fg(ansi::color::color8_bright::GREEN)), + // WARN + build(fg(ansi::color::color8_bright::YELLOW)), + // ERROR + build(fg(ansi::color::color8_bright::RED)), + // FATAL + build(fg(ansi::color::color8_bright::WHITE), bg(ansi::color::color8_bright::RED)), + }; + } std::array logging_config_t::get_default_log_level_names() - {} + { + return { + "TRACE", + "DEBUG", + "INFO", + "WARN", + "ERROR", + "FATAL", + }; + } + + std::string logging_config_t::get_default_error_color() + { + return build(fg(ansi::color::color8_bright::RED)); + } } diff --git a/tests/iterator_tests.cpp b/tests/iterator_tests.cpp index 66f787f..d267ccc 100644 --- a/tests/iterator_tests.cpp +++ b/tests/iterator_tests.cpp @@ -216,7 +216,7 @@ void test_iterate() } BLT_TRACE("================================"); for (auto a : blt::iterate(array_1).map([](const blt::vec2& in) { return in.normalize(); }) - .enumerate().filter([](const auto& f) { return f.value.x() > 0.5; })) + .enumerate().filter([](const auto& f) { return f.second.x() > 0.5; })) { if (!a) continue;