Use str_format_v for log_log_impl

Reduces duplicate code. This also ensures that the log messages are properly zero-terminated and that they do not end with truncated unicode characters.
This commit is contained in:
Robert Müller 2023-03-19 11:19:50 +01:00
parent 854445d4e1
commit baa448580a

View file

@ -73,6 +73,10 @@ void log_set_scope_logger(ILogger *logger)
}
}
// Separate declaration, as attributes are not allowed on function definitions
void log_log_impl(LEVEL level, bool have_color, LOG_COLOR color, const char *sys, const char *fmt, va_list args)
GNUC_ATTRIBUTE((format(printf, 5, 0)));
void log_log_impl(LEVEL level, bool have_color, LOG_COLOR color, const char *sys, const char *fmt, va_list args)
{
if(level > loglevel.load(std::memory_order_acquire))
@ -109,18 +113,7 @@ void log_log_impl(LEVEL level, bool have_color, LOG_COLOR color, const char *sys
char *pMessage = Msg.m_aLine + Msg.m_LineMessageOffset;
int MessageSize = sizeof(Msg.m_aLine) - Msg.m_LineMessageOffset;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
#if defined(CONF_FAMILY_WINDOWS)
_vsprintf_p(pMessage, MessageSize, fmt, args);
#else
vsnprintf(pMessage, MessageSize, fmt, args);
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
str_format_v(pMessage, MessageSize, fmt, args);
Msg.m_LineLength = str_length(Msg.m_aLine);
scope_logger->Log(&Msg);
in_logger = false;