From ff2d77a1cd492a39fda0ec8ef3b31065e680b194 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 2 Mar 2025 21:21:19 -0500 Subject: [PATCH] this is a breaking change (logging) --- CMakeLists.txt | 2 +- commit.py.save | 74 ---------------------------- include/blt/logging/logging.h | 91 +++++++++++++++++------------------ include/blt/std/logging.h | 5 -- libraries/parallel-hashmap | 2 +- src/blt/logging/logging.cpp | 56 ++++++++++++++------- 6 files changed, 83 insertions(+), 147 deletions(-) delete mode 100644 commit.py.save diff --git a/CMakeLists.txt b/CMakeLists.txt index 71074d0..0b06a48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 4.0.38) +set(BLT_VERSION 5.0.0) set(BLT_TARGET BLT) diff --git a/commit.py.save b/commit.py.save deleted file mode 100644 index 3ecb2e0..0000000 --- a/commit.py.save +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/python3 - -import subprocess - -#--------------------------------------- -# CONFIG -#--------------------------------------- - -VERSION_BEGIN_STR = "set(BLT_VERSION " -VERSION_END_STR = ")" - -#--------------------------------------- -# DO NOT TOUCH -#--------------------------------------- - -type = input("What kind of commit is this ((M)ajor, (m)inor, (p)atch)? ") - -def load_cmake(): - cmake_file = open("CMakeLists.txt", 'r') - cmake_text = cmake_file.read() - cmake_file.close() - return cmake_text - -def write_cmake(cmake_text): - cmake_file = open("CMakeLists.txt", 'w') - cmake_file.write(cmake_text) - cmake_file.close() - -def get_version(cmake_text): - begin = cmake_text.find(VERSION_BEGIN_STR) + len(find_text) - end = cmake_text.find(VERSION_END_STR, begin) - return (cmake_text[begin:end], begin, end) - -def split_version(cmake_text): - version, begin, end = get_version(cmake_text) - version_parts = version.split('.') - return (version_parts, begin, end) - -def recombine(cmake_text, version_parts, begin, end): - constructed_version = version_parts[0] + '.' + version_parts[1] + '.' + version_parts[2] - constructed_text_begin = cmake_text[0:begin] - constrcuted_text_end = cmake_text[end::] - return constructed_text_begin + constructed_version + constrcuted_text_end - - -def inc_major(cmake_text): - version_parts, begin, end = split_version(cmake_text) - version_parts[0] = str(int(version_parts[0]) + 1) - return recombine(cmake_text, version_parts, begin, end) - -def inc_minor(cmake_text): - version_parts, begin, end = split_version(cmake_text) - version_parts[1] = str(int(version_parts[1]) + 1) - return recombine(cmake_text, version_parts, begin, end) - -def inc_patch(cmake_text): - version_parts, begin, end = split_version(cmake_text) - version_parts[2] = str(int(version_parts[2]) + 1) - return recombine(cmake_text, version_parts, begin, end) - -if type.startswith('M'): - print("Selected major") - write_cmake(inc_major(load_cmake())) -elif type.startswith('m'): - print("Selected minor") - write_cmake(inc_minor(load_cmake())) -elif type.startswith('p') or type.startswith('P') or len(type) == 0: - print("Selected patch") - write_cmake(inc_patch(load_cmake())) - -#subprocess.call("./py_commit_helper.sh") -subprocess.call("git", "add", "*") -subprocess.call("git", "commit") -subprocess.call("sh -e 'git remote | xargs -L1 git push --all'") diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index 02535c8..48648c5 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -21,68 +21,63 @@ #include #include +#include #include #include +#include namespace blt::logging { - struct logger_t - { - explicit logger_t(std::string fmt): fmt(std::move(fmt)) - { - } + struct logger_t + { + explicit logger_t() = default; - 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; - } - } + template + void print_value(T&& t) + { + static_assert(meta::is_streamable_v, "T must be streamable in order to work with blt::logging!"); + m_stream << std::forward(t); + } - void compile(); + template + std::string log(std::string fmt, Args&&... args) + { + compile(std::move(fmt)); + ((consume_until_fmt(), print_value(std::forward(args))), ...); + return to_string(); + } - void insert_next_value(const std::string& arg); + std::string to_string(); - template - const std::string& log(Args&&... args) - { - (insert_next_value(make_string(std::forward(args))), ...); - return fmt; - } + private: + void compile(std::string fmt); - private: - std::string fmt; - }; + void consume_until_fmt(); - void print(const std::string& fmt); + std::string m_fmt; + std::stringstream m_stream; + size_t m_last_fmt_pos = 0; + }; - void newline(); + void print(const std::string& fmt); - template - void print(std::string fmt, Args&&... args) - { - logger_t logger{std::move(fmt)}; - logger.compile(); - print(logger.log(std::forward(args)...)); - } + void newline(); - template - void println(std::string fmt, Args&&... args) - { - print(std::move(fmt), std::forward(args)...); - newline(); - } + logger_t& get_global_logger(); + + 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/include/blt/std/logging.h b/include/blt/std/logging.h index f03a7a6..42bc3f4 100644 --- a/include/blt/std/logging.h +++ b/include/blt/std/logging.h @@ -828,11 +828,6 @@ void flush() { std::cout.flush(); } -void newline() -{ - std::cout << std::endl; -} - } #endif diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 7ef2e73..93201da 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 +Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3 diff --git a/src/blt/logging/logging.cpp b/src/blt/logging/logging.cpp index 6de83f2..f16eac4 100644 --- a/src/blt/logging/logging.cpp +++ b/src/blt/logging/logging.cpp @@ -20,26 +20,46 @@ namespace blt::logging { - void logger_t::compile() - { + struct logging_thread_context_t + { + logger_t logger; + }; - } + std::string logger_t::to_string() + { + auto str = m_stream.str(); + m_stream.str(""); + m_stream.clear(); + return str; + } - 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 logger_t::compile(std::string fmt) + { + m_fmt = std::move(fmt); + m_last_fmt_pos = 0; + } - void print(const std::string& fmt) - { - std::cout << fmt; - } + void logger_t::consume_until_fmt() + { + const auto begin = m_fmt.find('{', m_last_fmt_pos); + const auto end = m_fmt.find('}', begin); + m_stream << std::string_view(m_fmt.data() + static_cast(m_last_fmt_pos), begin - m_last_fmt_pos); + m_last_fmt_pos = end; + } - void newline() - { - std::cout << std::endl; - } + logger_t& get_global_logger() + { + thread_local logging_thread_context_t context; + return context.logger; + } + + void print(const std::string& fmt) + { + std::cout << fmt; + } + + void newline() + { + std::cout << std::endl; + } }