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