Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 | |
| 2 /* | |
| 3 * Copyright 2011 Google Inc. | |
|
scroggo
2013/04/08 19:24:53
2013
humper
2013/04/08 19:43:23
Done.
| |
| 4 * | |
| 5 * Use of this source code is governed by a BSD-style license that can be | |
| 6 * found in the LICENSE file. | |
| 7 */ | |
| 8 | |
| 9 #include "SkTLS.h" | |
| 10 #include "SkTypes.h" | |
| 11 #include "SkError.h" | |
| 12 #include "SkErrorInternals.h" | |
| 13 | |
| 14 #include <stdio.h> | |
| 15 #include <stdarg.h> | |
| 16 | |
| 17 namespace { | |
| 18 void *CreateThreadError() { | |
| 19 return SkNEW_ARGS(int, (kError_NoError)); | |
| 20 } | |
| 21 void DeleteThreadError(void* v) { | |
| 22 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.
| |
| 23 } | |
| 24 #define THREAD_ERROR \ | |
| 25 (*reinterpret_cast<SkError*>(SkTLS::Get(CreateThreadError, DeleteThreadE rror))) | |
| 26 | |
| 27 void *CreateThreadErrorCallback() { | |
| 28 return SkNEW_ARRAY(SkErrorCallbackFunction *, 1); | |
| 29 } | |
| 30 void DeleteThreadErrorCallback(void* v) { | |
| 31 delete reinterpret_cast<SkErrorCallbackFunction *>(v); | |
|
scroggo
2013/04/08 19:24:53
SkDELETE_ARRAY
humper
2013/04/08 19:43:23
Done.
| |
| 32 } | |
| 33 | |
| 34 #define THREAD_ERROR_CALLBACK \ | |
| 35 *(reinterpret_cast<SkErrorCallbackFunction *>(SkTLS::Get(CreateThreadErr orCallback, DeleteThreadErrorCallback))) | |
|
scroggo
2013/04/08 19:24:53
100 chars
humper
2013/04/08 19:43:23
Done.
| |
| 36 | |
| 37 void *CreateThreadErrorContext() { | |
| 38 return SkNEW_ARGS(void **, (NULL)); | |
| 39 } | |
| 40 void DeleteThreadErrorContext(void* v) { | |
| 41 delete reinterpret_cast<void **>(v); | |
|
scroggo
2013/04/08 19:24:53
SkDELETE
humper
2013/04/08 19:43:23
Done.
| |
| 42 } | |
| 43 #define THREAD_ERROR_CONTEXT \ | |
| 44 (*reinterpret_cast<void **>(SkTLS::Get(CreateThreadErrorContext, DeleteT hreadErrorContext))) | |
| 45 | |
| 46 #define ERROR_STRING_LENGTH 2048 | |
| 47 | |
| 48 void *CreateThreadErrorString() { | |
| 49 return SkNEW_ARRAY(char, (ERROR_STRING_LENGTH)); | |
| 50 } | |
| 51 void DeleteThreadErrorString(void* v) { | |
| 52 delete reinterpret_cast<char *>(v); | |
|
scroggo
2013/04/08 19:24:53
SkDELETE_ARRAY
humper
2013/04/08 19:43:23
Done.
| |
| 53 } | |
| 54 #define THREAD_ERROR_STRING \ | |
| 55 (reinterpret_cast<char *>(SkTLS::Get(CreateThreadErrorString, DeleteThre adErrorString))) | |
| 56 } | |
| 57 | |
| 58 SkError SkGetLastError() { | |
| 59 return SkErrorInternals::GetLastError(); | |
| 60 } | |
| 61 void SkClearLastError() { | |
| 62 SkErrorInternals::ClearError(); | |
| 63 } | |
| 64 void SkSetErrorCallback(SkErrorCallbackFunction cb, void *context) { | |
| 65 SkErrorInternals::SetErrorCallback(cb, context); | |
| 66 } | |
| 67 const char *SkGetLastErrorString() { | |
| 68 return SkErrorInternals::GetLastErrorString(); | |
| 69 } | |
| 70 | |
| 71 // ------------ Private Error functions --------- | |
| 72 | |
| 73 void SkErrorInternals::Init() { | |
| 74 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.
| |
| 75 SkErrorInternals::ClearError(); | |
| 76 SkErrorInternals::SetErrorCallback(NULL, NULL); | |
| 77 } | |
| 78 | |
| 79 void SkErrorInternals::SetErrorCallback(SkErrorCallbackFunction cb, void *contex t) { | |
| 80 if (cb == NULL) { | |
| 81 THREAD_ERROR_CALLBACK = SkErrorInternals::DefaultErrorCallback; | |
| 82 } else { | |
| 83 THREAD_ERROR_CALLBACK = cb; | |
| 84 } | |
| 85 THREAD_ERROR_CONTEXT = context; | |
| 86 } | |
| 87 | |
| 88 void SkErrorInternals::DefaultErrorCallback(SkError code, void *context) { | |
| 89 SkDebugf("Skia Error: %s\n", SkGetLastErrorString()); | |
| 90 } | |
| 91 | |
| 92 void SkErrorInternals::ClearError() { | |
| 93 SkErrorInternals::SetError( kError_NoError, "All is well" ); | |
| 94 } | |
| 95 | |
| 96 SkError SkErrorInternals::GetLastError() { | |
| 97 return THREAD_ERROR; | |
| 98 } | |
| 99 | |
| 100 const char *SkErrorInternals::GetLastErrorString() { | |
| 101 return THREAD_ERROR_STRING; | |
| 102 } | |
| 103 | |
| 104 void SkErrorInternals::SetError(SkError code, const char *fmt, ...) { | |
| 105 THREAD_ERROR = code; | |
| 106 va_list args; | |
| 107 | |
| 108 char *str = THREAD_ERROR_STRING; | |
| 109 const char *error_name = NULL; | |
| 110 switch( code ) { | |
| 111 case kError_NoError: error_name = "No Error"; break; | |
| 112 case kError_InvalidArgument: error_name = "Invalid Argument"; break; | |
| 113 case kError_InvalidOperation: error_name = "Invalid Operation"; break; | |
| 114 case kError_InvalidHandle: error_name = "Invalid Handle"; break; | |
| 115 case kError_InvalidPaint: error_name = "Invalid Paint"; break; | |
| 116 case kError_OutOfMemory: error_name = "Out Of Memory"; break; | |
| 117 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.
| |
| 118 } | |
| 119 | |
| 120 sprintf( str, "%s: ", error_name ); | |
| 121 int string_left = ERROR_STRING_LENGTH - strlen( str ); | |
| 122 str += strlen(str); | |
| 123 | |
| 124 va_start( args, fmt ); | |
| 125 vsnprintf( str, string_left, fmt, args ); | |
| 126 va_end( args ); | |
| 127 SkErrorCallbackFunction fn = THREAD_ERROR_CALLBACK; | |
| 128 if (fn && code != kError_NoError) { | |
| 129 fn(code, THREAD_ERROR_CONTEXT); | |
| 130 } | |
| 131 } | |
| OLD | NEW |