diff --git a/CMakeLists.txt b/CMakeLists.txt index cba9a6d..9ad00ae 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.3) +set(BLT_VERSION 5.2.4) set(BLT_TARGET BLT) diff --git a/include/blt/fs/path_helper.h b/include/blt/fs/path_helper.h index 530eb1a..40a01fe 100644 --- a/include/blt/fs/path_helper.h +++ b/include/blt/fs/path_helper.h @@ -28,6 +28,12 @@ namespace blt::fs std::string base_name(const std::string& str); std::string_view base_name_sv(std::string_view str); + std::string filename(const std::string& str); + std::string_view filename_sv(std::string_view str); + + std::string extension(const std::string& str); + std::string_view extension_sv(std::string_view str); + } #endif //BLT_FS_PATH_HELPER_H diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index ba091aa..0d41cc2 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -168,6 +168,8 @@ namespace blt::logging logging_config_t& get_global_config(); + std::ostream& get_local_stream(); + void set_thread_name(const std::string& name); const std::string& get_thread_name(); @@ -200,7 +202,7 @@ namespace blt::logging stream << std::endl; } - template + template void log(log_level_t level, const char* file, const i32 line, std::string fmt, Args&&... args) { auto& logger = get_global_logger(); @@ -212,9 +214,62 @@ namespace blt::logging } namespace detail - {} + { + + } } +#if defined(__clang__) || defined(__llvm__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" +#endif + +#ifdef BLT_DISABLE_LOGGING +#define BLT_LOG(level, fmt, ...) + +#else #define BLT_LOG(level, fmt, ...) blt::logging::log(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__) +#ifdef BLT_DISABLE_TRACE +#define BLT_TRACE(format, ...) +#else +#define BLT_TRACE(format, ...) BLT_LOG(blt::logging::log_level_t::TRACE, format, ##__VA_ARGS__) +#endif + +#ifdef BLT_DISABLE_DEBUG +#define BLT_DEBUG(format, ...) +#else +#define BLT_DEBUG(format, ...) BLT_LOG(blt::logging::log_level_t::DEBUG, format, ##__VA_ARGS__) +#endif + +#ifdef BLT_DISABLE_INFO +#define BLT_INFO(format, ...) +#else +#define BLT_INFO(format, ...) BLT_LOG(blt::logging::log_level_t::INFO, format, ##__VA_ARGS__) +#endif + +#ifdef BLT_DISABLE_WARN +#define BLT_WARN(format, ...) +#else +#define BLT_WARN(format, ...) BLT_LOG(blt::logging::log_level_t::WARN, format, ##__VA_ARGS__) +#endif + +#ifdef BLT_DISABLE_ERROR +#define BLT_ERROR(format, ...) +#else +#define BLT_ERROR(format, ...) BLT_LOG(blt::logging::log_level_t::ERROR, format, ##__VA_ARGS__) +#endif + +#ifdef BLT_DISABLE_FATAL +#define BLT_FATAL(format, ...) +#else +#define BLT_FATAL(format, ...) BLT_LOG(blt::logging::log_level_t::FATAL, format, ##__VA_ARGS__) +#endif + +#endif + +#if defined(__clang__) || defined(__llvm__) +#pragma clang diagnostic pop +#endif + #endif // BLT_LOGGING_LOGGING_H diff --git a/include/blt/logging/logging_config.h b/include/blt/logging/logging_config.h index 1ce8656..3859ad7 100644 --- a/include/blt/logging/logging_config.h +++ b/include/blt/logging/logging_config.h @@ -216,6 +216,8 @@ namespace blt::logging // This creates output where the user message always starts at the same column. bool m_ensure_alignment = true; + size_t m_longest_name_length = 0; + static std::string get_default_log_format(); static std::vector get_default_log_outputs(); static std::array get_default_log_level_colors(); diff --git a/src/blt/fs/path_helper.cpp b/src/blt/fs/path_helper.cpp index 9f62c16..bb2e210 100644 --- a/src/blt/fs/path_helper.cpp +++ b/src/blt/fs/path_helper.cpp @@ -38,4 +38,30 @@ namespace blt::fs const auto file_parts = string::split_sv(parts.back(), '.'); return file_parts.front(); } + + std::string filename(const std::string& str) + { + return std::string(filename_sv(str)); + } + + std::string_view filename_sv(const std::string_view str) + { + const auto parts = string::split_sv(str, delim); + return parts.back(); + } + + std::string extension(const std::string& str) + { + return std::string(extension_sv(str)); + } + + std::string_view extension_sv(const std::string_view str) + { + const auto parts = string::split_sv(str, delim); + const auto file_parts = parts.back().find_first_of('.'); + return parts.back().substr(std::min(file_parts + 1, parts.back().size())); + } } + + + diff --git a/src/blt/logging/logging.cpp b/src/blt/logging/logging.cpp index 5d52436..9cc26fb 100644 --- a/src/blt/logging/logging.cpp +++ b/src/blt/logging/logging.cpp @@ -37,6 +37,7 @@ namespace blt::logging struct logging_thread_context_t { std::stringstream stream; + std::stringstream logging_stream; std::string thread_name; logger_t logger{stream}; }; @@ -239,4 +240,11 @@ namespace blt::logging { return get_thread_context().thread_name; } + + std::ostream& get_local_stream() + { + auto& context = get_thread_context(); + context.logging_stream.str(""); + return context.logging_stream; + } } diff --git a/src/blt/logging/logging_config.cpp b/src/blt/logging/logging_config.cpp index fe15c92..ed29607 100644 --- a/src/blt/logging/logging_config.cpp +++ b/src/blt/logging/logging_config.cpp @@ -17,6 +17,7 @@ */ #include #include +#include #include #include #include @@ -91,6 +92,10 @@ namespace blt::logging m_log_tag_content.emplace_back(std::string_view(m_log_format.data() + i, m_log_format.size() - i)); m_log_tag_tokens.emplace_back(tags::detail::log_tag_token_t::CONTENT); } + + m_longest_name_length = 0; + for (const auto& name : m_log_level_names) + m_longest_name_length = std::max(m_longest_name_length, name.size()); } std::string add_year(const tm* current_time) @@ -187,10 +192,10 @@ namespace blt::logging } case tags::detail::log_tag_token_t::NS: { - auto str = std::to_string(nano_time % 1000); + auto str = std::to_string(nano_time % 1000000000ul); if (m_ensure_alignment) { - for (size_t i = str.size(); i < 4; ++i) + for (size_t i = str.size(); i < 9; ++i) str.insert(str.begin(), '0'); } fmt += str; @@ -198,24 +203,12 @@ namespace blt::logging } case tags::detail::log_tag_token_t::UNIX: { - auto str = std::to_string(millis_time % 1000); - if (m_ensure_alignment) - { - for (size_t i = str.size(); i < 4; ++i) - str.insert(str.begin(), '0'); - } - fmt += str; + fmt += std::to_string(millis_time); break; } case tags::detail::log_tag_token_t::UNIX_NANO: { - auto str = std::to_string(nano_time); - if (m_ensure_alignment) - { - for (size_t i = str.size(); i < 4; ++i) - str.insert(str.begin(), '0'); - } - fmt += str; + fmt += std::to_string(nano_time); break; } case tags::detail::log_tag_token_t::ISO_YEAR: @@ -275,7 +268,10 @@ namespace blt::logging fmt += thread_name; break; case tags::detail::log_tag_token_t::FILE: - fmt += file; + if (m_print_full_name) + fmt += file; + else + fmt += fs::filename_sv(file); break; case tags::detail::log_tag_token_t::LINE: fmt += std::to_string(line); @@ -294,7 +290,7 @@ namespace blt::logging 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 + return build(fg(ansi::color::color8_bright::BLUE)) + "[" + 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"; } diff --git a/tests/logger_tests.cpp b/tests/logger_tests.cpp index 03870f9..a9f8bb6 100644 --- a/tests/logger_tests.cpp +++ b/tests/logger_tests.cpp @@ -173,6 +173,15 @@ int main() blt::logging::println("Logged {} characters", charCount); + BLT_TRACE("Hello this is am empty trace!"); + BLT_TRACE("This is a trace with data {} {} {}", "bad at code", 413, "boy"); + + BLT_DEBUG("This is complete? {}", "this is working!"); + BLT_INFO("Hello there!"); + BLT_WARN("This is a warning!"); + BLT_ERROR("This is an error!"); + BLT_FATAL("This is a fatal error!"); + /*std::cout << "\033[2J"; constexpr int totalRows = 24; // std::cout << "\033[1;" << (totalRows - 1) << "r";