src/SDL_log.c
author Sam Lantinga
Tue, 30 Oct 2012 12:30:02 -0700
changeset 6616 4f272256d172
parent 6138 4c64952a58fb
child 6717 2acd95060548
child 8337 4a67a3cca43d
permissions -rw-r--r--
Added an assert log category, and NSLog support on Mac OS X and iOS
slouken@5221
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@5221
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5221
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5221
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5221
    20
*/
slouken@5221
    21
#include "SDL_config.h"
slouken@5221
    22
slouken@5221
    23
/* Simple log messages in SDL */
slouken@5221
    24
slouken@5221
    25
#include "SDL_log.h"
slouken@5221
    26
slouken@5221
    27
#if HAVE_STDIO_H
slouken@5221
    28
#include <stdio.h>
slouken@5221
    29
#endif
slouken@5221
    30
slouken@5221
    31
#if defined(__WIN32__)
slouken@5221
    32
#include "core/windows/SDL_windows.h"
slouken@5221
    33
#elif defined(__ANDROID__)
slouken@5221
    34
#include <android/log.h>
slouken@5221
    35
#endif
slouken@5221
    36
slouken@5221
    37
#define DEFAULT_PRIORITY                SDL_LOG_PRIORITY_CRITICAL
slouken@6616
    38
#define DEFAULT_ASSERT_PRIORITY         SDL_LOG_PRIORITY_WARN
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@6616
    54
static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
slouken@6616
    55
static SDL_LogPriority SDL_assert_priority = DEFAULT_ASSERT_PRIORITY;
slouken@5221
    56
static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
slouken@5235
    57
static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
slouken@5235
    58
static void *SDL_log_userdata = NULL;
slouken@5221
    59
slouken@5221
    60
static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
slouken@5235
    61
    NULL,
slouken@5221
    62
    "VERBOSE",
slouken@5221
    63
    "DEBUG",
slouken@5221
    64
    "INFO",
slouken@5221
    65
    "WARN",
slouken@5221
    66
    "ERROR",
slouken@5221
    67
    "CRITICAL"
slouken@5221
    68
};
slouken@5221
    69
slouken@5221
    70
#ifdef __ANDROID__
slouken@5221
    71
static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = {
slouken@5221
    72
    "APP",
slouken@5221
    73
    "ERROR",
slouken@5221
    74
    "SYSTEM",
slouken@5221
    75
    "AUDIO",
slouken@5221
    76
    "VIDEO",
slouken@5221
    77
    "RENDER",
slouken@5221
    78
    "INPUT"
slouken@5221
    79
};
slouken@5221
    80
slouken@5221
    81
static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = {
slouken@5221
    82
    ANDROID_LOG_VERBOSE,
slouken@5221
    83
    ANDROID_LOG_DEBUG,
slouken@5221
    84
    ANDROID_LOG_INFO,
slouken@5221
    85
    ANDROID_LOG_WARN,
slouken@5221
    86
    ANDROID_LOG_ERROR,
slouken@5221
    87
    ANDROID_LOG_FATAL
slouken@5221
    88
};
slouken@5221
    89
#endif /* __ANDROID__ */
slouken@5221
    90
slouken@5221
    91
slouken@5221
    92
void
slouken@5221
    93
SDL_LogSetAllPriority(SDL_LogPriority priority)
slouken@5221
    94
{
slouken@5221
    95
    SDL_LogLevel *entry;
slouken@5221
    96
slouken@5221
    97
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
    98
        entry->priority = priority;
slouken@5221
    99
    }
slouken@6616
   100
    SDL_default_priority = priority;
slouken@6616
   101
    SDL_assert_priority = priority;
slouken@6616
   102
    SDL_application_priority = priority;
slouken@5221
   103
}
slouken@5221
   104
slouken@5221
   105
void
slouken@5221
   106
SDL_LogSetPriority(int category, SDL_LogPriority priority)
slouken@5221
   107
{
slouken@5221
   108
    SDL_LogLevel *entry;
slouken@5221
   109
slouken@5221
   110
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   111
        if (entry->category == category) {
slouken@5221
   112
            entry->priority = priority;
slouken@5221
   113
            return;
slouken@5221
   114
        }
slouken@5221
   115
    }
slouken@5221
   116
slouken@5221
   117
    /* Create a new entry */
slouken@5221
   118
    entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
slouken@5221
   119
    if (entry) {
slouken@5221
   120
        entry->category = category;
slouken@5221
   121
        entry->priority = priority;
slouken@5221
   122
        entry->next = SDL_loglevels;
slouken@5221
   123
        SDL_loglevels = entry;
slouken@5221
   124
    }
slouken@5221
   125
}
slouken@5221
   126
slouken@5221
   127
SDL_LogPriority
slouken@5221
   128
SDL_LogGetPriority(int category)
slouken@5221
   129
{
slouken@5221
   130
    SDL_LogLevel *entry;
slouken@5221
   131
slouken@5221
   132
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   133
        if (entry->category == category) {
slouken@5221
   134
            return entry->priority;
slouken@5221
   135
        }
slouken@5221
   136
    }
slouken@5221
   137
slouken@5221
   138
    if (category == SDL_LOG_CATEGORY_APPLICATION) {
slouken@5221
   139
        return SDL_application_priority;
slouken@6616
   140
    } else if (category == SDL_LOG_CATEGORY_ASSERT) {
slouken@6616
   141
        return SDL_assert_priority;
slouken@5221
   142
    } else {
slouken@5221
   143
        return SDL_default_priority;
slouken@5221
   144
    }
slouken@5221
   145
}
slouken@5221
   146
slouken@5221
   147
void
slouken@5221
   148
SDL_LogResetPriorities(void)
slouken@5221
   149
{
slouken@5221
   150
    SDL_LogLevel *entry;
slouken@5221
   151
slouken@5221
   152
    while (SDL_loglevels) {
slouken@5221
   153
        entry = SDL_loglevels;
slouken@5221
   154
        SDL_loglevels = entry->next;
slouken@5221
   155
        SDL_free(entry);
slouken@5221
   156
    }
slouken@5221
   157
slouken@6616
   158
    SDL_default_priority = DEFAULT_PRIORITY;
slouken@6616
   159
    SDL_assert_priority = DEFAULT_ASSERT_PRIORITY;
slouken@5221
   160
    SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
slouken@5221
   161
}
slouken@5221
   162
slouken@5221
   163
void
slouken@5221
   164
SDL_Log(const char *fmt, ...)
slouken@5221
   165
{
slouken@5221
   166
    va_list ap;
slouken@5221
   167
slouken@5221
   168
    va_start(ap, fmt);
slouken@5221
   169
    SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   170
    va_end(ap);
slouken@5221
   171
}
slouken@5221
   172
slouken@5221
   173
void
slouken@5221
   174
SDL_LogVerbose(int category, const char *fmt, ...)
slouken@5221
   175
{
slouken@5221
   176
    va_list ap;
slouken@5221
   177
slouken@5221
   178
    va_start(ap, fmt);
slouken@5221
   179
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
slouken@5221
   180
    va_end(ap);
slouken@5221
   181
}
slouken@5221
   182
slouken@5221
   183
void
slouken@5348
   184
SDL_LogDebug(int category, const char *fmt, ...)
slouken@5348
   185
{
slouken@5348
   186
    va_list ap;
slouken@5348
   187
slouken@5348
   188
    va_start(ap, fmt);
slouken@5348
   189
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_DEBUG, fmt, ap);
slouken@5348
   190
    va_end(ap);
slouken@5348
   191
}
slouken@5348
   192
slouken@5348
   193
void
slouken@5221
   194
SDL_LogInfo(int category, const char *fmt, ...)
slouken@5221
   195
{
slouken@5221
   196
    va_list ap;
slouken@5221
   197
slouken@5221
   198
    va_start(ap, fmt);
slouken@5221
   199
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   200
    va_end(ap);
slouken@5221
   201
}
slouken@5221
   202
slouken@5221
   203
void
slouken@5221
   204
SDL_LogWarn(int category, const char *fmt, ...)
slouken@5221
   205
{
slouken@5221
   206
    va_list ap;
slouken@5221
   207
slouken@5221
   208
    va_start(ap, fmt);
slouken@5221
   209
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
slouken@5221
   210
    va_end(ap);
slouken@5221
   211
}
slouken@5221
   212
slouken@5221
   213
void
slouken@5221
   214
SDL_LogError(int category, const char *fmt, ...)
slouken@5221
   215
{
slouken@5221
   216
    va_list ap;
slouken@5221
   217
slouken@5221
   218
    va_start(ap, fmt);
slouken@5221
   219
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
slouken@5221
   220
    va_end(ap);
slouken@5221
   221
}
slouken@5221
   222
slouken@5221
   223
void
slouken@5221
   224
SDL_LogCritical(int category, const char *fmt, ...)
slouken@5221
   225
{
slouken@5221
   226
    va_list ap;
slouken@5221
   227
slouken@5221
   228
    va_start(ap, fmt);
slouken@5221
   229
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
slouken@5221
   230
    va_end(ap);
slouken@5221
   231
}
slouken@5221
   232
slouken@5221
   233
void
slouken@5221
   234
SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...)
slouken@5221
   235
{
slouken@5221
   236
    va_list ap;
slouken@5221
   237
slouken@5221
   238
    va_start(ap, fmt);
slouken@5221
   239
    SDL_LogMessageV(category, priority, fmt, ap);
slouken@5221
   240
    va_end(ap);
slouken@5221
   241
}
slouken@5221
   242
slouken@5221
   243
#ifdef __ANDROID__
slouken@5221
   244
static const char *
slouken@5221
   245
GetCategoryPrefix(int category)
slouken@5221
   246
{
slouken@5221
   247
    if (category < SDL_LOG_CATEGORY_RESERVED1) {
slouken@5221
   248
        return SDL_category_prefixes[category];
slouken@5221
   249
    }
slouken@5221
   250
    if (category < SDL_LOG_CATEGORY_CUSTOM) {
slouken@5221
   251
        return "RESERVED";
slouken@5221
   252
    }
slouken@5221
   253
    return "CUSTOM";
slouken@5221
   254
}
slouken@5221
   255
#endif /* __ANDROID__ */
slouken@5221
   256
slouken@5221
   257
void
slouken@5221
   258
SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
slouken@5221
   259
{
slouken@5223
   260
    char *message;
slouken@5221
   261
slouken@5235
   262
    /* Nothing to do if we don't have an output function */
slouken@5235
   263
    if (!SDL_log_function) {
slouken@5235
   264
        return;
slouken@5235
   265
    }
slouken@5235
   266
slouken@5221
   267
    /* Make sure we don't exceed array bounds */
slouken@6040
   268
    if ((int)priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
slouken@5221
   269
        return;
slouken@5221
   270
    }
slouken@5221
   271
slouken@5221
   272
    /* See if we want to do anything with this message */
slouken@5221
   273
    if (priority < SDL_LogGetPriority(category)) {
slouken@5221
   274
        return;
slouken@5221
   275
    }
slouken@5221
   276
slouken@5223
   277
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@5223
   278
    if (!message) {
slouken@5223
   279
        return;
slouken@5223
   280
    }
slouken@5223
   281
    SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
slouken@5235
   282
    SDL_log_function(SDL_log_userdata, category, priority, message);
slouken@5235
   283
    SDL_stack_free(message);
slouken@5235
   284
}
slouken@5221
   285
slouken@5235
   286
static void
slouken@5235
   287
SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
slouken@5235
   288
              const char *message)
slouken@5235
   289
{
slouken@5221
   290
#if defined(__WIN32__)
slouken@5223
   291
    /* Way too many allocations here, urgh */
slouken@5221
   292
    {
slouken@5223
   293
        char *output;
slouken@5223
   294
        size_t length;
slouken@5221
   295
        LPTSTR tstr;
slouken@5221
   296
slouken@5223
   297
        length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1;
slouken@5223
   298
        output = SDL_stack_alloc(char, length);
slouken@5223
   299
        SDL_snprintf(output, length, "%s: %s", SDL_priority_prefixes[priority], message);
slouken@5221
   300
        tstr = WIN_UTF8ToString(output);
slouken@5221
   301
        OutputDebugString(tstr);
slouken@5221
   302
        SDL_free(tstr);
slouken@5223
   303
        SDL_stack_free(output);
slouken@5221
   304
    }
slouken@5221
   305
#elif defined(__ANDROID__)
slouken@5221
   306
    {
slouken@5221
   307
        char tag[32];
slouken@5221
   308
slouken@5221
   309
        SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
slouken@5221
   310
        __android_log_write(SDL_android_priority[priority], tag, message);
slouken@5221
   311
    }
slouken@6616
   312
#elif defined(__APPLE__)
slouken@6616
   313
    extern void SDL_NSLog(const char *text);
slouken@6616
   314
    {
slouken@6616
   315
        char *text;
slouken@6616
   316
slouken@6616
   317
        text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@6616
   318
        if (text) {
slouken@6616
   319
            SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message);
slouken@6616
   320
            SDL_NSLog(text);
slouken@6616
   321
            SDL_stack_free(text);
slouken@6616
   322
            return;
slouken@6616
   323
        }
slouken@6616
   324
    }
slouken@5221
   325
#endif
slouken@5221
   326
#if HAVE_STDIO_H
slouken@5221
   327
    fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@5221
   328
#endif
slouken@5235
   329
}
slouken@5235
   330
slouken@5235
   331
void
slouken@5235
   332
SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
slouken@5235
   333
{
slouken@5235
   334
    if (callback) {
slouken@5235
   335
        *callback = SDL_log_function;
slouken@5235
   336
    }
slouken@5235
   337
    if (userdata) {
slouken@5235
   338
        *userdata = SDL_log_userdata;
slouken@5235
   339
    }
slouken@5235
   340
}
slouken@5235
   341
slouken@5235
   342
void
slouken@5235
   343
SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
slouken@5235
   344
{
slouken@5235
   345
    SDL_log_function = callback;
slouken@5235
   346
    SDL_log_userdata = userdata;
slouken@5221
   347
}
slouken@5221
   348
slouken@5221
   349
/* vi: set ts=4 sw=4 expandtab: */