GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/sdl2logger.cpp Lines: 5 44 11.4 %
Date: 2021-03-17 Branches: 0 96 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2017-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#ifdef USE_SDL2
23
24
#include "utils/sdl2logger.h"
25
26
#include "utils/checkutils.h"
27
28
PRAGMA48(GCC diagnostic push)
29
PRAGMA48(GCC diagnostic ignored "-Wshadow")
30
#include <SDL_assert.h>
31
#include <SDL_log.h>
32
PRAGMA48(GCC diagnostic pop)
33
34
#include "debug.h"
35
36
#define logStr(category, msg) \
37
    case category: \
38
        message.append(msg); \
39
        break
40
41
// before 2.0.4 this names not exists
42
#if !SDL_VERSION_ATLEAST(2, 0, 4)
43
#define SDL_AssertState SDL_assert_state
44
#define SDL_AssertData SDL_assert_data
45
#endif  // !SDL_VERSION_ATLEAST(2, 0, 4)
46
47
static void logCallback(void *userData A_UNUSED,
48
                        int category,
49
                        SDL_LogPriority priority,
50
                        const char *msg)
51
{
52
    std::string message("SDL ERROR:");
53
    switch (priority)
54
    {
55
        logStr(SDL_LOG_PRIORITY_VERBOSE,
56
            " VERBOSE");
57
        logStr(SDL_LOG_PRIORITY_DEBUG,
58
            " DEBUG");
59
        logStr(SDL_LOG_PRIORITY_INFO,
60
            " INFO");
61
        logStr(SDL_LOG_PRIORITY_WARN,
62
            " WARN");
63
        logStr(SDL_LOG_PRIORITY_ERROR,
64
            " ERROR");
65
        logStr(SDL_LOG_PRIORITY_CRITICAL,
66
            " CRITICAL");
67
        case SDL_NUM_LOG_PRIORITIES:
68
        default:
69
            message.append(" ?");
70
            break;
71
    }
72
73
    switch (category)
74
    {
75
        logStr(SDL_LOG_CATEGORY_APPLICATION,
76
            " APPLICATION");
77
        logStr(SDL_LOG_CATEGORY_ERROR,
78
            " ERROR");
79
        logStr(SDL_LOG_CATEGORY_ASSERT,
80
            " ASSERT");
81
        logStr(SDL_LOG_CATEGORY_SYSTEM,
82
            " SYSTEM");
83
        logStr(SDL_LOG_CATEGORY_AUDIO,
84
            " AUDIO");
85
        logStr(SDL_LOG_CATEGORY_VIDEO,
86
            " VIDEO");
87
        logStr(SDL_LOG_CATEGORY_RENDER,
88
            " RENDER");
89
        logStr(SDL_LOG_CATEGORY_INPUT,
90
            " INPUT");
91
        logStr(SDL_LOG_CATEGORY_TEST,
92
            " TEST");
93
        logStr(SDL_LOG_CATEGORY_RESERVED1,
94
            " RESERVED1");
95
        logStr(SDL_LOG_CATEGORY_RESERVED2,
96
            " RESERVED2");
97
        logStr(SDL_LOG_CATEGORY_RESERVED3,
98
            " RESERVED3");
99
        logStr(SDL_LOG_CATEGORY_RESERVED4,
100
            " RESERVED4");
101
        logStr(SDL_LOG_CATEGORY_RESERVED5,
102
            " RESERVED5");
103
        logStr(SDL_LOG_CATEGORY_RESERVED6,
104
            " RESERVED6");
105
        logStr(SDL_LOG_CATEGORY_RESERVED7,
106
            " RESERVED7");
107
        logStr(SDL_LOG_CATEGORY_RESERVED8,
108
            " RESERVED8");
109
        logStr(SDL_LOG_CATEGORY_RESERVED9,
110
            " RESERVED9");
111
        logStr(SDL_LOG_CATEGORY_RESERVED10,
112
            " RESERVED10");
113
        logStr(SDL_LOG_CATEGORY_CUSTOM,
114
            " CUSTOM");
115
        default:
116
            message.append(" ?");
117
            break;
118
    }
119
    reportAlways("%s %s",
120
        message.c_str(),
121
        msg)
122
}
123
124
static SDL_AssertState assertCallback(const SDL_AssertData *data,
125
                                      void *userdata A_UNUSED)
126
{
127
    reportAlways(
128
        "SDL assert at %s (%s:%d):\n%s",
129
        data->function,
130
        data->filename,
131
        data->linenum,
132
        data->condition)
133
    return SDL_ASSERTION_IGNORE;
134
}
135
136
1
void SDL2Logger::init()
137
{
138
#ifdef UNITTESTS
139
#if SDL_VERSION_ATLEAST(2, 0, 4)
140
1
    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN);
141
#else  // SDL_VERSION_ATLEAST(2, 0, 4)
142
    // versions below 2.0.4 report OpenGL error even if OpenGL was not used
143
    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_CRITICAL);
144
#endif  // SDL_VERSION_ATLEAST(2, 0, 4)
145
#else  // UNITTESTS
146
147
    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
148
#endif  // UNITTESTS
149
150
1
    SDL_LogSetOutputFunction(&logCallback, nullptr);
151
1
    SDL_SetAssertionHandler(&assertCallback, nullptr);
152
1
}
153
154
void SDL2Logger::setLogLevel(const int level)
155
{
156
    if (level > 0)
157
        SDL_LogSetAllPriority(static_cast<SDL_LogPriority>(level));
158
    else
159
        SDL_LogResetPriorities();
160
}
161
162
#endif  // USE_SDL2