src/SDL_log.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Mar 2011 23:44:51 -0700
changeset 5524 b7f53a566ef1
parent 5348 16202b090a63
child 5535 96594ac5fd1a
permissions -rw-r--r--
Filter out SDL_WINDOWEVENT_SIZE_CHANGED events too
slouken@5221
     1
/*
slouken@5221
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@5235
    48
/* The default log output function */
slouken@5235
    49
static void SDL_LogOutput(void *userdata,
slouken@5235
    50
                          int category, SDL_LogPriority priority,
slouken@5235
    51
                          const char *message);
slouken@5235
    52
slouken@5221
    53
static SDL_LogLevel *SDL_loglevels;
slouken@5221
    54
static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
slouken@5221
    55
static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
slouken@5235
    56
static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
slouken@5235
    57
static void *SDL_log_userdata = NULL;
slouken@5221
    58
slouken@5221
    59
static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
slouken@5235
    60
    NULL,
slouken@5221
    61
    "VERBOSE",
slouken@5221
    62
    "DEBUG",
slouken@5221
    63
    "INFO",
slouken@5221
    64
    "WARN",
slouken@5221
    65
    "ERROR",
slouken@5221
    66
    "CRITICAL"
slouken@5221
    67
};
slouken@5221
    68
slouken@5221
    69
#ifdef __ANDROID__
slouken@5221
    70
static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = {
slouken@5221
    71
    "APP",
slouken@5221
    72
    "ERROR",
slouken@5221
    73
    "SYSTEM",
slouken@5221
    74
    "AUDIO",
slouken@5221
    75
    "VIDEO",
slouken@5221
    76
    "RENDER",
slouken@5221
    77
    "INPUT"
slouken@5221
    78
};
slouken@5221
    79
slouken@5221
    80
static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = {
slouken@5221
    81
    ANDROID_LOG_VERBOSE,
slouken@5221
    82
    ANDROID_LOG_DEBUG,
slouken@5221
    83
    ANDROID_LOG_INFO,
slouken@5221
    84
    ANDROID_LOG_WARN,
slouken@5221
    85
    ANDROID_LOG_ERROR,
slouken@5221
    86
    ANDROID_LOG_FATAL
slouken@5221
    87
};
slouken@5221
    88
#endif /* __ANDROID__ */
slouken@5221
    89
slouken@5221
    90
slouken@5221
    91
void
slouken@5221
    92
SDL_LogSetAllPriority(SDL_LogPriority priority)
slouken@5221
    93
{
slouken@5221
    94
    SDL_LogLevel *entry;
slouken@5221
    95
slouken@5221
    96
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
    97
        entry->priority = priority;
slouken@5221
    98
    }
slouken@5221
    99
    SDL_application_priority = SDL_default_priority = priority;
slouken@5221
   100
}
slouken@5221
   101
slouken@5221
   102
void
slouken@5221
   103
SDL_LogSetPriority(int category, SDL_LogPriority priority)
slouken@5221
   104
{
slouken@5221
   105
    SDL_LogLevel *entry;
slouken@5221
   106
slouken@5221
   107
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   108
        if (entry->category == category) {
slouken@5221
   109
            entry->priority = priority;
slouken@5221
   110
            return;
slouken@5221
   111
        }
slouken@5221
   112
    }
slouken@5221
   113
slouken@5221
   114
    /* Create a new entry */
slouken@5221
   115
    entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
slouken@5221
   116
    if (entry) {
slouken@5221
   117
        entry->category = category;
slouken@5221
   118
        entry->priority = priority;
slouken@5221
   119
        entry->next = SDL_loglevels;
slouken@5221
   120
        SDL_loglevels = entry;
slouken@5221
   121
    }
slouken@5221
   122
}
slouken@5221
   123
slouken@5221
   124
SDL_LogPriority
slouken@5221
   125
SDL_LogGetPriority(int category)
slouken@5221
   126
{
slouken@5221
   127
    SDL_LogLevel *entry;
slouken@5221
   128
slouken@5221
   129
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   130
        if (entry->category == category) {
slouken@5221
   131
            return entry->priority;
slouken@5221
   132
        }
slouken@5221
   133
    }
slouken@5221
   134
slouken@5221
   135
    if (category == SDL_LOG_CATEGORY_APPLICATION) {
slouken@5221
   136
        return SDL_application_priority;
slouken@5221
   137
    } else {
slouken@5221
   138
        return SDL_default_priority;
slouken@5221
   139
    }
slouken@5221
   140
}
slouken@5221
   141
slouken@5221
   142
void
slouken@5221
   143
SDL_LogResetPriorities(void)
slouken@5221
   144
{
slouken@5221
   145
    SDL_LogLevel *entry;
slouken@5221
   146
slouken@5221
   147
    while (SDL_loglevels) {
slouken@5221
   148
        entry = SDL_loglevels;
slouken@5221
   149
        SDL_loglevels = entry->next;
slouken@5221
   150
        SDL_free(entry);
slouken@5221
   151
    }
slouken@5221
   152
slouken@5221
   153
    SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
slouken@5221
   154
    SDL_default_priority = DEFAULT_PRIORITY;
slouken@5221
   155
}
slouken@5221
   156
slouken@5221
   157
void
slouken@5221
   158
SDL_Log(const char *fmt, ...)
slouken@5221
   159
{
slouken@5221
   160
    va_list ap;
slouken@5221
   161
slouken@5221
   162
    va_start(ap, fmt);
slouken@5221
   163
    SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   164
    va_end(ap);
slouken@5221
   165
}
slouken@5221
   166
slouken@5221
   167
void
slouken@5221
   168
SDL_LogVerbose(int category, const char *fmt, ...)
slouken@5221
   169
{
slouken@5221
   170
    va_list ap;
slouken@5221
   171
slouken@5221
   172
    va_start(ap, fmt);
slouken@5221
   173
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
slouken@5221
   174
    va_end(ap);
slouken@5221
   175
}
slouken@5221
   176
slouken@5221
   177
void
slouken@5348
   178
SDL_LogDebug(int category, const char *fmt, ...)
slouken@5348
   179
{
slouken@5348
   180
    va_list ap;
slouken@5348
   181
slouken@5348
   182
    va_start(ap, fmt);
slouken@5348
   183
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_DEBUG, fmt, ap);
slouken@5348
   184
    va_end(ap);
slouken@5348
   185
}
slouken@5348
   186
slouken@5348
   187
void
slouken@5221
   188
SDL_LogInfo(int category, const char *fmt, ...)
slouken@5221
   189
{
slouken@5221
   190
    va_list ap;
slouken@5221
   191
slouken@5221
   192
    va_start(ap, fmt);
slouken@5221
   193
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   194
    va_end(ap);
slouken@5221
   195
}
slouken@5221
   196
slouken@5221
   197
void
slouken@5221
   198
SDL_LogWarn(int category, const char *fmt, ...)
slouken@5221
   199
{
slouken@5221
   200
    va_list ap;
slouken@5221
   201
slouken@5221
   202
    va_start(ap, fmt);
slouken@5221
   203
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
slouken@5221
   204
    va_end(ap);
slouken@5221
   205
}
slouken@5221
   206
slouken@5221
   207
void
slouken@5221
   208
SDL_LogError(int category, const char *fmt, ...)
slouken@5221
   209
{
slouken@5221
   210
    va_list ap;
slouken@5221
   211
slouken@5221
   212
    va_start(ap, fmt);
slouken@5221
   213
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
slouken@5221
   214
    va_end(ap);
slouken@5221
   215
}
slouken@5221
   216
slouken@5221
   217
void
slouken@5221
   218
SDL_LogCritical(int category, const char *fmt, ...)
slouken@5221
   219
{
slouken@5221
   220
    va_list ap;
slouken@5221
   221
slouken@5221
   222
    va_start(ap, fmt);
slouken@5221
   223
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
slouken@5221
   224
    va_end(ap);
slouken@5221
   225
}
slouken@5221
   226
slouken@5221
   227
void
slouken@5221
   228
SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...)
slouken@5221
   229
{
slouken@5221
   230
    va_list ap;
slouken@5221
   231
slouken@5221
   232
    va_start(ap, fmt);
slouken@5221
   233
    SDL_LogMessageV(category, priority, fmt, ap);
slouken@5221
   234
    va_end(ap);
slouken@5221
   235
}
slouken@5221
   236
slouken@5221
   237
#ifdef __ANDROID__
slouken@5221
   238
static const char *
slouken@5221
   239
GetCategoryPrefix(int category)
slouken@5221
   240
{
slouken@5221
   241
    if (category < SDL_LOG_CATEGORY_RESERVED1) {
slouken@5221
   242
        return SDL_category_prefixes[category];
slouken@5221
   243
    }
slouken@5221
   244
    if (category < SDL_LOG_CATEGORY_CUSTOM) {
slouken@5221
   245
        return "RESERVED";
slouken@5221
   246
    }
slouken@5221
   247
    return "CUSTOM";
slouken@5221
   248
}
slouken@5221
   249
#endif /* __ANDROID__ */
slouken@5221
   250
slouken@5221
   251
void
slouken@5221
   252
SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
slouken@5221
   253
{
slouken@5223
   254
    char *message;
slouken@5221
   255
slouken@5235
   256
    /* Nothing to do if we don't have an output function */
slouken@5235
   257
    if (!SDL_log_function) {
slouken@5235
   258
        return;
slouken@5235
   259
    }
slouken@5235
   260
slouken@5221
   261
    /* Make sure we don't exceed array bounds */
slouken@5221
   262
    if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
slouken@5221
   263
        return;
slouken@5221
   264
    }
slouken@5221
   265
slouken@5221
   266
    /* See if we want to do anything with this message */
slouken@5221
   267
    if (priority < SDL_LogGetPriority(category)) {
slouken@5221
   268
        return;
slouken@5221
   269
    }
slouken@5221
   270
slouken@5223
   271
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@5223
   272
    if (!message) {
slouken@5223
   273
        return;
slouken@5223
   274
    }
slouken@5223
   275
    SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
slouken@5235
   276
    SDL_log_function(SDL_log_userdata, category, priority, message);
slouken@5235
   277
    SDL_stack_free(message);
slouken@5235
   278
}
slouken@5221
   279
slouken@5235
   280
static void
slouken@5235
   281
SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
slouken@5235
   282
              const char *message)
slouken@5235
   283
{
slouken@5221
   284
#if defined(__WIN32__)
slouken@5223
   285
    /* Way too many allocations here, urgh */
slouken@5221
   286
    {
slouken@5223
   287
        char *output;
slouken@5223
   288
        size_t length;
slouken@5221
   289
        LPTSTR tstr;
slouken@5221
   290
slouken@5223
   291
        length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1;
slouken@5223
   292
        output = SDL_stack_alloc(char, length);
slouken@5223
   293
        SDL_snprintf(output, length, "%s: %s", SDL_priority_prefixes[priority], message);
slouken@5221
   294
        tstr = WIN_UTF8ToString(output);
slouken@5221
   295
        OutputDebugString(tstr);
slouken@5221
   296
        SDL_free(tstr);
slouken@5223
   297
        SDL_stack_free(output);
slouken@5221
   298
    }
slouken@5221
   299
#elif defined(__ANDROID__)
slouken@5221
   300
    {
slouken@5221
   301
        char tag[32];
slouken@5221
   302
slouken@5221
   303
        SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
slouken@5221
   304
        __android_log_write(SDL_android_priority[priority], tag, message);
slouken@5221
   305
    }
slouken@5221
   306
#endif
slouken@5221
   307
#if HAVE_STDIO_H
slouken@5221
   308
    fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@5221
   309
#endif
slouken@5235
   310
}
slouken@5235
   311
slouken@5235
   312
void
slouken@5235
   313
SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
slouken@5235
   314
{
slouken@5235
   315
    if (callback) {
slouken@5235
   316
        *callback = SDL_log_function;
slouken@5235
   317
    }
slouken@5235
   318
    if (userdata) {
slouken@5235
   319
        *userdata = SDL_log_userdata;
slouken@5235
   320
    }
slouken@5235
   321
}
slouken@5235
   322
slouken@5235
   323
void
slouken@5235
   324
SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
slouken@5235
   325
{
slouken@5235
   326
    SDL_log_function = callback;
slouken@5235
   327
    SDL_log_userdata = userdata;
slouken@5221
   328
}
slouken@5221
   329
slouken@5221
   330
/* vi: set ts=4 sw=4 expandtab: */