src/SDL_log.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7828 1451063c8ecd
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

The new header will include SDL_config.h, but allows for other global stuff.
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
*/
icculus@8093
    21
#include "./SDL_internal.h"
slouken@5221
    22
slouken@7828
    23
#if defined(__WIN32__)
slouken@7828
    24
#include "core/windows/SDL_windows.h"
slouken@7828
    25
#endif
slouken@7828
    26
slouken@5221
    27
/* Simple log messages in SDL */
slouken@5221
    28
slouken@5221
    29
#include "SDL_log.h"
slouken@5221
    30
slouken@5221
    31
#if HAVE_STDIO_H
slouken@5221
    32
#include <stdio.h>
slouken@5221
    33
#endif
slouken@5221
    34
slouken@7828
    35
#if defined(__ANDROID__)
slouken@5221
    36
#include <android/log.h>
slouken@5221
    37
#endif
slouken@5221
    38
slouken@5221
    39
#define DEFAULT_PRIORITY                SDL_LOG_PRIORITY_CRITICAL
slouken@6616
    40
#define DEFAULT_ASSERT_PRIORITY         SDL_LOG_PRIORITY_WARN
slouken@5221
    41
#define DEFAULT_APPLICATION_PRIORITY    SDL_LOG_PRIORITY_INFO
aschiffler@6717
    42
#define DEFAULT_TEST_PRIORITY           SDL_LOG_PRIORITY_VERBOSE
slouken@5221
    43
aschiffler@7274
    44
/* Forward definition of error function */
aschiffler@7274
    45
extern int SDL_SetError(const char *fmt, ...);
aschiffler@7274
    46
slouken@5221
    47
typedef struct SDL_LogLevel
slouken@5221
    48
{
slouken@5221
    49
    int category;
slouken@5221
    50
    SDL_LogPriority priority;
slouken@5221
    51
    struct SDL_LogLevel *next;
slouken@5221
    52
} SDL_LogLevel;
slouken@5221
    53
slouken@5235
    54
/* The default log output function */
slouken@5235
    55
static void SDL_LogOutput(void *userdata,
slouken@5235
    56
                          int category, SDL_LogPriority priority,
slouken@5235
    57
                          const char *message);
slouken@5235
    58
slouken@5221
    59
static SDL_LogLevel *SDL_loglevels;
slouken@6616
    60
static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
slouken@6616
    61
static SDL_LogPriority SDL_assert_priority = DEFAULT_ASSERT_PRIORITY;
slouken@5221
    62
static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
aschiffler@6717
    63
static SDL_LogPriority SDL_test_priority = DEFAULT_TEST_PRIORITY;
slouken@5235
    64
static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
slouken@5235
    65
static void *SDL_log_userdata = NULL;
slouken@5221
    66
slouken@5221
    67
static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
slouken@5235
    68
    NULL,
slouken@5221
    69
    "VERBOSE",
slouken@5221
    70
    "DEBUG",
slouken@5221
    71
    "INFO",
slouken@5221
    72
    "WARN",
slouken@5221
    73
    "ERROR",
slouken@5221
    74
    "CRITICAL"
slouken@5221
    75
};
slouken@5221
    76
slouken@5221
    77
#ifdef __ANDROID__
slouken@5221
    78
static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = {
slouken@5221
    79
    "APP",
slouken@5221
    80
    "ERROR",
slouken@5221
    81
    "SYSTEM",
slouken@5221
    82
    "AUDIO",
slouken@5221
    83
    "VIDEO",
slouken@5221
    84
    "RENDER",
slouken@5221
    85
    "INPUT"
slouken@5221
    86
};
slouken@5221
    87
slouken@5221
    88
static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = {
slouken@7727
    89
    ANDROID_LOG_UNKNOWN,
slouken@5221
    90
    ANDROID_LOG_VERBOSE,
slouken@5221
    91
    ANDROID_LOG_DEBUG,
slouken@5221
    92
    ANDROID_LOG_INFO,
slouken@5221
    93
    ANDROID_LOG_WARN,
slouken@5221
    94
    ANDROID_LOG_ERROR,
slouken@5221
    95
    ANDROID_LOG_FATAL
slouken@5221
    96
};
slouken@5221
    97
#endif /* __ANDROID__ */
slouken@5221
    98
slouken@5221
    99
slouken@5221
   100
void
slouken@5221
   101
SDL_LogSetAllPriority(SDL_LogPriority priority)
slouken@5221
   102
{
slouken@5221
   103
    SDL_LogLevel *entry;
slouken@5221
   104
slouken@5221
   105
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   106
        entry->priority = priority;
slouken@5221
   107
    }
slouken@6616
   108
    SDL_default_priority = priority;
slouken@6616
   109
    SDL_assert_priority = priority;
slouken@6616
   110
    SDL_application_priority = priority;
slouken@5221
   111
}
slouken@5221
   112
slouken@5221
   113
void
slouken@5221
   114
SDL_LogSetPriority(int category, SDL_LogPriority priority)
slouken@5221
   115
{
slouken@5221
   116
    SDL_LogLevel *entry;
slouken@5221
   117
slouken@5221
   118
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   119
        if (entry->category == category) {
slouken@5221
   120
            entry->priority = priority;
slouken@5221
   121
            return;
slouken@5221
   122
        }
slouken@5221
   123
    }
slouken@5221
   124
slouken@5221
   125
    /* Create a new entry */
slouken@5221
   126
    entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
slouken@5221
   127
    if (entry) {
slouken@5221
   128
        entry->category = category;
slouken@5221
   129
        entry->priority = priority;
slouken@5221
   130
        entry->next = SDL_loglevels;
slouken@5221
   131
        SDL_loglevels = entry;
slouken@5221
   132
    }
slouken@5221
   133
}
slouken@5221
   134
slouken@5221
   135
SDL_LogPriority
slouken@5221
   136
SDL_LogGetPriority(int category)
slouken@5221
   137
{
slouken@5221
   138
    SDL_LogLevel *entry;
slouken@5221
   139
slouken@5221
   140
    for (entry = SDL_loglevels; entry; entry = entry->next) {
slouken@5221
   141
        if (entry->category == category) {
slouken@5221
   142
            return entry->priority;
slouken@5221
   143
        }
slouken@5221
   144
    }
slouken@5221
   145
aschiffler@6717
   146
    if (category == SDL_LOG_CATEGORY_TEST) {
slouken@7191
   147
        return SDL_test_priority;
aschiffler@6717
   148
    } else if (category == SDL_LOG_CATEGORY_APPLICATION) {
slouken@5221
   149
        return SDL_application_priority;
slouken@6616
   150
    } else if (category == SDL_LOG_CATEGORY_ASSERT) {
slouken@6616
   151
        return SDL_assert_priority;
slouken@5221
   152
    } else {
slouken@5221
   153
        return SDL_default_priority;
slouken@5221
   154
    }
slouken@5221
   155
}
slouken@5221
   156
slouken@5221
   157
void
slouken@5221
   158
SDL_LogResetPriorities(void)
slouken@5221
   159
{
slouken@5221
   160
    SDL_LogLevel *entry;
slouken@5221
   161
slouken@5221
   162
    while (SDL_loglevels) {
slouken@5221
   163
        entry = SDL_loglevels;
slouken@5221
   164
        SDL_loglevels = entry->next;
slouken@5221
   165
        SDL_free(entry);
slouken@5221
   166
    }
slouken@5221
   167
slouken@6616
   168
    SDL_default_priority = DEFAULT_PRIORITY;
slouken@6616
   169
    SDL_assert_priority = DEFAULT_ASSERT_PRIORITY;
slouken@5221
   170
    SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
aschiffler@6717
   171
    SDL_test_priority = DEFAULT_TEST_PRIORITY;
slouken@5221
   172
}
slouken@5221
   173
slouken@5221
   174
void
slouken@5221
   175
SDL_Log(const char *fmt, ...)
slouken@5221
   176
{
slouken@5221
   177
    va_list ap;
slouken@5221
   178
slouken@5221
   179
    va_start(ap, fmt);
slouken@5221
   180
    SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   181
    va_end(ap);
slouken@5221
   182
}
slouken@5221
   183
slouken@5221
   184
void
slouken@5221
   185
SDL_LogVerbose(int category, const char *fmt, ...)
slouken@5221
   186
{
slouken@5221
   187
    va_list ap;
slouken@5221
   188
slouken@5221
   189
    va_start(ap, fmt);
slouken@5221
   190
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
slouken@5221
   191
    va_end(ap);
slouken@5221
   192
}
slouken@5221
   193
slouken@5221
   194
void
slouken@5348
   195
SDL_LogDebug(int category, const char *fmt, ...)
slouken@5348
   196
{
slouken@5348
   197
    va_list ap;
slouken@5348
   198
slouken@5348
   199
    va_start(ap, fmt);
slouken@5348
   200
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_DEBUG, fmt, ap);
slouken@5348
   201
    va_end(ap);
slouken@5348
   202
}
slouken@5348
   203
slouken@5348
   204
void
slouken@5221
   205
SDL_LogInfo(int category, const char *fmt, ...)
slouken@5221
   206
{
slouken@5221
   207
    va_list ap;
slouken@5221
   208
slouken@5221
   209
    va_start(ap, fmt);
slouken@5221
   210
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
slouken@5221
   211
    va_end(ap);
slouken@5221
   212
}
slouken@5221
   213
slouken@5221
   214
void
slouken@5221
   215
SDL_LogWarn(int category, const char *fmt, ...)
slouken@5221
   216
{
slouken@5221
   217
    va_list ap;
slouken@5221
   218
slouken@5221
   219
    va_start(ap, fmt);
slouken@5221
   220
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
slouken@5221
   221
    va_end(ap);
slouken@5221
   222
}
slouken@5221
   223
slouken@5221
   224
void
slouken@5221
   225
SDL_LogError(int category, const char *fmt, ...)
slouken@5221
   226
{
slouken@5221
   227
    va_list ap;
slouken@5221
   228
slouken@5221
   229
    va_start(ap, fmt);
slouken@5221
   230
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
slouken@5221
   231
    va_end(ap);
slouken@5221
   232
}
slouken@5221
   233
slouken@5221
   234
void
slouken@5221
   235
SDL_LogCritical(int category, const char *fmt, ...)
slouken@5221
   236
{
slouken@5221
   237
    va_list ap;
slouken@5221
   238
slouken@5221
   239
    va_start(ap, fmt);
slouken@5221
   240
    SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
slouken@5221
   241
    va_end(ap);
slouken@5221
   242
}
slouken@5221
   243
slouken@5221
   244
void
slouken@5221
   245
SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...)
slouken@5221
   246
{
slouken@5221
   247
    va_list ap;
slouken@5221
   248
slouken@5221
   249
    va_start(ap, fmt);
slouken@5221
   250
    SDL_LogMessageV(category, priority, fmt, ap);
slouken@5221
   251
    va_end(ap);
slouken@5221
   252
}
slouken@5221
   253
slouken@5221
   254
#ifdef __ANDROID__
slouken@5221
   255
static const char *
slouken@5221
   256
GetCategoryPrefix(int category)
slouken@5221
   257
{
slouken@5221
   258
    if (category < SDL_LOG_CATEGORY_RESERVED1) {
slouken@5221
   259
        return SDL_category_prefixes[category];
slouken@5221
   260
    }
slouken@5221
   261
    if (category < SDL_LOG_CATEGORY_CUSTOM) {
slouken@5221
   262
        return "RESERVED";
slouken@5221
   263
    }
slouken@5221
   264
    return "CUSTOM";
slouken@5221
   265
}
slouken@5221
   266
#endif /* __ANDROID__ */
slouken@5221
   267
slouken@5221
   268
void
slouken@5221
   269
SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
slouken@5221
   270
{
slouken@5223
   271
    char *message;
icculus@7344
   272
    size_t len;
slouken@5221
   273
slouken@5235
   274
    /* Nothing to do if we don't have an output function */
slouken@5235
   275
    if (!SDL_log_function) {
slouken@5235
   276
        return;
slouken@5235
   277
    }
slouken@5235
   278
slouken@5221
   279
    /* Make sure we don't exceed array bounds */
slouken@6040
   280
    if ((int)priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
slouken@5221
   281
        return;
slouken@5221
   282
    }
slouken@5221
   283
slouken@5221
   284
    /* See if we want to do anything with this message */
slouken@5221
   285
    if (priority < SDL_LogGetPriority(category)) {
slouken@5221
   286
        return;
slouken@5221
   287
    }
slouken@5221
   288
slouken@5223
   289
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@5223
   290
    if (!message) {
slouken@5223
   291
        return;
slouken@5223
   292
    }
icculus@7344
   293
slouken@5223
   294
    SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
icculus@7344
   295
icculus@7344
   296
    /* Chop off final endline. */
icculus@7344
   297
    len = SDL_strlen(message);
icculus@7344
   298
    if ((len > 0) && (message[len-1] == '\n')) {
icculus@7344
   299
        message[--len] = '\0';
icculus@7344
   300
        if ((len > 0) && (message[len-1] == '\r')) {  /* catch "\r\n", too. */
icculus@7344
   301
            message[--len] = '\0';
icculus@7344
   302
        }
icculus@7344
   303
    }
icculus@7344
   304
slouken@5235
   305
    SDL_log_function(SDL_log_userdata, category, priority, message);
slouken@5235
   306
    SDL_stack_free(message);
slouken@5235
   307
}
slouken@5221
   308
aschiffler@7274
   309
#if defined(__WIN32__)
aschiffler@7274
   310
/* Flag tracking the attachment of the console: 0=unattached, 1=attached, -1=error */
aschiffler@7274
   311
static int consoleAttached = 0;
aschiffler@7274
   312
aschiffler@7274
   313
/* Handle to stderr output of console. */
aschiffler@7274
   314
static HANDLE stderrHandle = NULL;
aschiffler@7274
   315
#endif
aschiffler@7274
   316
slouken@5235
   317
static void
slouken@5235
   318
SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
slouken@5235
   319
              const char *message)
slouken@5235
   320
{
slouken@5221
   321
#if defined(__WIN32__)
slouken@5223
   322
    /* Way too many allocations here, urgh */
aschiffler@7614
   323
    /* Note: One can't call SDL_SetError here, since that function itself logs. */
slouken@5221
   324
    {
slouken@5223
   325
        char *output;
slouken@5223
   326
        size_t length;
slouken@5221
   327
        LPTSTR tstr;
aschiffler@7274
   328
        BOOL attachResult;
aschiffler@7274
   329
        DWORD attachError;
aschiffler@7274
   330
        unsigned long charsWritten; 
aschiffler@7274
   331
aschiffler@7274
   332
        /* Maybe attach console and get stderr handle */
aschiffler@7274
   333
        if (consoleAttached == 0) {
aschiffler@7274
   334
            attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
aschiffler@7274
   335
            if (!attachResult) {
aschiffler@7274
   336
                    attachError = GetLastError();
aschiffler@7274
   337
                    if (attachError == ERROR_INVALID_HANDLE) {
icculus@7710
   338
                        OutputDebugString(TEXT("Parent process has no console\r\n"));
aschiffler@7274
   339
                        consoleAttached = -1;
aschiffler@7274
   340
                    } else if (attachError == ERROR_GEN_FAILURE) {
icculus@7710
   341
                         OutputDebugString(TEXT("Could not attach to console of parent process\r\n"));
aschiffler@7274
   342
                         consoleAttached = -1;
aschiffler@7274
   343
                    } else if (attachError == ERROR_ACCESS_DENIED) {  
aschiffler@7274
   344
                         /* Already attached */
aschiffler@7274
   345
                        consoleAttached = 1;
aschiffler@7274
   346
                    } else {
icculus@7710
   347
                        OutputDebugString(TEXT("Error attaching console\r\n"));
aschiffler@7274
   348
                        consoleAttached = -1;
aschiffler@7274
   349
                    }
aschiffler@7274
   350
                } else {
aschiffler@7274
   351
                    /* Newly attached */
aschiffler@7274
   352
                    consoleAttached = 1;
aschiffler@7274
   353
                }
aschiffler@7274
   354
			
aschiffler@7274
   355
                if (consoleAttached == 1) {
aschiffler@7274
   356
                        stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
aschiffler@7274
   357
                }
aschiffler@7274
   358
        }
slouken@5221
   359
icculus@7710
   360
        length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1;
slouken@5223
   361
        output = SDL_stack_alloc(char, length);
icculus@7710
   362
        SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message);
slouken@5221
   363
        tstr = WIN_UTF8ToString(output);
aschiffler@7274
   364
        
aschiffler@7292
   365
        /* Output to debugger */
aschiffler@7292
   366
        OutputDebugString(tstr);
aschiffler@7274
   367
       
aschiffler@7274
   368
        /* Screen output to stderr, if console was attached. */
aschiffler@7274
   369
        if (consoleAttached == 1) {
aschiffler@7274
   370
                if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) {
icculus@7710
   371
                    OutputDebugString(TEXT("Error calling WriteConsole\r\n"));
aschiffler@7274
   372
                }
aschiffler@7274
   373
                if (charsWritten == ERROR_NOT_ENOUGH_MEMORY) {
icculus@7710
   374
                    OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
aschiffler@7274
   375
                }
aschiffler@7274
   376
        }
aschiffler@7274
   377
slouken@5221
   378
        SDL_free(tstr);
slouken@5223
   379
        SDL_stack_free(output);
slouken@5221
   380
    }
slouken@5221
   381
#elif defined(__ANDROID__)
slouken@5221
   382
    {
slouken@5221
   383
        char tag[32];
slouken@5221
   384
slouken@5221
   385
        SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
slouken@5221
   386
        __android_log_write(SDL_android_priority[priority], tag, message);
slouken@5221
   387
    }
slouken@7307
   388
#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
slouken@7307
   389
    /* Technically we don't need SDL_VIDEO_DRIVER_COCOA, but that's where this function is defined for now.
slouken@7307
   390
    */
slouken@6616
   391
    extern void SDL_NSLog(const char *text);
slouken@6616
   392
    {
slouken@6616
   393
        char *text;
slouken@6616
   394
slouken@6616
   395
        text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@6616
   396
        if (text) {
slouken@6616
   397
            SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message);
slouken@6616
   398
            SDL_NSLog(text);
slouken@6616
   399
            SDL_stack_free(text);
slouken@6616
   400
            return;
slouken@6616
   401
        }
slouken@6616
   402
    }
kimonline@7009
   403
#elif defined(__PSP__)
slouken@7191
   404
    {
slouken@7191
   405
        FILE*        pFile;
slouken@7191
   406
        pFile = fopen ("SDL_Log.txt", "a");
slouken@7350
   407
        fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@7191
   408
        fclose (pFile);
slouken@7191
   409
    }
slouken@5221
   410
#endif
slouken@5221
   411
#if HAVE_STDIO_H
slouken@5221
   412
    fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@5221
   413
#endif
slouken@5235
   414
}
slouken@5235
   415
slouken@5235
   416
void
slouken@5235
   417
SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
slouken@5235
   418
{
slouken@5235
   419
    if (callback) {
slouken@5235
   420
        *callback = SDL_log_function;
slouken@5235
   421
    }
slouken@5235
   422
    if (userdata) {
slouken@5235
   423
        *userdata = SDL_log_userdata;
slouken@5235
   424
    }
slouken@5235
   425
}
slouken@5235
   426
slouken@5235
   427
void
slouken@5235
   428
SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
slouken@5235
   429
{
slouken@5235
   430
    SDL_log_function = callback;
slouken@5235
   431
    SDL_log_userdata = userdata;
slouken@5221
   432
}
slouken@5221
   433
slouken@5221
   434
/* vi: set ts=4 sw=4 expandtab: */