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

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