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