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