Logging: use "%.*s" instead of "%s" for strings of known length

This commit is contained in:
livinskyov
2026-09-27 16:44:36 +01:00
committed by Gleb Mazovetskiy
parent 97c93db93b
commit 81c9ab4533
3 changed files with 17 additions and 13 deletions
+4 -2
View File
@@ -71,13 +71,15 @@ int tcp_client::join(std::string_view addrstr)
asio::error_code errorCode;
const asio::ip::basic_resolver_results<asio::ip::tcp> range = resolver.resolve(host, port, errorCode);
if (errorCode) {
SDL_SetError("%s", errorCode.message().c_str());
const std::string message = errorCode.message();
SDL_SetError("%.*s", static_cast<int>(message.size()), message.c_str());
return -1;
}
asio::connect(sock, range, errorCode);
if (errorCode) {
SDL_SetError("%s", errorCode.message().c_str());
const std::string message = errorCode.message();
SDL_SetError("%.*s", static_cast<int>(message.size()), message.c_str());
return -1;
}
+3 -2
View File
@@ -135,10 +135,11 @@ void LuaLogMessage(LogPriority priority, std::string_view fmt, sol::variadic_arg
const std::expected<std::string, std::string> formatted = FormatLuaMessage(fmt, formatArgs);
if (!formatted.has_value()) {
const std::string fullError = StrCat("Format error, fmt: ", fmt, " error: ", formatted.error());
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", fullError.c_str());
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%.*s", static_cast<int>(fullError.size()), fullError.c_str());
return;
}
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, static_cast<SDL_LogPriority>(priority), "%s", formatted->c_str());
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, static_cast<SDL_LogPriority>(priority),
"%.*s", static_cast<int>(formatted->size()), formatted->c_str());
}
void LuaLogInfo(std::string_view fmt, sol::variadic_args args)
+10 -9
View File
@@ -56,7 +56,7 @@ std::string format(std::string_view fmt, Args &&...args)
return std::vformat(fmt, std::make_format_args(args...));
} catch (const std::format_error &e) {
const std::string fullError = StrCat("Format error, fmt: ", fmt, " error: ", e.what());
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", fullError.c_str());
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%.*s", static_cast<int>(fullError.size()), fullError.c_str());
app_fatal(fullError);
}
#else
@@ -76,7 +76,7 @@ template <typename... Args>
void Log(std::string_view fmt, Args &&...args)
{
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_Log("%s", str.c_str());
SDL_Log("%.*s", static_cast<int>(str.size()), str.c_str());
}
inline void LogVerbose(LogCategory category, std::string_view str)
@@ -98,7 +98,7 @@ void LogVerbose(LogCategory category, std::string_view fmt, Args &&...args)
{
if (!IsLogLevel(category, SDL_LOG_PRIORITY_VERBOSE)) return;
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogVerbose(static_cast<int>(category), "%s", str.c_str());
SDL_LogVerbose(static_cast<int>(category), "%.*s", static_cast<int>(str.size()), str.c_str());
}
template <typename... Args>
@@ -117,7 +117,7 @@ void LogDebug(LogCategory category, std::string_view fmt, Args &&...args)
{
if (!IsLogLevel(category, SDL_LOG_PRIORITY_DEBUG)) return;
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogDebug(static_cast<int>(category), "%s", str.c_str());
SDL_LogDebug(static_cast<int>(category), "%.*s", static_cast<int>(str.size()), str.c_str());
}
template <typename... Args>
@@ -135,7 +135,7 @@ template <typename... Args>
void LogInfo(LogCategory category, std::string_view fmt, Args &&...args)
{
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogInfo(static_cast<int>(category), "%s", str.c_str());
SDL_LogInfo(static_cast<int>(category), "%.*s", static_cast<int>(str.size()), str.c_str());
}
template <typename... Args>
@@ -153,7 +153,7 @@ template <typename... Args>
void LogWarn(LogCategory category, std::string_view fmt, Args &&...args)
{
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogWarn(static_cast<int>(category), "%s", str.c_str());
SDL_LogWarn(static_cast<int>(category), "%.*s", static_cast<int>(str.size()), str.c_str());
}
template <typename... Args>
@@ -171,7 +171,7 @@ template <typename... Args>
void LogError(LogCategory category, std::string_view fmt, Args &&...args)
{
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogError(static_cast<int>(category), "%s", str.c_str());
SDL_LogError(static_cast<int>(category), "%.*s", static_cast<int>(str.size()), str.c_str());
}
template <typename... Args>
@@ -189,7 +189,7 @@ template <typename... Args>
void LogCritical(LogCategory category, std::string_view fmt, Args &&...args)
{
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogCritical(static_cast<int>(category), "%s", str.c_str());
SDL_LogCritical(static_cast<int>(category), "%.*s", static_cast<int>(str.size()), str.c_str());
}
template <typename... Args>
@@ -208,7 +208,8 @@ template <typename... Args>
void LogMessageV(LogCategory category, LogPriority priority, std::string_view fmt, Args &&...args)
{
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogMessageV(static_cast<int>(category), static_cast<SDL_LogPriority>(priority), "%s", str.c_str());
SDL_LogMessageV(static_cast<int>(category), static_cast<SDL_LogPriority>(priority),
"%.*s", static_cast<int>(str.size()), str.c_str());
}
template <typename... Args>