aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkError.cpp
blob: 9e4385b4894dd2cc2448df2fb40bef6de149e909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

/*
 * Copyright 2013 Google Inc.
 *
 * 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 new SkError(kNoError_SkError); }
void DeleteThreadError(void *v) { delete reinterpret_cast<SkError *>(v); }
    #define THREAD_ERROR \
        (*reinterpret_cast<SkError*>(SkTLS::Get(CreateThreadError, DeleteThreadError)))

    void *CreateThreadErrorCallback() {
        return new SkErrorCallbackFunction(SkErrorInternals::DefaultErrorCallback);
    }
    void DeleteThreadErrorCallback(void* v) {
        delete reinterpret_cast<SkErrorCallbackFunction *>(v);
    }

    #define THREAD_ERROR_CALLBACK                                                             \
        *(reinterpret_cast<SkErrorCallbackFunction *>(SkTLS::Get(CreateThreadErrorCallback,   \
                                                                 DeleteThreadErrorCallback)))

    void *CreateThreadErrorContext() { return new void **(nullptr); }
    void DeleteThreadErrorContext(void *v) { delete reinterpret_cast<void **>(v); }
    #define THREAD_ERROR_CONTEXT \
        (*reinterpret_cast<void **>(SkTLS::Get(CreateThreadErrorContext, DeleteThreadErrorContext)))

    #define ERROR_STRING_LENGTH 2048

    void *CreateThreadErrorString() { return new char[(ERROR_STRING_LENGTH)]; }
    void DeleteThreadErrorString(void *v) { delete[] reinterpret_cast<char *>(v); }
    #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::SetErrorCallback(SkErrorCallbackFunction cb, void *context) {
    if (cb == nullptr) {
        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( kNoError_SkError, "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 = nullptr;
    switch( code ) {
        case kNoError_SkError:
            error_name = "No Error";
            break;
        case kInvalidArgument_SkError:
            error_name = "Invalid Argument";
            break;
        case kInvalidOperation_SkError:
            error_name = "Invalid Operation";
            break;
        case kInvalidHandle_SkError:
            error_name = "Invalid Handle";
            break;
        case kInvalidPaint_SkError:
            error_name = "Invalid Paint";
            break;
        case kOutOfMemory_SkError:
            error_name = "Out Of Memory";
            break;
        case kParseError_SkError:
            error_name = "Parse Error";
            break;
        default:
            error_name = "Unknown error";
            break;
    }

    sprintf( str, "%s: ", error_name );
    int string_left = SkToInt(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 != kNoError_SkError) {
        fn(code, THREAD_ERROR_CONTEXT);
    }
}