Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Unified Diff: src/core/SkError.cpp

Issue 13699004: first draft of error checking / reporting API (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkError.cpp
diff --git a/src/core/SkError.cpp b/src/core/SkError.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..c5ca6d935bd2295e6001bf2d545c7216357303ff
--- /dev/null
+++ b/src/core/SkError.cpp
@@ -0,0 +1,131 @@
+
+/*
+ * Copyright 2011 Google Inc.
scroggo 2013/04/08 19:24:53 2013
humper 2013/04/08 19:43:23 Done.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkTLS.h"
+#include "SkTypes.h"
+#include "SkError.h"
+#include "SkErrorInternals.h"
+
+#include <stdio.h>
+#include <stdarg.h>
+
+namespace {
+ void *CreateThreadError() {
+ return SkNEW_ARGS(int, (kError_NoError));
+ }
+ void DeleteThreadError(void* v) {
+ delete reinterpret_cast<SkError*>(v);
scroggo 2013/04/08 19:24:53 SkDELETE. Also, shouldn't you reinterpret as an i
humper 2013/04/08 19:43:23 Done.
+ }
+ #define THREAD_ERROR \
+ (*reinterpret_cast<SkError*>(SkTLS::Get(CreateThreadError, DeleteThreadError)))
+
+ void *CreateThreadErrorCallback() {
+ return SkNEW_ARRAY(SkErrorCallbackFunction *, 1);
+ }
+ void DeleteThreadErrorCallback(void* v) {
+ delete reinterpret_cast<SkErrorCallbackFunction *>(v);
scroggo 2013/04/08 19:24:53 SkDELETE_ARRAY
humper 2013/04/08 19:43:23 Done.
+ }
+
+ #define THREAD_ERROR_CALLBACK \
+ *(reinterpret_cast<SkErrorCallbackFunction *>(SkTLS::Get(CreateThreadErrorCallback, DeleteThreadErrorCallback)))
scroggo 2013/04/08 19:24:53 100 chars
humper 2013/04/08 19:43:23 Done.
+
+ void *CreateThreadErrorContext() {
+ return SkNEW_ARGS(void **, (NULL));
+ }
+ void DeleteThreadErrorContext(void* v) {
+ delete reinterpret_cast<void **>(v);
scroggo 2013/04/08 19:24:53 SkDELETE
humper 2013/04/08 19:43:23 Done.
+ }
+ #define THREAD_ERROR_CONTEXT \
+ (*reinterpret_cast<void **>(SkTLS::Get(CreateThreadErrorContext, DeleteThreadErrorContext)))
+
+ #define ERROR_STRING_LENGTH 2048
+
+ void *CreateThreadErrorString() {
+ return SkNEW_ARRAY(char, (ERROR_STRING_LENGTH));
+ }
+ void DeleteThreadErrorString(void* v) {
+ delete reinterpret_cast<char *>(v);
scroggo 2013/04/08 19:24:53 SkDELETE_ARRAY
humper 2013/04/08 19:43:23 Done.
+ }
+ #define THREAD_ERROR_STRING \
+ (reinterpret_cast<char *>(SkTLS::Get(CreateThreadErrorString, DeleteThreadErrorString)))
+}
+
+SkError SkGetLastError() {
+ return SkErrorInternals::GetLastError();
+}
+void SkClearLastError() {
+ SkErrorInternals::ClearError();
+}
+void SkSetErrorCallback(SkErrorCallbackFunction cb, void *context) {
+ SkErrorInternals::SetErrorCallback(cb, context);
+}
+const char *SkGetLastErrorString() {
+ return SkErrorInternals::GetLastErrorString();
+}
+
+// ------------ Private Error functions ---------
+
+void SkErrorInternals::Init() {
+ SkDebugf("Initializing the error system\n");
scroggo 2013/04/08 19:24:53 It seems to me that this adds clutter to the debug
humper 2013/04/08 19:43:23 Done.
+ SkErrorInternals::ClearError();
+ SkErrorInternals::SetErrorCallback(NULL, NULL);
+}
+
+void SkErrorInternals::SetErrorCallback(SkErrorCallbackFunction cb, void *context) {
+ if (cb == NULL) {
+ THREAD_ERROR_CALLBACK = SkErrorInternals::DefaultErrorCallback;
+ } else {
+ THREAD_ERROR_CALLBACK = cb;
+ }
+ THREAD_ERROR_CONTEXT = context;
+}
+
+void SkErrorInternals::DefaultErrorCallback(SkError code, void *context) {
+ SkDebugf("Skia Error: %s\n", SkGetLastErrorString());
+}
+
+void SkErrorInternals::ClearError() {
+ SkErrorInternals::SetError( kError_NoError, "All is well" );
+}
+
+SkError SkErrorInternals::GetLastError() {
+ return THREAD_ERROR;
+}
+
+const char *SkErrorInternals::GetLastErrorString() {
+ return THREAD_ERROR_STRING;
+}
+
+void SkErrorInternals::SetError(SkError code, const char *fmt, ...) {
+ THREAD_ERROR = code;
+ va_list args;
+
+ char *str = THREAD_ERROR_STRING;
+ const char *error_name = NULL;
+ switch( code ) {
+ case kError_NoError: error_name = "No Error"; break;
+ case kError_InvalidArgument: error_name = "Invalid Argument"; break;
+ case kError_InvalidOperation: error_name = "Invalid Operation"; break;
+ case kError_InvalidHandle: error_name = "Invalid Handle"; break;
+ case kError_InvalidPaint: error_name = "Invalid Paint"; break;
+ case kError_OutOfMemory: error_name = "Out Of Memory"; break;
+ case kError_ParseError: error_name = "Parse Error"; break;
scroggo 2013/04/08 19:24:53 Generally we don't put an entire case on one line,
humper 2013/04/08 19:43:23 Done.
+ }
+
+ sprintf( str, "%s: ", error_name );
+ int string_left = ERROR_STRING_LENGTH - strlen( str );
+ str += strlen(str);
+
+ va_start( args, fmt );
+ vsnprintf( str, string_left, fmt, args );
+ va_end( args );
+ SkErrorCallbackFunction fn = THREAD_ERROR_CALLBACK;
+ if (fn && code != kError_NoError) {
+ fn(code, THREAD_ERROR_CONTEXT);
+ }
+}
« no previous file with comments | « include/core/SkError.h ('k') | src/core/SkErrorInternals.h » ('j') | src/core/SkErrorInternals.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698