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