Browse Source

Format long log messages without truncating

Reuse the functionality from our string utilities to format log messages, which supports arbitrarily large strings.
Michael Ragazzon 6 months ago
parent
commit
9f2a313bdb
3 changed files with 12 additions and 15 deletions
  1. 5 0
      Include/RmlUi/Core/StringUtilities.h
  2. 4 12
      Source/Core/Log.cpp
  3. 3 3
      Source/Core/StringUtilities.cpp

+ 5 - 0
Include/RmlUi/Core/StringUtilities.h

@@ -31,6 +31,7 @@
 
 #include "Header.h"
 #include "Types.h"
+#include <stdarg.h>
 
 namespace Rml {
 
@@ -139,6 +140,10 @@ namespace StringUtilities {
 
 	/// Converts a byte offset of a UTF-8 string to a character position.
 	RMLUICORE_API int ConvertByteOffsetToCharacterOffset(StringView string, int byte_offset);
+
+	namespace Detail {
+		int FormatString(String& string, const char* format, va_list argument_list);
+	}
 } // namespace StringUtilities
 
 /*

+ 4 - 12
Source/Core/Log.cpp

@@ -38,24 +38,16 @@ namespace Rml {
 
 void Log::Message(Log::Type type, const char* fmt, ...)
 {
-	const int buffer_size = 1024;
-	char buffer[buffer_size];
+	String string;
 	va_list argument_list;
-
-	// Print the message to the buffer.
 	va_start(argument_list, fmt);
-	int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
-	if (len < 0 || len > buffer_size - 2)
-	{
-		len = buffer_size - 2;
-	}
-	buffer[len] = '\0';
+	StringUtilities::Detail::FormatString(string, fmt, argument_list);
 	va_end(argument_list);
 
 	if (SystemInterface* system_interface = GetSystemInterface())
-		system_interface->LogMessage(type, buffer);
+		system_interface->LogMessage(type, string);
 	else
-		LogDefault::LogMessage(type, buffer);
+		LogDefault::LogMessage(type, string);
 }
 
 void Log::ParseError(const String& filename, int line_number, const char* fmt, ...)

+ 3 - 3
Source/Core/StringUtilities.cpp

@@ -37,7 +37,7 @@
 
 namespace Rml {
 
-static int FormatString(String& string, const char* format, va_list argument_list)
+int StringUtilities::Detail::FormatString(String& string, const char* format, va_list argument_list)
 {
 	constexpr size_t InternalBufferSize = 256;
 	char buffer[InternalBufferSize];
@@ -85,7 +85,7 @@ int FormatString(String& string, const char* format, ...)
 {
 	va_list argument_list;
 	va_start(argument_list, format);
-	int result = FormatString(string, format, argument_list);
+	int result = StringUtilities::Detail::FormatString(string, format, argument_list);
 	va_end(argument_list);
 	return result;
 }
@@ -95,7 +95,7 @@ String CreateString(const char* format, ...)
 	String result;
 	va_list argument_list;
 	va_start(argument_list, format);
-	FormatString(result, format, argument_list);
+	StringUtilities::Detail::FormatString(result, format, argument_list);
 	va_end(argument_list);
 	return result;
 }