src/SDL_assert.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Oct 2013 23:02:29 -0700
changeset 7828 1451063c8ecd
parent 7588 f1ca6adffc4b
child 8093 b43765095a6f
child 8535 e8ee0708ef5c
permissions -rw-r--r--
Fixed building using MinGW Our SDL_windows.h needed to be included before anything else so UNICODE is defined.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6759
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
6217
6952b11b7f46 Make sure that we use consistent configuration options on platforms like Windows so that command line builds and IDE builds have ABI compatibility.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    21
#include "SDL_config.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
7828
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    23
#if defined(__WIN32__)
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    24
#include "core/windows/SDL_windows.h"
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    25
#endif
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    26
3651
cb5b1aedb5a7 Fixed include ordering
Sam Lantinga <slouken@libsdl.org>
parents: 3648
diff changeset
    27
#include "SDL.h"
5006
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 4472
diff changeset
    28
#include "SDL_atomic.h"
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
    29
#include "SDL_messagebox.h"
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
    30
#include "SDL_video.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#include "SDL_assert.h"
4472
791b3256fb22 Mostly cleaned up warnings with -Wmissing-prototypes
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    32
#include "SDL_assert_c.h"
3671
0d6f520c0eb9 Fixed building under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 3670
diff changeset
    33
#include "video/SDL_sysvideo.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    35
#ifdef __WIN32__
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    36
#ifndef WS_OVERLAPPEDWINDOW
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    37
#define WS_OVERLAPPEDWINDOW 0
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    38
#endif
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
#else  /* fprintf, _exit(), etc. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
#include <stdio.h>
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
#include <stdlib.h>
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    42
#include <unistd.h>
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    45
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    46
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    47
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    48
/*
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    49
 * We keep all triggered assertions in a singly-linked list so we can
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
 *  generate a report later.
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
 */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    52
static SDL_assert_data *triggered_assertions = NULL;
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    53
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    54
static SDL_mutex *assertion_mutex = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    55
static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    56
static void *assertion_userdata = NULL;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    58
#ifdef __GNUC__
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    59
static void
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    60
debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    61
#endif
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    62
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    63
static void
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    64
debug_print(const char *fmt, ...)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
    va_start(ap, fmt);
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
    68
    SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    va_end(ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
static void SDL_AddAssertionToReport(SDL_assert_data *data)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
    /* (data) is always a static struct defined with the assert macros, so
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
       we don't have to worry about copying or allocating them. */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    77
    data->trigger_count++;
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    78
    if (data->trigger_count == 1) {  /* not yet added? */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
        data->next = triggered_assertions;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
        triggered_assertions = data;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    84
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
static void SDL_GenerateAssertionReport(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
{
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    87
    const SDL_assert_data *item = triggered_assertions;
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    88
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    89
    /* only do this if the app hasn't assigned an assertion handler. */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    90
    if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
        debug_print("\n\nSDL assertion report.\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
        debug_print("All SDL assertions between last init/quit:\n\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    94
        while (item != NULL) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
            debug_print(
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
                "'%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
                "    * %s (%s:%d)\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
                "    * triggered %u time%s.\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
                "    * always ignore: %s.\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
                item->condition, item->function, item->filename,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
                item->linenum, item->trigger_count,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
                (item->trigger_count == 1) ? "" : "s",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
                item->always_ignore ? "yes" : "no");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
            item = item->next;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
        debug_print("\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   108
        SDL_ResetAssertionReport();
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   112
static void SDL_ExitProcess(int exitcode)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   114
#ifdef __WIN32__
6305
601b0e251822 SDL_ExitProcess() was ignoring exit code parameter.
Ryan C. Gordon <icculus@icculus.org>
parents: 6217
diff changeset
   115
    ExitProcess(exitcode);
3656
f17ea6f49745 Assume _exit() is available. It may be, even if unix isn't defined.
Sam Lantinga <slouken@libsdl.org>
parents: 3655
diff changeset
   116
#else
6305
601b0e251822 SDL_ExitProcess() was ignoring exit code parameter.
Ryan C. Gordon <icculus@icculus.org>
parents: 6217
diff changeset
   117
    _exit(exitcode);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
}
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   120
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   121
static void SDL_AbortAssertion(void)
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   122
{
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   123
    SDL_Quit();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   124
    SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   125
}
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   126
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   128
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   129
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
{
6759
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   131
#ifdef __WIN32__
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   132
    #define ENDLINE "\r\n"
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   133
#else
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   134
    #define ENDLINE "\n"
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   135
#endif
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   136
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
    const char *envr;
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   138
    SDL_assert_state state = SDL_ASSERTION_ABORT;
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3671
diff changeset
   139
    SDL_Window *window;
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   140
    SDL_MessageBoxData messagebox;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   141
    SDL_MessageBoxButtonData buttons[] = {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   142
        {   0,  SDL_ASSERTION_RETRY,            "Retry" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   143
        {   0,  SDL_ASSERTION_BREAK,            "Break" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   144
        {   0,  SDL_ASSERTION_ABORT,            "Abort" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   145
        {   SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   146
                SDL_ASSERTION_IGNORE,           "Ignore" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   147
        {   SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   148
                SDL_ASSERTION_ALWAYS_IGNORE,    "Always Ignore" }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   149
    };
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   150
    char *message;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   151
    int selected;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   153
    (void) userdata;  /* unused in default handler. */
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   154
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   155
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   156
    if (!message) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   157
        /* Uh oh, we're in real trouble now... */
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   158
        return SDL_ASSERTION_ABORT;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   159
    }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   160
    SDL_snprintf(message, SDL_MAX_LOG_MESSAGE,
6759
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   161
                 "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   162
                    "  '%s'",
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   163
                 data->function, data->filename, data->linenum,
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   164
                 data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   165
                 data->condition);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   166
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   167
    debug_print("\n\n%s\n\n", message);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   169
    /* let env. variable override, so unit tests won't block in a GUI. */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
    envr = SDL_getenv("SDL_ASSERT");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
    if (envr != NULL) {
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   172
        SDL_stack_free(message);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   173
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
        if (SDL_strcmp(envr, "abort") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
            return SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
        } else if (SDL_strcmp(envr, "break") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
            return SDL_ASSERTION_BREAK;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
        } else if (SDL_strcmp(envr, "retry") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
            return SDL_ASSERTION_RETRY;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
        } else if (SDL_strcmp(envr, "ignore") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
            return SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
        } else if (SDL_strcmp(envr, "always_ignore") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
            return SDL_ASSERTION_ALWAYS_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
        } else {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
            return SDL_ASSERTION_ABORT;  /* oh well. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   189
    /* Leave fullscreen mode, if possible (scary!) */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   190
    window = SDL_GetFocusWindow();
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   191
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   192
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   193
            SDL_MinimizeWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   194
        } else {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   195
            /* !!! FIXME: ungrab the input if we're not fullscreen? */
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   196
            /* No need to mess with the window */
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   197
            window = NULL;
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   198
        }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   199
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   200
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   201
    /* Show a messagebox if we can, otherwise fall back to stdio */
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   202
    SDL_zero(messagebox);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   203
    messagebox.flags = SDL_MESSAGEBOX_WARNING;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   204
    messagebox.window = window;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   205
    messagebox.title = "Assertion Failed";
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   206
    messagebox.message = message;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   207
    messagebox.numbuttons = SDL_arraysize(buttons);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   208
    messagebox.buttons = buttons;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   210
    if (SDL_ShowMessageBox(&messagebox, &selected) == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   211
        if (selected == -1) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   212
            state = SDL_ASSERTION_IGNORE;
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   213
        } else {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   214
            state = (SDL_assert_state)selected;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
    }
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   217
#ifdef HAVE_STDIO_H
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   218
    else
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   219
    {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   220
        /* this is a little hacky. */
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   221
        for ( ; ; ) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   222
            char buf[32];
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   223
            fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   224
            fflush(stderr);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   225
            if (fgets(buf, sizeof (buf), stdin) == NULL) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   226
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   227
            }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   228
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   229
            if (SDL_strcmp(buf, "a") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   230
                state = SDL_ASSERTION_ABORT;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   231
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   232
            } else if (SDL_strcmp(buf, "b") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   233
                state = SDL_ASSERTION_BREAK;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   234
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   235
            } else if (SDL_strcmp(buf, "r") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   236
                state = SDL_ASSERTION_RETRY;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   237
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   238
            } else if (SDL_strcmp(buf, "i") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   239
                state = SDL_ASSERTION_IGNORE;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   240
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   241
            } else if (SDL_strcmp(buf, "A") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   242
                state = SDL_ASSERTION_ALWAYS_IGNORE;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   243
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   244
            }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   245
        }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   246
    }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   247
#endif /* HAVE_STDIO_H */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   249
    /* Re-enter fullscreen mode */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   250
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   251
        SDL_RestoreWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   252
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   253
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   254
    SDL_stack_free(message);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   255
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   256
    return state;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
SDL_assert_state
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   261
SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   262
                    int line)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
{
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   264
    static int assertion_running = 0;
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   265
    static SDL_SpinLock spinlock = 0;
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   266
    SDL_assert_state state = SDL_ASSERTION_IGNORE;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   268
    SDL_AtomicLock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   269
    if (assertion_mutex == NULL) { /* never called SDL_Init()? */
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   270
        assertion_mutex = SDL_CreateMutex();
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   271
        if (assertion_mutex == NULL) {
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   272
            SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   273
            return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   274
        }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   275
    }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   276
    SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   277
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   278
    if (SDL_LockMutex(assertion_mutex) < 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
        return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
    /* doing this because Visual C is upset over assigning in the macro. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    if (data->trigger_count == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
        data->function = func;
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   285
        data->filename = file;
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   286
        data->linenum = line;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
    SDL_AddAssertionToReport(data);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   291
    assertion_running++;
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   292
    if (assertion_running > 1) {   /* assert during assert! Abort. */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   293
        if (assertion_running == 2) {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   294
            SDL_AbortAssertion();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   295
        } else if (assertion_running == 3) {  /* Abort asserted! */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   296
            SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   297
        } else {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   298
            while (1) { /* do nothing but spin; what else can you do?! */ }
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   299
        }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   302
    if (!data->always_ignore) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   303
        state = assertion_handler(data, assertion_userdata);
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   304
    }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
    switch (state)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   307
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   308
        case SDL_ASSERTION_ABORT:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
            SDL_AbortAssertion();
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
            return SDL_ASSERTION_IGNORE;  /* shouldn't return, but oh well. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
        case SDL_ASSERTION_ALWAYS_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   313
            state = SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   314
            data->always_ignore = 1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   316
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   317
        case SDL_ASSERTION_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   318
        case SDL_ASSERTION_RETRY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
        case SDL_ASSERTION_BREAK:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   320
            break;  /* macro handles these. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   323
    assertion_running--;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
    SDL_UnlockMutex(assertion_mutex);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   325
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   326
    return state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
void SDL_AssertionsQuit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   331
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   332
    SDL_GenerateAssertionReport();
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   333
    if (assertion_mutex != NULL) {
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   334
        SDL_DestroyMutex(assertion_mutex);
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   335
        assertion_mutex = NULL;
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   336
    }
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   337
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   338
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   339
void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   340
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   341
    if (handler != NULL) {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   342
        assertion_handler = handler;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   343
        assertion_userdata = userdata;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   344
    } else {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   345
        assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   346
        assertion_userdata = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   347
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   348
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   349
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   350
const SDL_assert_data *SDL_GetAssertionReport(void)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   351
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   352
    return triggered_assertions;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   353
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   354
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   355
void SDL_ResetAssertionReport(void)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   356
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   357
    SDL_assert_data *next = NULL;
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   358
    SDL_assert_data *item;
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   359
    for (item = triggered_assertions; item != NULL; item = next) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   360
        next = (SDL_assert_data *) item->next;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   361
        item->always_ignore = SDL_FALSE;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   362
        item->trigger_count = 0;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   363
        item->next = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   364
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   365
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   366
    triggered_assertions = NULL;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   367
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   369
/* vi: set ts=4 sw=4 expandtab: */