From dd030a9b5be5641626f869c09843b24a5809b581 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Sat, 1 Mar 2025 21:56:57 -0500 Subject: [PATCH] silly code --- CMakeLists.txt | 1 + include/blt/logging/logging.h | 58 +++++++++++++++++++++++++++++++++-- src/blt/logging/logging.cpp | 24 ++++++++++++++- src/blt/logging/status.cpp | 2 +- tests/logger_tests.cpp | 25 +++++++++++++++ 5 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 tests/logger_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c5e4dbb..71074d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -204,6 +204,7 @@ if (${BUILD_TESTS}) blt_add_test(blt_iterator tests/iterator_tests.cpp test) blt_add_test(blt_argparse tests/argparse_tests.cpp test) + blt_add_test(blt_logging tests/logger_tests.cpp test) message("Built tests") endif () diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index da4b5f2..02535c8 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -19,16 +19,70 @@ #ifndef BLT_LOGGING_LOGGING_H #define BLT_LOGGING_LOGGING_H +#include +#include #include +#include namespace blt::logging { + struct logger_t + { + explicit logger_t(std::string fmt): fmt(std::move(fmt)) + { + } + + template + std::string make_string(T&& t) + { + if constexpr (std::is_same_v || std::is_convertible_v) + return std::forward(t); + else if constexpr (std::is_same_v || std::is_same_v, char*> || std::is_convertible_v< + T, std::string_view>) + return std::string(std::forward(t)); + else if constexpr (std::is_same_v) + return std::string() + std::forward(t); + else if constexpr (std::is_arithmetic_v) + return std::to_string(std::forward(t)); + else + { + BLT_UNREACHABLE; + } + } + + void compile(); + + void insert_next_value(const std::string& arg); + + template + const std::string& log(Args&&... args) + { + (insert_next_value(make_string(std::forward(args))), ...); + return fmt; + } + + private: + std::string fmt; + }; + + void print(const std::string& fmt); + + void newline(); template - void log(const std::string& str, Args&&... args){ - + void print(std::string fmt, Args&&... args) + { + logger_t logger{std::move(fmt)}; + logger.compile(); + print(logger.log(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/logging.cpp b/src/blt/logging/logging.cpp index 5f723e8..6de83f2 100644 --- a/src/blt/logging/logging.cpp +++ b/src/blt/logging/logging.cpp @@ -16,8 +16,30 @@ * along with this program. If not, see . */ #include +#include -namespace blt +namespace blt::logging { + void logger_t::compile() + { + } + + void logger_t::insert_next_value(const std::string& arg) + { + const auto begin = fmt.find('{'); + const auto end = fmt.find('}', begin); + fmt.erase(fmt.begin() + static_cast(begin), fmt.begin() + static_cast(end) + 1); + fmt.insert(begin, arg); + } + + void print(const std::string& fmt) + { + std::cout << fmt; + } + + void newline() + { + std::cout << std::endl; + } } diff --git a/src/blt/logging/status.cpp b/src/blt/logging/status.cpp index 9ff1411..8fc772b 100644 --- a/src/blt/logging/status.cpp +++ b/src/blt/logging/status.cpp @@ -17,7 +17,7 @@ */ #include -namespace blt +namespace blt::logging { } \ No newline at end of file diff --git a/tests/logger_tests.cpp b/tests/logger_tests.cpp new file mode 100644 index 0000000..7e0e946 --- /dev/null +++ b/tests/logger_tests.cpp @@ -0,0 +1,25 @@ +/* + * + * Copyright (C) 2025 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +int main() +{ + blt::logging::println("This is a println!"); + 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!"); +} \ No newline at end of file