diff --git a/CMakeLists.txt b/CMakeLists.txt index 77a14de..7d95e2c 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.1) +set(BLT_VERSION 5.1.2) set(BLT_TARGET BLT) diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index 1cd41ee..cf98092 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -23,85 +23,119 @@ #include #include #include +#include namespace blt::logging { - struct logger_t - { - explicit logger_t() = default; + struct logger_t + { + explicit logger_t() = default; - template - std::string print_value(T&& t) - { - static_assert(meta::is_streamable_v, "T must be streamable in order to work with blt::logging!"); - m_stream.str(""); - m_stream.clear(); - m_stream << std::forward(t); - return m_stream.str(); - } + template + std::string log(std::string fmt, Args&&... args) + { + compile(std::move(fmt)); + auto sequence = std::make_integer_sequence{}; + while (auto pair = consume_until_fmt()) + { + auto [begin, end] = *pair; + if (end - begin > 0) + { + auto format_data = handle_fmt(m_fmt.substr(begin + 1, begin - end - 1)); + auto [arg_pos, fmt_type] = format_data; + if (arg_pos == -1) + arg_pos = static_cast(m_arg_pos++); + if (fmt_type) + { + if (fmt_type == fmt_type_t::GENERAL) + { + apply_func([this](auto&& value) + { + if (static_cast(value) > 0xFFFFFFFFFul) + exponential(); + else + fixed(); + m_stream << std::forward(value); + }, arg_pos, sequence, std::forward(args)...); + } else if (fmt_type == fmt_type_t::CHAR) + { - template - std::string log(std::string fmt, Args&&... args) - { - compile(std::move(fmt)); - m_args_to_str.clear(); - m_args_to_str.resize(sizeof...(Args)); - insert(std::make_integer_sequence{}, std::forward(args)...); - finish(); - return to_string(); - } + } else if (fmt_type == fmt_type_t::BINARY) + { - std::string to_string(); + } + } + else + { + apply_func([this](auto&& value) + { + m_stream << std::forward(value); + }, arg_pos, sequence, std::forward(args)...); + } + } + else + apply_func([this](auto&& value) + { + m_stream << std::forward(value); + }, m_arg_pos++, sequence, std::forward(args)...); + } + finish(); + return to_string(); + } - private: - template - void insert(std::integer_sequence, Args&&... args) - { - ((handle_insert(std::forward(args))), ...); - } + std::string to_string(); - template - void handle_insert(T&& t) - { - m_args_to_str[index] = print_value(std::forward(t)); - } + private: + template + void apply_func(const Func& func, const size_t arg, std::integer_sequence, Args&&... args) + { + ((handle_func(func, arg, std::forward(args))), ...); + } - void handle_fmt(std::string_view fmt); + template + void handle_func(const Func& func, const size_t arg, T&& t) + { + if (index == arg) + func(std::forward(t)); + } - const std::string& get(size_t index); + [[nodiscard]] std::pair> handle_fmt(std::string_view fmt); - void compile(std::string fmt); + void exponential(); + void fixed(); - bool consume_until_fmt(); + void compile(std::string fmt); - void finish(); + std::optional> consume_until_fmt(); - std::string m_fmt; - std::stringstream m_stream; - std::vector m_args_to_str; - size_t m_last_fmt_pos = 0; - size_t m_arg_pos = 0; - }; + void finish(); - void print(const std::string& fmt); + std::string m_fmt; + std::stringstream m_stream; + fmt_parser_t m_parser; + size_t m_last_fmt_pos = 0; + size_t m_arg_pos = 0; + }; - void newline(); + void print(const std::string& fmt); - logger_t& get_global_logger(); + void newline(); - template - void print(std::string fmt, Args&&... args) - { - auto& logger = get_global_logger(); - print(logger.log(std::move(fmt), std::forward(args)...)); - } + logger_t& get_global_logger(); - template - void println(std::string fmt, Args&&... args) - { - print(std::move(fmt), std::forward(args)...); - newline(); - } + template + void print(std::string fmt, Args&&... args) + { + auto& logger = get_global_logger(); + print(logger.log(std::move(fmt), std::forward(args)...)); + } + + template + void println(std::string fmt, Args&&... args) + { + print(std::move(fmt), std::forward(args)...); + newline(); + } } #endif // BLT_LOGGING_LOGGING_H diff --git a/src/blt/logging/fmt_tokenizer.cpp b/src/blt/logging/fmt_tokenizer.cpp index 507690c..b2bb522 100644 --- a/src/blt/logging/fmt_tokenizer.cpp +++ b/src/blt/logging/fmt_tokenizer.cpp @@ -116,7 +116,8 @@ namespace blt::logging { consume(); parse_fmt_spec_stage_1(); - }else + } + else { std::cerr << "Expected ':' when parsing format field after arg id!" << std::endl; std::exit(EXIT_FAILURE); @@ -260,10 +261,67 @@ namespace blt::logging void fmt_parser_t::parse_precision() { - + if (!has_next()) + { + std::cerr << "Missing token when parsing precision" << std::endl; + std::exit(EXIT_FAILURE); + } + auto [_, value] = next(); + m_spec.precision = std::stoll(std::string(value)); } void fmt_parser_t::parse_type() { + if (!has_next()) + { + std::cerr << "Missing token when parsing type" << std::endl; + std::exit(EXIT_FAILURE); + } + auto [_, value] = next(); + if (value.size() != 1) + { + std::cerr << "Type contains more than one character, we are not sure how to interpret this value '" << value << "'\n"; + std::exit(EXIT_FAILURE); + } + m_spec.uppercase = std::isupper(value.front()); + switch (value.front()) + { + case 'b': + case 'B': + m_spec.type = fmt_type_t::BINARY; + break; + case 'c': + m_spec.type = fmt_type_t::CHAR; + break; + case 'd': + m_spec.type = fmt_type_t::DECIMAL; + break; + case 'o': + m_spec.type = fmt_type_t::OCTAL; + break; + case 'x': + case 'X': + m_spec.type = fmt_type_t::HEX; + break; + case 'a': + case 'A': + m_spec.type = fmt_type_t::HEX_FLOAT; + break; + case 'e': + case 'E': + m_spec.type = fmt_type_t::EXPONENT; + break; + case 'f': + case 'F': + m_spec.type = fmt_type_t::FIXED_POINT; + break; + case 'g': + case 'G': + m_spec.type = fmt_type_t::GENERAL; + break; + default: + std::cerr << "Invalid type " << value << std::endl; + std::exit(EXIT_FAILURE); + } } } diff --git a/src/blt/logging/logging.cpp b/src/blt/logging/logging.cpp index 4e9309b..971269f 100644 --- a/src/blt/logging/logging.cpp +++ b/src/blt/logging/logging.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 @@ -35,25 +36,63 @@ namespace blt::logging return str; } - void logger_t::handle_fmt(const std::string_view fmt) + std::pair> logger_t::handle_fmt(const std::string_view fmt) { - std::cout << fmt << std::endl; + const auto spec = m_parser.parse(fmt); + if (spec.leading_zeros) + m_stream << std::setfill('0'); + else + m_stream << std::setfill(' '); + if (spec.width > 0) + m_stream << std::setw(static_cast(spec.width)); + else + m_stream << std::setw(0); + if (spec.precision > 0) + m_stream << std::setprecision(static_cast(spec.precision)); + else + m_stream << std::setprecision(2); + if (spec.uppercase) + m_stream << std::uppercase; + else + m_stream << std::nouppercase; + std::optional type; + switch (spec.type) + { + case fmt_type_t::BINARY: + case fmt_type_t::CHAR: + case fmt_type_t::GENERAL: + type = spec.type; + break; + case fmt_type_t::DECIMAL: + m_stream << std::dec; + break; + case fmt_type_t::OCTAL: + m_stream << std::oct; + break; + case fmt_type_t::HEX: + m_stream << std::hex; + break; + case fmt_type_t::HEX_FLOAT: + m_stream << std::hexfloat; + break; + case fmt_type_t::EXPONENT: + m_stream << std::scientific; + break; + case fmt_type_t::FIXED_POINT: + m_stream << std::fixed; + break; + } + return {spec.arg_id, type}; } - const std::string& logger_t::get(const size_t index) + void logger_t::exponential() { - if (index >= m_args_to_str.size()) - { - std::cerr << "Insufficient number of arguments provided to format string '" << m_fmt << "' got "; - for (const auto& [i, arg] : enumerate(std::as_const(m_args_to_str))) - { - std::cerr << '\'' << arg << "'"; - if (i != m_args_to_str.size() - 1) - std::cerr << " "; - } - std::exit(EXIT_FAILURE); - } - return m_args_to_str[index]; + m_stream << std::scientific; + } + + void logger_t::fixed() + { + m_stream << std::fixed; } void logger_t::compile(std::string fmt) @@ -61,13 +100,15 @@ namespace blt::logging m_fmt = std::move(fmt); m_last_fmt_pos = 0; m_arg_pos = 0; + m_stream.str(""); + m_stream.clear(); } - bool logger_t::consume_until_fmt() + std::optional> logger_t::consume_until_fmt() { const auto begin = m_fmt.find('{', m_last_fmt_pos); if (begin == std::string::npos) - return false; + return {}; const auto end = m_fmt.find('}', begin); if (end == std::string::npos) { @@ -75,25 +116,12 @@ namespace blt::logging std::exit(EXIT_FAILURE); } m_stream << std::string_view(m_fmt.data() + static_cast(m_last_fmt_pos), begin - m_last_fmt_pos);\ - if (end - begin > 1) - handle_fmt(std::string_view(m_fmt.data() + static_cast(begin + 1), end - begin - 1)); - else - { - // no arguments, must consume from args - m_stream << get(m_arg_pos++); - } m_last_fmt_pos = end + 1; - return true; + return std::pair{begin, end}; } void logger_t::finish() { - m_stream.str(""); - m_stream.clear(); - - while (consume_until_fmt()) - {} - m_stream << std::string_view(m_fmt.data() + static_cast(m_last_fmt_pos), m_fmt.size() - m_last_fmt_pos); m_last_fmt_pos = m_fmt.size(); } diff --git a/tests/logger_tests.cpp b/tests/logger_tests.cpp index e0de5c7..77e8046 100644 --- a/tests/logger_tests.cpp +++ b/tests/logger_tests.cpp @@ -27,5 +27,10 @@ int main() blt::logging::println("This is a println with args '{}'", 42); blt::logging::println("This is a println with multiple args '{}' '{}' '{}'", 42, 32.34231233, "Hello World!"); blt::logging::println("This is a '{1}' fmt string with positionals '{0}'", "I am a string!", "Well so am I except cooler :3"); + blt::logging::println("This is a println with a sign {:+}", 4120); + blt::logging::println("This is a println with a space {: }", 4120); + blt::logging::println("This is a println with a with {:3}", 4120); + blt::logging::println("This is a println with a with leading zeros {:010}", 4120); + blt::logging::println("This is a println with a precision {:.3}", 42.232342349); // blt::logging::println("This is println {}\twith a std::endl in the middle of it"); }