gfx/skia/trunk/src/core/SkError.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkError.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,143 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2013 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +#include "SkTLS.h"
    1.13 +#include "SkTypes.h"
    1.14 +#include "SkError.h"
    1.15 +#include "SkErrorInternals.h"
    1.16 +
    1.17 +#include <stdio.h>
    1.18 +#include <stdarg.h>
    1.19 +
    1.20 +namespace {
    1.21 +    void *CreateThreadError() {
    1.22 +        return SkNEW_ARGS(SkError, (kNoError_SkError));
    1.23 +    }
    1.24 +    void DeleteThreadError(void* v) {
    1.25 +        SkDELETE(reinterpret_cast<SkError*>(v));
    1.26 +    }
    1.27 +    #define THREAD_ERROR \
    1.28 +        (*reinterpret_cast<SkError*>(SkTLS::Get(CreateThreadError, DeleteThreadError)))
    1.29 +
    1.30 +    void *CreateThreadErrorCallback() {
    1.31 +        return SkNEW_ARGS(SkErrorCallbackFunction, (SkErrorInternals::DefaultErrorCallback));
    1.32 +    }
    1.33 +    void DeleteThreadErrorCallback(void* v) {
    1.34 +        SkDELETE(reinterpret_cast<SkErrorCallbackFunction *>(v));
    1.35 +    }
    1.36 +
    1.37 +    #define THREAD_ERROR_CALLBACK                                                             \
    1.38 +        *(reinterpret_cast<SkErrorCallbackFunction *>(SkTLS::Get(CreateThreadErrorCallback,   \
    1.39 +                                                                 DeleteThreadErrorCallback)))
    1.40 +
    1.41 +    void *CreateThreadErrorContext() {
    1.42 +        return SkNEW_ARGS(void **, (NULL));
    1.43 +    }
    1.44 +    void DeleteThreadErrorContext(void* v) {
    1.45 +        SkDELETE(reinterpret_cast<void **>(v));
    1.46 +    }
    1.47 +    #define THREAD_ERROR_CONTEXT \
    1.48 +        (*reinterpret_cast<void **>(SkTLS::Get(CreateThreadErrorContext, DeleteThreadErrorContext)))
    1.49 +
    1.50 +    #define ERROR_STRING_LENGTH 2048
    1.51 +
    1.52 +    void *CreateThreadErrorString() {
    1.53 +        return SkNEW_ARRAY(char, (ERROR_STRING_LENGTH));
    1.54 +    }
    1.55 +    void DeleteThreadErrorString(void* v) {
    1.56 +        SkDELETE_ARRAY(reinterpret_cast<char *>(v));
    1.57 +    }
    1.58 +    #define THREAD_ERROR_STRING \
    1.59 +        (reinterpret_cast<char *>(SkTLS::Get(CreateThreadErrorString, DeleteThreadErrorString)))
    1.60 +}
    1.61 +
    1.62 +SkError SkGetLastError() {
    1.63 +    return SkErrorInternals::GetLastError();
    1.64 +}
    1.65 +void SkClearLastError() {
    1.66 +    SkErrorInternals::ClearError();
    1.67 +}
    1.68 +void SkSetErrorCallback(SkErrorCallbackFunction cb, void *context) {
    1.69 +    SkErrorInternals::SetErrorCallback(cb, context);
    1.70 +}
    1.71 +const char *SkGetLastErrorString() {
    1.72 +    return SkErrorInternals::GetLastErrorString();
    1.73 +}
    1.74 +
    1.75 +// ------------ Private Error functions ---------
    1.76 +
    1.77 +void SkErrorInternals::SetErrorCallback(SkErrorCallbackFunction cb, void *context) {
    1.78 +    if (cb == NULL) {
    1.79 +        THREAD_ERROR_CALLBACK = SkErrorInternals::DefaultErrorCallback;
    1.80 +    } else {
    1.81 +        THREAD_ERROR_CALLBACK = cb;
    1.82 +    }
    1.83 +    THREAD_ERROR_CONTEXT = context;
    1.84 +}
    1.85 +
    1.86 +void SkErrorInternals::DefaultErrorCallback(SkError code, void *context) {
    1.87 +    SkDebugf("Skia Error: %s\n", SkGetLastErrorString());
    1.88 +}
    1.89 +
    1.90 +void SkErrorInternals::ClearError() {
    1.91 +    SkErrorInternals::SetError( kNoError_SkError, "All is well" );
    1.92 +}
    1.93 +
    1.94 +SkError SkErrorInternals::GetLastError() {
    1.95 +    return THREAD_ERROR;
    1.96 +}
    1.97 +
    1.98 +const char *SkErrorInternals::GetLastErrorString() {
    1.99 +    return THREAD_ERROR_STRING;
   1.100 +}
   1.101 +
   1.102 +void SkErrorInternals::SetError(SkError code, const char *fmt, ...) {
   1.103 +    THREAD_ERROR = code;
   1.104 +    va_list args;
   1.105 +
   1.106 +    char *str = THREAD_ERROR_STRING;
   1.107 +    const char *error_name = NULL;
   1.108 +    switch( code ) {
   1.109 +        case kNoError_SkError:
   1.110 +            error_name = "No Error";
   1.111 +            break;
   1.112 +        case kInvalidArgument_SkError:
   1.113 +            error_name = "Invalid Argument";
   1.114 +            break;
   1.115 +        case kInvalidOperation_SkError:
   1.116 +            error_name = "Invalid Operation";
   1.117 +            break;
   1.118 +        case kInvalidHandle_SkError:
   1.119 +            error_name = "Invalid Handle";
   1.120 +            break;
   1.121 +        case kInvalidPaint_SkError:
   1.122 +            error_name = "Invalid Paint";
   1.123 +            break;
   1.124 +        case kOutOfMemory_SkError:
   1.125 +            error_name = "Out Of Memory";
   1.126 +            break;
   1.127 +        case kParseError_SkError:
   1.128 +            error_name = "Parse Error";
   1.129 +            break;
   1.130 +        default:
   1.131 +            error_name = "Unknown error";
   1.132 +            break;
   1.133 +    }
   1.134 +
   1.135 +    sprintf( str, "%s: ", error_name );
   1.136 +    int string_left = SkToInt(ERROR_STRING_LENGTH - strlen(str));
   1.137 +    str += strlen(str);
   1.138 +
   1.139 +    va_start( args, fmt );
   1.140 +    vsnprintf( str, string_left, fmt, args );
   1.141 +    va_end( args );
   1.142 +    SkErrorCallbackFunction fn = THREAD_ERROR_CALLBACK;
   1.143 +    if (fn && code != kNoError_SkError) {
   1.144 +        fn(code, THREAD_ERROR_CONTEXT);
   1.145 +    }
   1.146 +}

mercurial