upgrade core add header check and incrace upload speed
This commit is contained in:
+157
-1
@@ -5,6 +5,7 @@
|
||||
#include <fmt/args.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <array>
|
||||
|
||||
// 格式化用的辅助函数
|
||||
|
||||
@@ -168,6 +169,144 @@ std::string uns::toBinary(unsigned long number, int bits)
|
||||
return "0b" + res;
|
||||
}
|
||||
|
||||
bool StartsWith(std::string_view str, std::string_view prefix)
|
||||
{
|
||||
if (str.size() < prefix.size())
|
||||
return false;
|
||||
return str.compare(0, prefix.size(), prefix) == 0;
|
||||
}
|
||||
|
||||
bool IsSizeUnit(std::string_view str)
|
||||
{
|
||||
if (str == "bit")
|
||||
return true;
|
||||
if (str.empty())
|
||||
return false;
|
||||
char last = str.back();
|
||||
return last == 'b' || last == 'B';
|
||||
}
|
||||
|
||||
bool IsNonNegativeInteger(std::string_view str, int& value)
|
||||
{
|
||||
if (str.empty())
|
||||
return false;
|
||||
int result = 0;
|
||||
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
|
||||
if (ec != std::errc() || ptr != str.data() + str.size())
|
||||
return false;
|
||||
if (result < 0)
|
||||
return false;
|
||||
value = result;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> ParseSizeFormat(std::string_view input)
|
||||
{
|
||||
constexpr std::pair<std::string_view, int> default_value = { "B", 2 };
|
||||
if (!StartsWith(input, "siz"))
|
||||
return { std::string(default_value.first), default_value.second };
|
||||
|
||||
std::string_view body = input.substr(3);
|
||||
if (body.empty() || body.front() != '-')
|
||||
return { std::string(default_value.first), default_value.second };
|
||||
|
||||
body.remove_prefix(1);
|
||||
size_t first_dash = body.find('-');
|
||||
if (first_dash == std::string_view::npos)
|
||||
{
|
||||
// siz-xx 或 siz-x
|
||||
if (IsSizeUnit(body))
|
||||
return { std::string(body), 2 };
|
||||
int precision = 0;
|
||||
if (IsNonNegativeInteger(body, precision))
|
||||
return { "B", precision };
|
||||
return { std::string(default_value.first), default_value.second };
|
||||
}
|
||||
|
||||
// siz-xx-y
|
||||
std::string_view unit = body.substr(0, first_dash);
|
||||
std::string_view precision_str = body.substr(first_dash + 1);
|
||||
if (!IsSizeUnit(unit))
|
||||
return { std::string(default_value.first), default_value.second };
|
||||
int precision = 0;
|
||||
if (!IsNonNegativeInteger(precision_str, precision))
|
||||
return { std::string(default_value.first), default_value.second };
|
||||
return { std::string(unit), precision };
|
||||
}
|
||||
|
||||
std::string FormatFileSize(size_t size, const std::string& unit, int precision)
|
||||
{
|
||||
static constexpr double K = 1024.0;
|
||||
static constexpr std::array<const char*, 9> units =
|
||||
{
|
||||
"B",
|
||||
"KB",
|
||||
"MB",
|
||||
"GB",
|
||||
"TB",
|
||||
"PB",
|
||||
"EB",
|
||||
"ZB",
|
||||
"YB"
|
||||
};
|
||||
|
||||
static constexpr std::array<const char*, 9> iec_units =
|
||||
{
|
||||
"B",
|
||||
"KIB",
|
||||
"MIB",
|
||||
"GIB",
|
||||
"TIB",
|
||||
"PIB",
|
||||
"EIB",
|
||||
"ZIB",
|
||||
"YIB"
|
||||
};
|
||||
|
||||
std::string input_unit = unit;
|
||||
std::transform(input_unit.begin(), input_unit.end(), input_unit.begin(), [] (unsigned char c)
|
||||
{
|
||||
return static_cast<char>(std::toupper(c));
|
||||
});
|
||||
|
||||
double bytes = static_cast<double>(size);
|
||||
if ((input_unit == "BIT") || (input_unit == "BITS"))
|
||||
bytes /= 8.0;
|
||||
else
|
||||
{
|
||||
size_t unit_index = 0;
|
||||
bool found = false;
|
||||
for (size_t i = 0; i < units.size(); ++i)
|
||||
{
|
||||
if ((input_unit == units[i]) || (input_unit == iec_units[i]))
|
||||
{
|
||||
unit_index = i;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
return std::format("{} {}", size, input_unit);
|
||||
for (size_t i = 0; i < unit_index; ++i)
|
||||
bytes *= K;
|
||||
}
|
||||
|
||||
size_t output_index = 0;
|
||||
while ((bytes >= K) && (output_index < (units.size() - 1)))
|
||||
{
|
||||
bytes /= K;
|
||||
++output_index;
|
||||
}
|
||||
if (precision < 0)
|
||||
precision = 0;
|
||||
|
||||
if (std::fabs(bytes - std::round(bytes)) < std::numeric_limits<double>::epsilon())
|
||||
return fmt::format("{} {}", static_cast<size_t>(std::round(bytes)), units[output_index]);
|
||||
|
||||
std::string value = fmt::format("{:.{}f}", bytes, precision);
|
||||
return fmt::format("{} {}", value, units[output_index]);
|
||||
}
|
||||
|
||||
std::string ServerLogger::GenerateLogHeader(uns::ServerLogLevel LogLevel)
|
||||
{
|
||||
std::string hstr;
|
||||
@@ -245,7 +384,7 @@ inline std::string StripAnsiCodes(const std::string& input) noexcept
|
||||
catch (...)
|
||||
{
|
||||
// 极罕见的内存耗尽情况,直接降级返回原串或空串,绝不崩溃
|
||||
return input;
|
||||
return input;
|
||||
}
|
||||
bool in_escape = false;
|
||||
for (char c : input)
|
||||
@@ -411,6 +550,18 @@ void ServerLogger::RotateIfNeeded(std::time_t now, bool check_size_after_write)
|
||||
}
|
||||
}
|
||||
|
||||
size_t GetUnsignedInteger(const uns::LogVariant& value)
|
||||
{
|
||||
return std::visit([] (const auto& v) -> size_t
|
||||
{
|
||||
using T = std::decay_t<decltype(v)>;
|
||||
if constexpr (std::is_integral_v<T> && !std::is_same_v<T, char> && !std::is_same_v<T, bool>)
|
||||
return static_cast<size_t>(v);
|
||||
else
|
||||
return 0;
|
||||
}, value);
|
||||
}
|
||||
|
||||
inline std::string RewriteFormatString(const std::string& real_format, const uns::LogArg* args, size_t count, fmt::dynamic_format_arg_store<fmt::format_context>& store)
|
||||
{
|
||||
std::string out;
|
||||
@@ -463,6 +614,11 @@ inline std::string RewriteFormatString(const std::string& real_format, const uns
|
||||
else
|
||||
store.push_back(args[arg_index]);
|
||||
}
|
||||
else if (StartsWith(inside, "siz"))
|
||||
{
|
||||
auto [u, p] = ParseSizeFormat(inside);
|
||||
store.push_back(FormatFileSize(GetUnsignedInteger(arg.value), u, p));
|
||||
}
|
||||
else
|
||||
store.push_back(args[arg_index]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user