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

Side by Side 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: Several cleanups from Leon 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1
2 /*
3 * Copyright 2013 Google Inc.
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(SkError, (kError_NoError));
20 }
21 void DeleteThreadError(void* v) {
22 SkDELETE(reinterpret_cast<SkError*>(v));
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 SkDELETE_ARRAY(reinterpret_cast<SkErrorCallbackFunction *>(v));
32 }
33
34 #define THREAD_ERROR_CALLBACK \
35 *(reinterpret_cast<SkErrorCallbackFunction *>(SkTLS::Get(CreateThreadErr orCallback, \
36 DeleteThreadErr orCallback)))
37
38 void *CreateThreadErrorContext() {
39 return SkNEW_ARGS(void **, (NULL));
40 }
41 void DeleteThreadErrorContext(void* v) {
42 SkDELETE(reinterpret_cast<void **>(v));
43 }
44 #define THREAD_ERROR_CONTEXT \
45 (*reinterpret_cast<void **>(SkTLS::Get(CreateThreadErrorContext, DeleteT hreadErrorContext)))
46
47 #define ERROR_STRING_LENGTH 2048
48
49 void *CreateThreadErrorString() {
50 return SkNEW_ARRAY(char, (ERROR_STRING_LENGTH));
51 }
52 void DeleteThreadErrorString(void* v) {
53 SkDELETE_ARRAY(reinterpret_cast<char *>(v));
54 }
55 #define THREAD_ERROR_STRING \
56 (reinterpret_cast<char *>(SkTLS::Get(CreateThreadErrorString, DeleteThre adErrorString)))
57 }
58
59 SkError SkGetLastError() {
60 return SkErrorInternals::GetLastError();
61 }
62 void SkClearLastError() {
63 SkErrorInternals::ClearError();
64 }
65 void SkSetErrorCallback(SkErrorCallbackFunction cb, void *context) {
66 SkErrorInternals::SetErrorCallback(cb, context);
67 }
68 const char *SkGetLastErrorString() {
69 return SkErrorInternals::GetLastErrorString();
70 }
71
72 // ------------ Private Error functions ---------
73
74 void SkErrorInternals::Init() {
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:
112 error_name = "No Error";
113 break;
114 case kError_InvalidArgument:
115 error_name = "Invalid Argument";
116 break;
117 case kError_InvalidOperation:
118 error_name = "Invalid Operation";
119 break;
120 case kError_InvalidHandle:
121 error_name = "Invalid Handle";
122 break;
123 case kError_InvalidPaint:
124 error_name = "Invalid Paint";
125 break;
126 case kError_OutOfMemory:
127 error_name = "Out Of Memory";
128 break;
129 case kError_ParseError:
130 error_name = "Parse Error";
131 break;
132 default:
133 error_name = "Unknown error";
134 break;
135 }
136
137 sprintf( str, "%s: ", error_name );
138 int string_left = ERROR_STRING_LENGTH - strlen( str );
139 str += strlen(str);
140
141 va_start( args, fmt );
142 vsnprintf( str, string_left, fmt, args );
143 va_end( args );
144 SkErrorCallbackFunction fn = THREAD_ERROR_CALLBACK;
145 if (fn && code != kError_NoError) {
146 fn(code, THREAD_ERROR_CONTEXT);
147 }
148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698