Create an Error object to represent GL errors.

BUG=angle:520

Change-Id: I1a54e6f308b5b0f310c725a7771af737a77ecd42
Reviewed-on: https://chromium-review.googlesource.com/209619
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Tested-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/common/debug.cpp b/src/common/debug.cpp
index d6eecf7..dcad327 100644
--- a/src/common/debug.cpp
+++ b/src/common/debug.cpp
@@ -8,6 +8,7 @@
 
 #include "common/debug.h"
 #include "common/platform.h"
+#include "common/angleutils.h"
 
 #include <stdarg.h>
 #include <vector>
@@ -25,22 +26,7 @@
 static void output(bool traceFileDebugOnly, PerfOutputFunction perfFunc, const char *format, va_list vararg)
 {
 #if defined(ANGLE_ENABLE_PERF) || defined(ANGLE_ENABLE_TRACE)
-    static std::vector<char> asciiMessageBuffer(512);
-
-    // Attempt to just print to the current buffer
-    int len = vsnprintf(&asciiMessageBuffer[0], asciiMessageBuffer.size(), format, vararg);
-    if (len < 0 || static_cast<size_t>(len) >= asciiMessageBuffer.size())
-    {
-        // Buffer was not large enough, calculate the required size and resize the buffer
-        len = vsnprintf(NULL, 0, format, vararg);
-        asciiMessageBuffer.resize(len + 1);
-
-        // Print again
-        vsnprintf(&asciiMessageBuffer[0], asciiMessageBuffer.size(), format, vararg);
-    }
-
-    // NULL terminate the buffer to be safe
-    asciiMessageBuffer[len] = '\0';
+    std::string formattedMessage = FormatString(format, vararg);
 #endif
 
 #if defined(ANGLE_ENABLE_PERF)
@@ -48,12 +34,12 @@
     {
         // The perf function only accepts wide strings, widen the ascii message
         static std::wstring wideMessage;
-        if (wideMessage.capacity() < asciiMessageBuffer.size())
+        if (wideMessage.capacity() < formattedMessage.length())
         {
-            wideMessage.reserve(asciiMessageBuffer.size());
+            wideMessage.reserve(formattedMessage.size());
         }
 
-        wideMessage.assign(asciiMessageBuffer.begin(), asciiMessageBuffer.begin() + len);
+        wideMessage.assign(formattedMessage.begin(), formattedMessage.end());
 
         perfFunc(0, wideMessage.c_str());
     }
@@ -70,7 +56,7 @@
     static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app);
     if (file)
     {
-        file.write(&asciiMessageBuffer[0], len);
+        file.write(formattedMessage.c_str(), formattedMessage.length());
         file.flush();
     }