summaryrefslogtreecommitdiff
path: root/src/utils/sdl2logger.cpp
blob: 97a6200dfe6d6e89aa13c97656bdd514dc376c97 (plain) (blame)
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
132
133
134
135
/*
 *  The ManaPlus Client
 *  Copyright (C) 2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef USE_SDL2

#include "utils/sdl2logger.h"

#include "utils/checkutils.h"

PRAGMA48(GCC diagnostic push)
PRAGMA48(GCC diagnostic ignored "-Wshadow")
#include <SDL_assert.h>
#include <SDL_log.h>
PRAGMA48(GCC diagnostic pop)

#include "debug.h"

#define logStr(category, msg) \
    case category: \
        message.append(msg); \
        break

static void logCallback(void *userData A_UNUSED,
                        int category,
                        SDL_LogPriority priority,
                        const char *msg)
{
    std::string message("SDL ERROR:");
    switch (priority)
    {
        logStr(SDL_LOG_PRIORITY_VERBOSE,
            " VERBOSE");
        logStr(SDL_LOG_PRIORITY_DEBUG,
            " DEBUG");
        logStr(SDL_LOG_PRIORITY_INFO,
            " INFO");
        logStr(SDL_LOG_PRIORITY_WARN,
            " WARN");
        logStr(SDL_LOG_PRIORITY_ERROR,
            " ERROR");
        logStr(SDL_LOG_PRIORITY_CRITICAL,
            " CRITICAL");
        case SDL_NUM_LOG_PRIORITIES:
        default:
            message.append(" ?");
            break;
    }

    switch (category)
    {
        logStr(SDL_LOG_CATEGORY_APPLICATION,
            " APPLICATION");
        logStr(SDL_LOG_CATEGORY_ERROR,
            " ERROR");
        logStr(SDL_LOG_CATEGORY_ASSERT,
            " ASSERT");
        logStr(SDL_LOG_CATEGORY_SYSTEM,
            " SYSTEM");
        logStr(SDL_LOG_CATEGORY_AUDIO,
            " AUDIO");
        logStr(SDL_LOG_CATEGORY_VIDEO,
            " VIDEO");
        logStr(SDL_LOG_CATEGORY_RENDER,
            " RENDER");
        logStr(SDL_LOG_CATEGORY_INPUT,
            " INPUT");
        logStr(SDL_LOG_CATEGORY_TEST,
            " TEST");
        logStr(SDL_LOG_CATEGORY_RESERVED1,
            " RESERVED1");
        logStr(SDL_LOG_CATEGORY_RESERVED2,
            " RESERVED2");
        logStr(SDL_LOG_CATEGORY_RESERVED3,
            " RESERVED3");
        logStr(SDL_LOG_CATEGORY_RESERVED4,
            " RESERVED4");
        logStr(SDL_LOG_CATEGORY_RESERVED5,
            " RESERVED5");
        logStr(SDL_LOG_CATEGORY_RESERVED6,
            " RESERVED6");
        logStr(SDL_LOG_CATEGORY_RESERVED7,
            " RESERVED7");
        logStr(SDL_LOG_CATEGORY_RESERVED8,
            " RESERVED8");
        logStr(SDL_LOG_CATEGORY_RESERVED9,
            " RESERVED9");
        logStr(SDL_LOG_CATEGORY_RESERVED10,
            " RESERVED10");
        logStr(SDL_LOG_CATEGORY_CUSTOM,
            " CUSTOM");
        default:
            message.append(" ?");
            break;
    }
    reportAlways("%s %s",
        message.c_str(),
        msg);
}

static SDL_AssertState assertCallback(const SDL_AssertData *data,
                                      void *userdata A_UNUSED)
{
    reportAlways(
        "SDL assert at %s (%s:%d):\n%s",
        data->function,
        data->filename,
        data->linenum,
        data->condition);
    return SDL_ASSERTION_IGNORE;
}

void SDL2Logger::init()
{
    SDL_LogSetOutputFunction(&logCallback, nullptr);
    SDL_SetAssertionHandler(&assertCallback, nullptr);
}

#endif  // USE_SDL2