src/SDL_log.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Feb 2011 20:05:52 -0800
changeset 5223 ab0d7cecc0f6
parent 5221 2ee8112bfc6b
child 5235 1e28342cb15d
permissions -rwxr-xr-x
Fixed stack overflow on Windows
slouken@5221
     1
/*
slouken@5221
     2
    SDL - Simple DirectMedia Layer
slouken@5221
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@5221
     4
slouken@5221
     5
    This library is free software; you can redistribute it and/or
slouken@5221
     6
    modify it under the terms of the GNU Lesser General Public
slouken@5221
     7
    License as published by the Free Software Foundation; either
slouken@5221
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@5221
     9
slouken@5221
    10
    This library is distributed in the hope that it will be useful,
slouken@5221
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5221
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5221
    13
    Lesser General Public License for more details.
slouken@5221
    14
slouken@5221
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5221
    16
    License along with this library; if not, write to the Free Software
slouken@5221
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5221
    18
slouken@5221
    19
    Sam Lantinga
slouken@5221
    20
    slouken@libsdl.org
slouken@5221
    21
*/
slouken@5221
    22
#include "SDL_config.h"
slouken@5221
    23
slouken@5221
    24
/* Simple log messages in SDL */
slouken@5221
    25
slouken@5221
    26
#include "SDL_log.h"
slouken@5221
    27
slouken@5221
    28
#if HAVE_STDIO_H
slouken@5221
    29
#include <stdio.h>
slouken@5221
    30
#endif
slouken@5221
    31
slouken@5221
    32
#if defined(__WIN32__)
slouken@5221
    33
#include "core/windows/SDL_windows.h"
slouken@5221
    34
#elif defined(__ANDROID__)
slouken@5221
    35
#include <android/log.h>
slouken@5221
    36
#endif
slouken@5221
    37
slouken@5221
    38
#define DEFAULT_PRIORITY                SDL_LOG_PRIORITY_CRITICAL
slouken@5221
    39
#define DEFAULT_APPLICATION_PRIORITY    SDL_LOG_PRIORITY_INFO
slouken@5221
    40
slouken@5221
    41
typedef struct SDL_LogLevel
slouken@5221
    42
{
slouken@5221
    43
    int category;
slouken@5221
    44
    SDL_LogPriority priority;
slouken@5221
    45
    struct SDL_LogLevel *next;
slouken@5221
    46
} SDL_LogLevel;
slouken@5221
    47
slouken@5221
    48
static SDL_LogLevel *SDL_loglevels;
slouken@5221
    49
static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
slouken@5221
    50
static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
slouken@5221
    51
slouken@5221
    52
static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
slouken@5221
    53
    "VERBOSE",
slouken@5221
    54
    "DEBUG",
slouken@5221
    55
    "INFO",
slouken@5221
    56
    "WARN",
slouken@5221
    57
    "ERROR",
slouken@5221
    58
    "CRITICAL"
slouken@5221
    59
};
slouken@5221
    60
slouken@5221
    61
#ifdef __ANDROID__
slouken@5221
    62
static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = {
slouken@5221
    63
    "APP",
slouken@5221
    64
    "ERROR",
slouken@5221
    65
    "SYSTEM",
slouken@5221
    66
    "AUDIO",
slouken@5221
    67
    "VIDEO",
slouken@5221
    68
    "RENDER",
slouken@5221
    69
    "INPUT"
slouken@5221
    70
};
slouken@5221
    71
slouken@5221
    72
static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = {
slouken@5221
    73
    ANDROID_LOG_VERBOSE,
slouken@5221
    74
    ANDROID_LOG_DEBUG,
slouken@5221
    75
    ANDROID_LOG_INFO,
slouken@5221
    76
    ANDROID_LOG_WARN,
slouken@5221
    77
    ANDROID_LOG_ERROR,
slouken@5221
    78
    ANDROID_LOG_FATAL
slouken@5221
    79
};
slouken@5221
    80
#endif /* __ANDROID__ */
slouken@5221
    81
slouken@5221
    82
slouken@5221
    83
void
slouken@5221
    84
SDL_LogSetAllPriority(SDL_LogPriority priority)
slouken@5221
    85
{
slouken@5221
    86
    SDL_LogLevel *entry;
slouken@5221
    87
slouken@5221
    88
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
    89
        entry->priority = priority;
slouken@5221
    90
    }
slouken@5221
    91
    SDL_application_priority = SDL_default_priority = priority;
slouken@5221
    92
}
slouken@5221
    93
slouken@5221
    94
void
slouken@5221
    95
SDL_LogSetPriority(int category, SDL_LogPriority priority)
slouken@5221
    96
{
slouken@5221
    97
    SDL_LogLevel *entry;
slouken@5221
    98
slouken@5221
    99
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   100
        if (entry->category == category) {
slouken@5221
   101
            entry->priority = priority;
slouken@5221
   102
            return;
slouken@5221
   103
        }
slouken@5221
   104
    }
slouken@5221
   105
slouken@5221
   106
    /* Create a new entry */
slouken@5221
   107
    entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
slouken@5221
   108
    if (entry) {
slouken@5221
   109
        entry->category = category;
slouken@5221
   110
        entry->priority = priority;
slouken@5221
   111
        entry->next = SDL_loglevels;
slouken@5221
   112
        SDL_loglevels = entry;
slouken@5221
   113
    }
slouken@5221
   114
}
slouken@5221
   115
slouken@5221
   116
SDL_LogPriority
slouken@5221
   117
SDL_LogGetPriority(int category)
slouken@5221
   118
{
slouken@5221
   119
    SDL_LogLevel *entry;
slouken@5221
   120
slouken@5221
   121
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   122
        if (entry->category == category) {
slouken@5221
   123
            return entry->priority;
slouken@5221
   124
        }
slouken@5221
   125
    }
slouken@5221
   126
slouken@5221
   127
    if (category == SDL_LOG_CATEGORY_APPLICATION) {
slouken@5221
   128
        return SDL_application_priority;
slouken@5221
   129
    } else {
slouken@5221
   130
        return SDL_default_priority;
slouken@5221
   131
    }
slouken@5221
   132
}
slouken@5221
   133
slouken@5221
   134
void
slouken@5221
   135
SDL_LogResetPriorities(void)
slouken@5221
   136
{
slouken@5221
   137
    SDL_LogLevel *entry;
slouken@5221
   138
slouken@5221
   139
    while (SDL_loglevels) {
slouken@5221
   140
        entry = SDL_loglevels;
slouken@5221
   141
        SDL_loglevels = entry->next;
slouken@5221
   142
        SDL_free(entry);
slouken@5221
   143
    }
slouken@5221
   144
slouken@5221
   145
    SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
slouken@5221
   146
    SDL_default_priority = DEFAULT_PRIORITY;
slouken@5221
   147
}
slouken@5221
   148
slouken@5221
   149
void
slouken@5221
   150
SDL_Log(const char *fmt, ...)
slouken@5221
   151
{
slouken@5221
   152
    va_list ap;
slouken@5221
   153
slouken@5221
   154
    va_start(ap, fmt);
slouken@5221
   155
    SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   156
    va_end(ap);
slouken@5221
   157
}
slouken@5221
   158
slouken@5221
   159
void
slouken@5221
   160
SDL_LogVerbose(int category, const char *fmt, ...)
slouken@5221
   161
{
slouken@5221
   162
    va_list ap;
slouken@5221
   163
slouken@5221
   164
    va_start(ap, fmt);
slouken@5221
   165
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
slouken@5221
   166
    va_end(ap);
slouken@5221
   167
}
slouken@5221
   168
slouken@5221
   169
void
slouken@5221
   170
SDL_LogInfo(int category, const char *fmt, ...)
slouken@5221
   171
{
slouken@5221
   172
    va_list ap;
slouken@5221
   173
slouken@5221
   174
    va_start(ap, fmt);
slouken@5221
   175
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   176
    va_end(ap);
slouken@5221
   177
}
slouken@5221
   178
slouken@5221
   179
void
slouken@5221
   180
SDL_LogWarn(int category, const char *fmt, ...)
slouken@5221
   181
{
slouken@5221
   182
    va_list ap;
slouken@5221
   183
slouken@5221
   184
    va_start(ap, fmt);
slouken@5221
   185
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
slouken@5221
   186
    va_end(ap);
slouken@5221
   187
}
slouken@5221
   188
slouken@5221
   189
void
slouken@5221
   190
SDL_LogError(int category, const char *fmt, ...)
slouken@5221
   191
{
slouken@5221
   192
    va_list ap;
slouken@5221
   193
slouken@5221
   194
    va_start(ap, fmt);
slouken@5221
   195
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
slouken@5221
   196
    va_end(ap);
slouken@5221
   197
}
slouken@5221
   198
slouken@5221
   199
void
slouken@5221
   200
SDL_LogCritical(int category, const char *fmt, ...)
slouken@5221
   201
{
slouken@5221
   202
    va_list ap;
slouken@5221
   203
slouken@5221
   204
    va_start(ap, fmt);
slouken@5221
   205
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
slouken@5221
   206
    va_end(ap);
slouken@5221
   207
}
slouken@5221
   208
slouken@5221
   209
void
slouken@5221
   210
SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...)
slouken@5221
   211
{
slouken@5221
   212
    va_list ap;
slouken@5221
   213
slouken@5221
   214
    va_start(ap, fmt);
slouken@5221
   215
    SDL_LogMessageV(category, priority, fmt, ap);
slouken@5221
   216
    va_end(ap);
slouken@5221
   217
}
slouken@5221
   218
slouken@5221
   219
#ifdef __ANDROID__
slouken@5221
   220
static const char *
slouken@5221
   221
GetCategoryPrefix(int category)
slouken@5221
   222
{
slouken@5221
   223
    if (category < SDL_LOG_CATEGORY_RESERVED1) {
slouken@5221
   224
        return SDL_category_prefixes[category];
slouken@5221
   225
    }
slouken@5221
   226
    if (category < SDL_LOG_CATEGORY_CUSTOM) {
slouken@5221
   227
        return "RESERVED";
slouken@5221
   228
    }
slouken@5221
   229
    return "CUSTOM";
slouken@5221
   230
}
slouken@5221
   231
#endif /* __ANDROID__ */
slouken@5221
   232
slouken@5221
   233
void
slouken@5221
   234
SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
slouken@5221
   235
{
slouken@5223
   236
    char *message;
slouken@5221
   237
slouken@5221
   238
    /* Make sure we don't exceed array bounds */
slouken@5221
   239
    if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
slouken@5221
   240
        return;
slouken@5221
   241
    }
slouken@5221
   242
slouken@5221
   243
    /* See if we want to do anything with this message */
slouken@5221
   244
    if (priority < SDL_LogGetPriority(category)) {
slouken@5221
   245
        return;
slouken@5221
   246
    }
slouken@5221
   247
slouken@5223
   248
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@5223
   249
    if (!message) {
slouken@5223
   250
        return;
slouken@5223
   251
    }
slouken@5223
   252
    SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
slouken@5221
   253
slouken@5221
   254
#if defined(__WIN32__)
slouken@5223
   255
    /* Way too many allocations here, urgh */
slouken@5221
   256
    {
slouken@5223
   257
        char *output;
slouken@5223
   258
        size_t length;
slouken@5221
   259
        LPTSTR tstr;
slouken@5221
   260
slouken@5223
   261
        length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1;
slouken@5223
   262
        output = SDL_stack_alloc(char, length);
slouken@5223
   263
        SDL_snprintf(output, length, "%s: %s", SDL_priority_prefixes[priority], message);
slouken@5221
   264
        tstr = WIN_UTF8ToString(output);
slouken@5221
   265
        OutputDebugString(tstr);
slouken@5221
   266
        SDL_free(tstr);
slouken@5223
   267
        SDL_stack_free(output);
slouken@5221
   268
    }
slouken@5221
   269
#elif defined(__ANDROID__)
slouken@5221
   270
    {
slouken@5221
   271
        char tag[32];
slouken@5221
   272
slouken@5221
   273
        SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
slouken@5221
   274
        __android_log_write(SDL_android_priority[priority], tag, message);
slouken@5221
   275
    }
slouken@5221
   276
#endif
slouken@5221
   277
#if HAVE_STDIO_H
slouken@5221
   278
    fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@5221
   279
#endif
slouken@5223
   280
    SDL_stack_free(message);
slouken@5221
   281
}
slouken@5221
   282
slouken@5221
   283
/* vi: set ts=4 sw=4 expandtab: */