From ea80856073d4896da5df78d60369b327346a0b56 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Sat, 11 Oct 2025 19:30:36 +0800 Subject: Split and log debug tag. --- include/cru/base/StringUtil.h | 47 +++++++++++++++++++++++++++++++++++++++++++ include/cru/base/log/Logger.h | 8 ++++++++ 2 files changed, 55 insertions(+) (limited to 'include/cru') diff --git a/include/cru/base/StringUtil.h b/include/cru/base/StringUtil.h index 8f085283..ef6359ff 100644 --- a/include/cru/base/StringUtil.h +++ b/include/cru/base/StringUtil.h @@ -1,10 +1,57 @@ #pragma once #include "Base.h" +#include "Bitmask.h" #include +#include #include +#include namespace cru { +namespace details { +struct SplitOptionsTag {}; +} // namespace details +using SplitOption = Bitmask; +struct SplitOptions { + static constexpr SplitOption RemoveEmpty = SplitOption::FromOffset(1); +}; + +template +std::vector Split(const TString& str, const TString& sep, + SplitOption options = {}) { + using size_type = typename TString::size_type; + + if (sep.empty()) throw std::invalid_argument("Sep can't be empty."); + if (str.empty()) return {}; + + size_type current_pos = 0; + std::vector result; + + while (current_pos != TString::npos) { + if (current_pos == str.size()) { + if (!options.Has(SplitOptions::RemoveEmpty)) { + result.push_back({}); + } + break; + } + + auto next_pos = str.find(sep, current_pos); + auto sub = str.substr(current_pos, next_pos - current_pos); + if (!(options.Has(SplitOptions::RemoveEmpty) && sub.empty())) { + result.push_back(sub); + } + current_pos = + next_pos == TString::npos ? TString::npos : next_pos + sep.size(); + } + + return result; +} + +inline std::vector Split(const char* str, const std::string& sep, + SplitOption options = {}) { + return Split(std::string(str), sep, options); +} + using CodePoint = std::int32_t; constexpr CodePoint k_invalid_code_point = -1; diff --git a/include/cru/base/log/Logger.h b/include/cru/base/log/Logger.h index 1ef53654..f44bad72 100644 --- a/include/cru/base/log/Logger.h +++ b/include/cru/base/log/Logger.h @@ -6,7 +6,9 @@ #include #include #include +#include #include +#include #include #include @@ -40,6 +42,11 @@ class CRU_BASE_API Logger : public Object2 { void AddLogTarget(std::unique_ptr source); void RemoveLogTarget(ILogTarget* source); + void AddDebugTag(std::string tag); + void RemoveDebugTag(const std::string& tag); + void LoadDebugTagFromEnv(const char* env_var = "CRU_LOG_DEBUG", + std::string sep = ","); + public: void Log(LogLevel level, std::string tag, std::string message) { Log(LogInfo(level, std::move(tag), std::move(message))); @@ -59,6 +66,7 @@ class CRU_BASE_API Logger : public Object2 { private: std::mutex log_queue_mutex_; + std::unordered_set debug_tags_; std::condition_variable log_queue_condition_variable_; std::list log_queue_; bool log_stop_; -- cgit v1.2.3