From 637b4fa0e6b1dc2ac714cad43e9381c3455b6964 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 4 Mar 2025 13:58:14 -0500 Subject: [PATCH] nice error messages --- CMakeLists.txt | 2 +- include/blt/logging/fmt_tokenizer.h | 5 +++-- include/blt/logging/logging.h | 25 +------------------------ libraries/parallel-hashmap | 2 +- src/blt/logging/fmt_tokenizer.cpp | 18 ++++++++++++++++++ src/blt/logging/logging.cpp | 22 +++++++++++++++------- tests/logger_tests.cpp | 12 +++++++++--- 7 files changed, 48 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 14e8a8a..4f359be 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.1.7) +set(BLT_VERSION 5.1.8) set(BLT_TARGET BLT) diff --git a/include/blt/logging/fmt_tokenizer.h b/include/blt/logging/fmt_tokenizer.h index 2a81740..6e95c18 100644 --- a/include/blt/logging/fmt_tokenizer.h +++ b/include/blt/logging/fmt_tokenizer.h @@ -56,7 +56,8 @@ namespace blt::logging EXPONENT, // 'e' FIXED_POINT, // 'f' GENERAL, // 'g' - TYPE // 't' + TYPE, // 't' + UNSPECIFIED // default }; struct fmt_spec_t @@ -64,7 +65,7 @@ namespace blt::logging i64 arg_id = -1; i64 width = -1; i64 precision = -1; - fmt_type_t type = fmt_type_t::DECIMAL; + fmt_type_t type = fmt_type_t::UNSPECIFIED; fmt_sign_t sign = fmt_sign_t::MINUS; bool leading_zeros = false; bool uppercase = false; diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index 6e1192f..b696550 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -70,17 +70,11 @@ namespace blt::logging case fmt_sign_t::SPACE: if constexpr (std::is_arithmetic_v) { - if (type.type != fmt_type_t::BINARY && t >= 0) + if (type.type != fmt_type_t::BINARY && static_cast(t) >= 0l) stream << ' '; } break; case fmt_sign_t::PLUS: - if constexpr (std::is_arithmetic_v) - { - if (t >= 0) - stream << '+'; - } - break; case fmt_sign_t::MINUS: break; } @@ -129,23 +123,6 @@ namespace blt::logging stream << "{INVALID TYPE}"; } break; - case fmt_type_t::GENERAL: - if constexpr (std::is_arithmetic_v) - { - if (static_cast(t) > 10e12) - exponential(stream); - else - fixed(stream); - stream << t; - } - else - { - if constexpr (blt::meta::is_streamable_v) - stream << t; - else - stream << "{INVALID TYPE}"; - } - break; case fmt_type_t::TYPE: stream << blt::type_string(); break; diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 93201da..7ef2e73 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3 +Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 diff --git a/src/blt/logging/fmt_tokenizer.cpp b/src/blt/logging/fmt_tokenizer.cpp index 05ea4b0..dc434ef 100644 --- a/src/blt/logging/fmt_tokenizer.cpp +++ b/src/blt/logging/fmt_tokenizer.cpp @@ -15,6 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include #include #include #include @@ -379,6 +380,23 @@ namespace blt::logging default: std::stringstream ss; ss << "Invalid type " << value; + ss << std::endl << std::endl; + ss << "Expected one of: " << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "b | B" << std::setw(6) << ' ' << "Print as binary output" << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "c" << std::setw(6) << ' ' << "Print as character output" << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "d" << std::setw(6) << ' ' << "Print as decimal (base 10) output" << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "o" << std::setw(6) << ' ' << "Print as octal (base 8) output" << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "x | X" << std::setw(6) << ' ' << "Print as hexadecimal (base 16) output" << + std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "a | A" << std::setw(6) << ' ' << "Print floats as hexadecimal (base 16) output" + << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "e | E" << std::setw(6) << ' ' << "Print floats in scientific notation" << + std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "f | F" << std::setw(6) << ' ' << + "Print floats in fixed point output, useful for setting precision of output" << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "g | G" << std::setw(6) << ' ' << + "Print floats in general output, switching between fixed point and scientific notation based on magnitude" << std::endl; + ss << std::setw(4) << ' ' << std::left << std::setw(5) << "t | T" << std::setw(6) << ' ' << "Print the type as a string" << std::endl; throw std::runtime_error(ss.str()); } } diff --git a/src/blt/logging/logging.cpp b/src/blt/logging/logging.cpp index 94314c2..c480b7e 100644 --- a/src/blt/logging/logging.cpp +++ b/src/blt/logging/logging.cpp @@ -49,11 +49,17 @@ namespace blt::logging if (spec.precision > 0) m_stream << std::setprecision(static_cast(spec.precision)); else - m_stream << std::setprecision(static_cast(std::cout.precision())); + m_stream << std::setprecision(16); + if (spec.alternate_form) + m_stream << std::showbase; if (spec.uppercase) m_stream << std::uppercase; else m_stream << std::nouppercase; + if (spec.sign == fmt_sign_t::PLUS) + m_stream << std::showpos; + else + m_stream << std::noshowpos; } void logger_t::process_strings() @@ -78,21 +84,16 @@ namespace blt::logging switch (spec.type) { case fmt_type_t::DECIMAL: + stream << std::noboolalpha; stream << std::dec; break; case fmt_type_t::OCTAL: - if (spec.alternate_form) - stream << "0"; stream << std::oct; break; case fmt_type_t::HEX: - if (spec.alternate_form) - stream << (spec.uppercase ? "0X" : "0x"); stream << std::hex; break; case fmt_type_t::HEX_FLOAT: - if (spec.alternate_form) - stream << (spec.uppercase ? "0X" : "0x"); stream << std::hexfloat; break; case fmt_type_t::EXPONENT: @@ -101,6 +102,13 @@ namespace blt::logging case fmt_type_t::FIXED_POINT: stream << std::fixed; break; + case fmt_type_t::GENERAL: + stream << std::defaultfloat; + break; + case fmt_type_t::UNSPECIFIED: + stream << std::boolalpha; + stream << std::dec; + break; default: break; } diff --git a/tests/logger_tests.cpp b/tests/logger_tests.cpp index 906e835..c4d7bc5 100644 --- a/tests/logger_tests.cpp +++ b/tests/logger_tests.cpp @@ -41,13 +41,19 @@ int main() blt::logging::println("This is a println with hex with leading {:#.10x}", 4250); blt::logging::println("This is a println with binary {:#b}", 6969420); blt::logging::println("This is a println with binary with space {: #b}", 6969421); + blt::logging::println("This is a println with binary with space {: b}", 69); blt::logging::println("This is a println with octal {:#o}", 6669); blt::logging::println("This is a println with hexfloat {:a}", 402.4320); blt::logging::println("This is a println with exponent {:e}", 44320902.4320); - blt::logging::println("This is a println with exponent {:e}", 9532434234042340); - blt::logging::println("This is a println with exponent {:g}", 953243.49); - blt::logging::println("This is a println with exponent {:g}", 953243324023403240.49); + blt::logging::println("This is a println with exponent {:e}", 9532434234042340.0); + blt::logging::println("This is a println with general {:g}", 953243.49); + blt::logging::println("This is a println with general {:g}", 953243324023403240.49); blt::logging::println("This is a println with a char {:c}", 66); blt::logging::println("This is a println with type {:t}", some_silly_type_t{}); + blt::logging::println("This is a println with boolean {}", true); + blt::logging::println("This is a println with boolean as int {:d}", false); + blt::logging::println("This is a println with boolean as hex {:#x}", true); + blt::logging::println("This is a println with boolean as octal {:o}", true); + blt::logging::println("This is a println with boolean as test {:h}", true); // blt::logging::println("This is println {}\twith a std::endl in the middle of it"); }