src/SDL_error.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 16 Oct 2018 08:29:27 -0700
changeset 12332 c80f702e4b8b
parent 11875 91138f56d762
child 12503 806492103856
permissions -rw-r--r--
Support vibration magnitude on Android 8.0 (thanks Rachel!)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "./SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* Simple error handling in SDL */
slouken@0
    24
slouken@5221
    25
#include "SDL_log.h"
slouken@0
    26
#include "SDL_error.h"
slouken@0
    27
#include "SDL_error_c.h"
slouken@0
    28
slouken@4618
    29
slouken@1361
    30
/* Routine to get the thread-specific error variable */
slouken@1361
    31
#if SDL_THREADS_DISABLED
slouken@4627
    32
/* The default (non-thread-safe) global error variable */
slouken@0
    33
static SDL_error SDL_global_error;
slouken@7191
    34
#define SDL_GetErrBuf() (&SDL_global_error)
slouken@1361
    35
#else
slouken@1361
    36
extern SDL_error *SDL_GetErrBuf(void);
slouken@1361
    37
#endif /* SDL_THREADS_DISABLED */
slouken@0
    38
slouken@7191
    39
#define SDL_ERRBUFIZE   1024
slouken@0
    40
slouken@0
    41
/* Private functions */
slouken@0
    42
slouken@1895
    43
static const char *
slouken@1895
    44
SDL_LookupString(const char *key)
slouken@0
    45
{
slouken@1895
    46
    /* FIXME: Add code to lookup key in language string hash-table */
slouken@1895
    47
    return key;
slouken@0
    48
}
slouken@0
    49
slouken@0
    50
/* Public functions */
slouken@0
    51
slouken@11137
    52
static char *SDL_GetErrorMsg(char *errstr, int maxlen);
slouken@11137
    53
icculus@7037
    54
int
slouken@8820
    55
SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
slouken@0
    56
{
slouken@1895
    57
    va_list ap;
slouken@1895
    58
    SDL_error *error;
slouken@0
    59
aschiffler@5766
    60
    /* Ignore call if invalid format pointer was passed */
icculus@7037
    61
    if (fmt == NULL) return -1;
slouken@7191
    62
slouken@1895
    63
    /* Copy in the key, mark error as valid */
slouken@1895
    64
    error = SDL_GetErrBuf();
slouken@1895
    65
    error->error = 1;
slouken@1895
    66
    SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
slouken@0
    67
slouken@1895
    68
    va_start(ap, fmt);
slouken@1895
    69
    error->argc = 0;
slouken@1895
    70
    while (*fmt) {
slouken@1895
    71
        if (*fmt++ == '%') {
slouken@1895
    72
            while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
slouken@1895
    73
                ++fmt;
slouken@1895
    74
            }
slouken@1895
    75
            switch (*fmt++) {
slouken@1895
    76
            case 0:            /* Malformed format string.. */
slouken@1895
    77
                --fmt;
slouken@1895
    78
                break;
sezero@11875
    79
            case 'l':
sezero@11875
    80
                switch (*fmt++) {
sezero@11875
    81
                case 0:        /* Malformed format string.. */
sezero@11875
    82
                    --fmt;
sezero@11875
    83
                    break;
sezero@11875
    84
                case 'i': case 'd': case 'u':
sezero@11875
    85
                    error->args[error->argc++].value_l = va_arg(ap, long);
sezero@11875
    86
                    break;
sezero@11875
    87
                }
sezero@11875
    88
                break;
slouken@1895
    89
            case 'c':
slouken@1895
    90
            case 'i':
slouken@1895
    91
            case 'd':
slouken@1895
    92
            case 'u':
slouken@1895
    93
            case 'o':
slouken@1895
    94
            case 'x':
slouken@1895
    95
            case 'X':
slouken@1895
    96
                error->args[error->argc++].value_i = va_arg(ap, int);
slouken@1895
    97
                break;
slouken@1895
    98
            case 'f':
slouken@1895
    99
                error->args[error->argc++].value_f = va_arg(ap, double);
slouken@1895
   100
                break;
slouken@1895
   101
            case 'p':
slouken@1895
   102
                error->args[error->argc++].value_ptr = va_arg(ap, void *);
slouken@1895
   103
                break;
slouken@1895
   104
            case 's':
slouken@1895
   105
                {
slouken@1895
   106
                    int i = error->argc;
slouken@1895
   107
                    const char *str = va_arg(ap, const char *);
slouken@1895
   108
                    if (str == NULL)
slouken@1895
   109
                        str = "(null)";
slouken@1895
   110
                    SDL_strlcpy((char *) error->args[i].buf, str,
slouken@1895
   111
                                ERR_MAX_STRLEN);
slouken@1895
   112
                    error->argc++;
slouken@1895
   113
                }
slouken@1895
   114
                break;
slouken@1895
   115
            default:
slouken@1895
   116
                break;
slouken@1895
   117
            }
slouken@1895
   118
            if (error->argc >= ERR_MAX_ARGS) {
slouken@1895
   119
                break;
slouken@1895
   120
            }
slouken@1895
   121
        }
slouken@1895
   122
    }
slouken@1895
   123
    va_end(ap);
slouken@0
   124
slouken@11137
   125
    if (SDL_LogGetPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) {
slouken@11137
   126
        /* If we are in debug mode, print out an error message
slouken@11137
   127
         * Avoid stomping on the static buffer in GetError, just
slouken@11137
   128
         * in case this is called while processing a ShowMessageBox to
slouken@11137
   129
         * show an error already in that static buffer.
slouken@11137
   130
         */
slouken@11137
   131
        char errmsg[SDL_ERRBUFIZE];
slouken@11137
   132
        SDL_GetErrorMsg(errmsg, sizeof(errmsg));
slouken@11137
   133
        SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg);
slouken@11137
   134
    }
icculus@7037
   135
    return -1;
slouken@0
   136
}
slouken@0
   137
icculus@10643
   138
/* Available for backwards compatibility */
icculus@10643
   139
const char *
icculus@10643
   140
SDL_GetError(void)
icculus@10643
   141
{
icculus@10643
   142
    static char errmsg[SDL_ERRBUFIZE];
icculus@10643
   143
icculus@10643
   144
    return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
icculus@10643
   145
}
icculus@10643
   146
icculus@10643
   147
void
icculus@10643
   148
SDL_ClearError(void)
icculus@10643
   149
{
icculus@10643
   150
    SDL_error *error;
icculus@10643
   151
icculus@10643
   152
    error = SDL_GetErrBuf();
icculus@10643
   153
    error->error = 0;
icculus@10643
   154
}
icculus@10643
   155
icculus@10643
   156
/* Very common errors go here */
icculus@10643
   157
int
icculus@10643
   158
SDL_Error(SDL_errorcode code)
icculus@10643
   159
{
icculus@10643
   160
    switch (code) {
icculus@10643
   161
    case SDL_ENOMEM:
icculus@10643
   162
        return SDL_SetError("Out of memory");
icculus@10643
   163
    case SDL_EFREAD:
icculus@10643
   164
        return SDL_SetError("Error reading from datastream");
icculus@10643
   165
    case SDL_EFWRITE:
icculus@10643
   166
        return SDL_SetError("Error writing to datastream");
icculus@10643
   167
    case SDL_EFSEEK:
icculus@10643
   168
        return SDL_SetError("Error seeking in datastream");
icculus@10643
   169
    case SDL_UNSUPPORTED:
icculus@10643
   170
        return SDL_SetError("That operation is not supported");
icculus@10643
   171
    default:
icculus@10643
   172
        return SDL_SetError("Unknown SDL error");
icculus@10643
   173
    }
icculus@10643
   174
}
icculus@10643
   175
icculus@10643
   176
#ifdef TEST_ERROR
icculus@10643
   177
int
icculus@10643
   178
main(int argc, char *argv[])
icculus@10643
   179
{
icculus@10643
   180
    char buffer[BUFSIZ + 1];
icculus@10643
   181
icculus@10643
   182
    SDL_SetError("Hi there!");
icculus@10643
   183
    printf("Error 1: %s\n", SDL_GetError());
icculus@10643
   184
    SDL_ClearError();
icculus@10643
   185
    SDL_memset(buffer, '1', BUFSIZ);
icculus@10643
   186
    buffer[BUFSIZ] = 0;
icculus@10643
   187
    SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
icculus@10643
   188
    printf("Error 2: %s\n", SDL_GetError());
icculus@10643
   189
    exit(0);
icculus@10643
   190
}
icculus@10643
   191
#endif
icculus@10643
   192
icculus@10643
   193
icculus@10643
   194
/* keep this at the end of the file so it works with GCC builds that don't
icculus@10643
   195
   support "#pragma GCC diagnostic push" ... we'll just leave the warning
icculus@10643
   196
   disabled after this. */
icculus@10644
   197
/* this pragma arrived in GCC 4.2 and causes a warning on older GCCs! Sigh. */
icculus@10644
   198
#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 2))))
hadess@10396
   199
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
slouken@10403
   200
#endif
icculus@10644
   201
slouken@0
   202
/* This function has a bit more overhead than most error functions
slouken@0
   203
   so that it supports internationalization and thread-safe errors.
slouken@0
   204
*/
slouken@4472
   205
static char *
icculus@9430
   206
SDL_GetErrorMsg(char *errstr, int maxlen)
slouken@0
   207
{
slouken@1895
   208
    SDL_error *error;
slouken@0
   209
slouken@1895
   210
    /* Clear the error string */
slouken@1895
   211
    *errstr = '\0';
slouken@1895
   212
    --maxlen;
slouken@0
   213
slouken@1895
   214
    /* Get the thread-safe error, and print it out */
slouken@1895
   215
    error = SDL_GetErrBuf();
slouken@1895
   216
    if (error->error) {
slouken@1895
   217
        const char *fmt;
slouken@1895
   218
        char *msg = errstr;
slouken@1895
   219
        int len;
slouken@1895
   220
        int argi;
slouken@0
   221
slouken@1895
   222
        fmt = SDL_LookupString(error->key);
slouken@1895
   223
        argi = 0;
slouken@1895
   224
        while (*fmt && (maxlen > 0)) {
slouken@1895
   225
            if (*fmt == '%') {
slouken@1895
   226
                char tmp[32], *spot = tmp;
slouken@1895
   227
                *spot++ = *fmt++;
slouken@1895
   228
                while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
slouken@1895
   229
                       && spot < (tmp + SDL_arraysize(tmp) - 2)) {
slouken@1895
   230
                    *spot++ = *fmt++;
slouken@1895
   231
                }
sezero@11875
   232
                if (*fmt == 'l') {
sezero@11875
   233
                    *spot++ = *fmt++;
sezero@11875
   234
                    *spot++ = *fmt++;
sezero@11875
   235
                    *spot++ = '\0';
sezero@11875
   236
                    switch (spot[-2]) {
sezero@11875
   237
                    case 'i': case 'd': case 'u':
sezero@11875
   238
                      len = SDL_snprintf(msg, maxlen, tmp,
sezero@11875
   239
                                         error->args[argi++].value_l);
sezero@11875
   240
                      if (len > 0) {
sezero@11875
   241
                          msg += len;
sezero@11875
   242
                          maxlen -= len;
sezero@11875
   243
                      }
sezero@11875
   244
                      break;
sezero@11875
   245
                    }
sezero@11875
   246
                    continue;
sezero@11875
   247
                }
slouken@1895
   248
                *spot++ = *fmt++;
slouken@1895
   249
                *spot++ = '\0';
slouken@1895
   250
                switch (spot[-2]) {
slouken@1895
   251
                case '%':
slouken@1895
   252
                    *msg++ = '%';
slouken@1895
   253
                    maxlen -= 1;
slouken@1895
   254
                    break;
slouken@1895
   255
                case 'c':
slouken@1895
   256
                case 'i':
slouken@1895
   257
                case 'd':
slouken@1895
   258
                case 'u':
slouken@1895
   259
                case 'o':
slouken@1895
   260
                case 'x':
slouken@1895
   261
                case 'X':
slouken@1895
   262
                    len =
slouken@1895
   263
                        SDL_snprintf(msg, maxlen, tmp,
slouken@1895
   264
                                     error->args[argi++].value_i);
icculus@9430
   265
                    if (len > 0) {
icculus@9430
   266
                        msg += len;
icculus@9430
   267
                        maxlen -= len;
icculus@9430
   268
                    }
slouken@1895
   269
                    break;
icculus@9430
   270
slouken@1895
   271
                case 'f':
slouken@1895
   272
                    len =
slouken@1895
   273
                        SDL_snprintf(msg, maxlen, tmp,
slouken@1895
   274
                                     error->args[argi++].value_f);
icculus@9430
   275
                    if (len > 0) {
icculus@9430
   276
                        msg += len;
icculus@9430
   277
                        maxlen -= len;
icculus@9430
   278
                    }
slouken@1895
   279
                    break;
icculus@9430
   280
slouken@1895
   281
                case 'p':
slouken@1895
   282
                    len =
slouken@1895
   283
                        SDL_snprintf(msg, maxlen, tmp,
slouken@1895
   284
                                     error->args[argi++].value_ptr);
icculus@9430
   285
                    if (len > 0) {
icculus@9430
   286
                        msg += len;
icculus@9430
   287
                        maxlen -= len;
icculus@9430
   288
                    }
slouken@1895
   289
                    break;
icculus@9430
   290
slouken@1895
   291
                case 's':
slouken@1895
   292
                    len =
slouken@1895
   293
                        SDL_snprintf(msg, maxlen, tmp,
slouken@3013
   294
                                     SDL_LookupString(error->args[argi++].
slouken@3013
   295
                                                      buf));
icculus@9430
   296
                    if (len > 0) {
icculus@9430
   297
                        msg += len;
icculus@9430
   298
                        maxlen -= len;
icculus@9430
   299
                    }
slouken@1895
   300
                    break;
icculus@9430
   301
slouken@1895
   302
                }
slouken@1895
   303
            } else {
slouken@1895
   304
                *msg++ = *fmt++;
slouken@1895
   305
                maxlen -= 1;
slouken@1895
   306
            }
slouken@1895
   307
        }
icculus@9430
   308
icculus@9430
   309
        /* slide back if we've overshot the end of our buffer. */
icculus@9430
   310
        if (maxlen < 0) {
icculus@9430
   311
            msg -= (-maxlen) + 1;
icculus@9430
   312
        }
icculus@9430
   313
slouken@1895
   314
        *msg = 0;               /* NULL terminate the string */
slouken@1895
   315
    }
slouken@1895
   316
    return (errstr);
slouken@0
   317
}
slouken@4992
   318
slouken@1895
   319
/* vi: set ts=4 sw=4 expandtab: */