diff options
author | Shauren <shauren.trinity@gmail.com> | 2024-10-16 18:58:45 +0200 |
---|---|---|
committer | Ovahlord <dreadkiller@gmx.de> | 2024-10-17 21:44:13 +0200 |
commit | 03312d4aa47995f5e9e48609b6dea5b385e2199e (patch) | |
tree | c6dbcc09e4e8ea2a910ec52a89c41588bfd28030 | |
parent | fa5ba783853cb640a810be7c3d1e10d77cd53708 (diff) |
Core/Misc: Replace std::ostringstream based formatting with Trinity::StringFormat where possible in common and database projects
(cherry picked from commit 1a41281e37efdc4be602066dc3fdba12b24d1d0f)
-rw-r--r-- | src/common/DataStores/DB2FileLoader.cpp | 35 | ||||
-rw-r--r-- | src/common/Logging/Log.cpp | 23 | ||||
-rw-r--r-- | src/common/Utilities/Util.cpp | 105 | ||||
-rw-r--r-- | src/server/database/Database/DatabaseWorkerPool.cpp | 5 |
4 files changed, 48 insertions, 120 deletions
diff --git a/src/common/DataStores/DB2FileLoader.cpp b/src/common/DataStores/DB2FileLoader.cpp index a0cc609250e..12df8086a45 100644 --- a/src/common/DataStores/DB2FileLoader.cpp +++ b/src/common/DataStores/DB2FileLoader.cpp @@ -19,12 +19,13 @@ #include "ByteConverter.h" #include "DB2Meta.h" #include "Errors.h" -#include "Log.h" +#include "StringFormat.h" #include <fmt/ranges.h> #include <limits> -#include <sstream> #include <system_error> +#include <unordered_map> #include <utility> +#include <vector> #include <cstring> enum class DB2ColumnCompression : uint32 @@ -514,19 +515,14 @@ char* DB2FileLoaderRegularImpl::AutoProduceStrings(char** indexTable, uint32 ind { if (!(_header->Locale & (1 << locale))) { - char const* sep = ""; - std::ostringstream str; + std::array<char const*, TOTAL_LOCALES> detectedLocales; + auto itr = detectedLocales.begin(); for (uint32 i = 0; i < TOTAL_LOCALES; ++i) - { if (_header->Locale & (1 << i)) - { - str << sep << localeNames[i]; - sep = ", "; - } - } + *itr++ = localeNames[i]; - TC_LOG_ERROR("", "Attempted to load {} which has locales {} as {}. Check if you placed your localized db2 files in correct directory.", _fileName, str.str(), localeNames[locale]); - return nullptr; + throw DB2FileLoadException(Trinity::StringFormat("Attempted to load {} which has locales {} as {}. Check if you placed your localized db2 files in correct directory.", + _fileName, fmt::join(detectedLocales.begin(), itr, ", "), localeNames[locale])); } if (!_loadInfo->GetStringFieldCount(false)) @@ -1172,19 +1168,14 @@ char* DB2FileLoaderSparseImpl::AutoProduceStrings(char** indexTable, uint32 inde if (!(_header->Locale & (1 << locale))) { - char const* sep = ""; - std::ostringstream str; + std::array<char const*, TOTAL_LOCALES> detectedLocales; + auto itr = detectedLocales.begin(); for (uint32 i = 0; i < TOTAL_LOCALES; ++i) - { if (_header->Locale & (1 << i)) - { - str << sep << localeNames[i]; - sep = ", "; - } - } + *itr++ = localeNames[i]; - TC_LOG_ERROR("", "Attempted to load {} which has locales {} as {}. Check if you placed your localized db2 files in correct directory.", _fileName, str.str(), localeNames[locale]); - return nullptr; + throw DB2FileLoadException(Trinity::StringFormat("Attempted to load {} which has locales {} as {}. Check if you placed your localized db2 files in correct directory.", + _fileName, fmt::join(detectedLocales.begin(), itr, ", "), localeNames[locale])); } uint32 records = _catalog.size(); diff --git a/src/common/Logging/Log.cpp b/src/common/Logging/Log.cpp index feefce33f1e..b64aec29866 100644 --- a/src/common/Logging/Log.cpp +++ b/src/common/Logging/Log.cpp @@ -27,7 +27,6 @@ #include "Strand.h" #include "StringConvert.h" #include "Util.h" -#include <sstream> Log::Log() : AppenderId(0), lowestLogLevel(LOG_LEVEL_FATAL), _ioContext(nullptr), _strand(nullptr) { @@ -265,27 +264,7 @@ Logger const* Log::GetLoggerByType(std::string_view type) const std::string Log::GetTimestampStr() { time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - - std::tm aTm; - localtime_r(&tt, &aTm); - - // YYYY year - // MM month (2 digits 01-12) - // DD day (2 digits 01-31) - // HH hour (2 digits 00-23) - // MM minutes (2 digits 00-59) - // SS seconds (2 digits 00-59) - try - { - return Trinity::StringFormat("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}", - aTm.tm_year + 1900, aTm.tm_mon + 1, aTm.tm_mday, aTm.tm_hour, aTm.tm_min, aTm.tm_sec); - } - catch (std::exception const& ex) - { - fprintf(stderr, "Failed to initialize timestamp part of log filename! %s", ex.what()); - fflush(stderr); - ABORT(); - } + return TimeToTimestampStr(tt); } bool Log::SetLogLevel(std::string const& name, int32 newLeveli, bool isLogger /* = true */) diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 9730c169832..ddf649ff663 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -24,8 +24,6 @@ #include <boost/core/demangle.hpp> #include <utf8.h> #include <algorithm> -#include <iomanip> -#include <sstream> #include <string> #include <cctype> #include <cstdarg> @@ -131,86 +129,49 @@ std::string secsToTimeString(uint64 timeInSecs, TimeFormat timeFormat, bool hour return Trinity::StringFormat("0:{:02}", secs); } - std::ostringstream ss; - if (days) + std::string result; + if (timeFormat == TimeFormat::ShortText) { - ss << days; - switch (timeFormat) + std::back_insert_iterator<std::string> itr = std::back_inserter(result); + if (days) + Trinity::StringFormatTo(itr, "{}d", days); + if (hours || hoursOnly) + Trinity::StringFormatTo(itr, "{}h", hours); + if (!hoursOnly) { - case TimeFormat::ShortText: - ss << "d"; - break; - case TimeFormat::FullText: - if (days == 1) - ss << " Day "; - else - ss << " Days "; - break; - default: - return "<Unknown time format>"; + if (minutes) + Trinity::StringFormatTo(itr, "{}m", minutes); + if (secs || result.empty()) + Trinity::StringFormatTo(itr, "{}s", secs); } } - - if (hours || hoursOnly) + else if (timeFormat == TimeFormat::FullText) { - ss << hours; - switch (timeFormat) + auto formatTimeField = [](std::string& result, uint64 value, std::string_view label) { - case TimeFormat::ShortText: - ss << "h"; - break; - case TimeFormat::FullText: - if (hours <= 1) - ss << " Hour "; - else - ss << " Hours "; - break; - default: - return "<Unknown time format>"; - } - } - if (!hoursOnly) - { - if (minutes) - { - ss << minutes; - switch (timeFormat) - { - case TimeFormat::ShortText: - ss << "m"; - break; - case TimeFormat::FullText: - if (minutes == 1) - ss << " Minute "; - else - ss << " Minutes "; - break; - default: - return "<Unknown time format>"; - } - } - - if (secs || (!days && !hours && !minutes)) + if (!result.empty()) + result.append(1, ' '); + Trinity::StringFormatTo(std::back_inserter(result), "{} {}", value, label); + if (value != 1) + result.append(1, 's'); + }; + if (days) + formatTimeField(result, days, "Day"); + if (hours || hoursOnly) + formatTimeField(result, hours, "Hour"); + if (!hoursOnly) { - ss << secs; - switch (timeFormat) - { - case TimeFormat::ShortText: - ss << "s"; - break; - case TimeFormat::FullText: - if (secs <= 1) - ss << " Second."; - else - ss << " Seconds."; - break; - default: - return "<Unknown time format>"; - } + if (minutes) + formatTimeField(result, minutes, "Minute"); + if (secs || result.empty()) + formatTimeField(result, secs, "Second"); } + result.append(1, '.'); } + else + result = "<Unknown time format>"; - return ss.str(); + return result; } Optional<int64> MoneyStringToMoney(std::string const& moneyString) diff --git a/src/server/database/Database/DatabaseWorkerPool.cpp b/src/server/database/Database/DatabaseWorkerPool.cpp index 41fe3028a05..8476851da5d 100644 --- a/src/server/database/Database/DatabaseWorkerPool.cpp +++ b/src/server/database/Database/DatabaseWorkerPool.cpp @@ -37,7 +37,6 @@ #include <mysqld_error.h> #include <utility> #ifdef TRINITY_DEBUG -#include <sstream> #include <boost/stacktrace.hpp> #endif @@ -503,9 +502,7 @@ T* DatabaseWorkerPool<T>::GetFreeConnection() #ifdef TRINITY_DEBUG if (WarnSyncQueries<T>) { - std::ostringstream ss; - ss << boost::stacktrace::stacktrace(); - TC_LOG_WARN("sql.performances", "Sync query at:\n{}", ss.str()); + TC_LOG_WARN("sql.performances", "Sync query at:\n{}", boost::stacktrace::to_string(boost::stacktrace::stacktrace())); } #endif |