src/SDL_log.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 14 Jul 2013 18:17:28 -0700
changeset 7523 9e9ab1dc3811
parent 7350 302af2a46a66
child 7614 af0bd07212bd
permissions -rw-r--r--
Fixed bug 1919 - Window icon disappears as soon as a renderer is created

Sebastian

Setting a window icon works just fine until a renderer is added to the window.
After adding the renderer the icon disappears.

Reproduce by:
- Take the example code from the wiki: http://wiki.libsdl.org/moin.fcg/SDL_SetWindowIcon

- Add the following two lines after SDL_FreeSurface(surface);
SDL_Delay(1000);
SDL_Renderer* ren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

-compile and run

You will see the window icon correctly at first. After the Delay the Icon will disappear.
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;
icculus@7344
   269
    size_t len;
slouken@5221
   270
slouken@5235
   271
    /* Nothing to do if we don't have an output function */
slouken@5235
   272
    if (!SDL_log_function) {
slouken@5235
   273
        return;
slouken@5235
   274
    }
slouken@5235
   275
slouken@5221
   276
    /* Make sure we don't exceed array bounds */
slouken@6040
   277
    if ((int)priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
slouken@5221
   278
        return;
slouken@5221
   279
    }
slouken@5221
   280
slouken@5221
   281
    /* See if we want to do anything with this message */
slouken@5221
   282
    if (priority < SDL_LogGetPriority(category)) {
slouken@5221
   283
        return;
slouken@5221
   284
    }
slouken@5221
   285
slouken@5223
   286
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@5223
   287
    if (!message) {
slouken@5223
   288
        return;
slouken@5223
   289
    }
icculus@7344
   290
slouken@5223
   291
    SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
icculus@7344
   292
icculus@7344
   293
    /* Chop off final endline. */
icculus@7344
   294
    len = SDL_strlen(message);
icculus@7344
   295
    if ((len > 0) && (message[len-1] == '\n')) {
icculus@7344
   296
        message[--len] = '\0';
icculus@7344
   297
        if ((len > 0) && (message[len-1] == '\r')) {  /* catch "\r\n", too. */
icculus@7344
   298
            message[--len] = '\0';
icculus@7344
   299
        }
icculus@7344
   300
    }
icculus@7344
   301
slouken@5235
   302
    SDL_log_function(SDL_log_userdata, category, priority, message);
slouken@5235
   303
    SDL_stack_free(message);
slouken@5235
   304
}
slouken@5221
   305
aschiffler@7274
   306
#if defined(__WIN32__)
aschiffler@7274
   307
/* Flag tracking the attachment of the console: 0=unattached, 1=attached, -1=error */
aschiffler@7274
   308
static int consoleAttached = 0;
aschiffler@7274
   309
aschiffler@7274
   310
/* Handle to stderr output of console. */
aschiffler@7274
   311
static HANDLE stderrHandle = NULL;
aschiffler@7274
   312
#endif
aschiffler@7274
   313
slouken@5235
   314
static void
slouken@5235
   315
SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
slouken@5235
   316
              const char *message)
slouken@5235
   317
{
slouken@5221
   318
#if defined(__WIN32__)
slouken@5223
   319
    /* Way too many allocations here, urgh */
slouken@5221
   320
    {
slouken@5223
   321
        char *output;
slouken@5223
   322
        size_t length;
slouken@5221
   323
        LPTSTR tstr;
aschiffler@7274
   324
        BOOL attachResult;
aschiffler@7274
   325
        DWORD attachError;
aschiffler@7274
   326
        unsigned long charsWritten; 
aschiffler@7274
   327
aschiffler@7274
   328
        /* Maybe attach console and get stderr handle */
aschiffler@7274
   329
        if (consoleAttached == 0) {
aschiffler@7274
   330
            attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
aschiffler@7274
   331
            if (!attachResult) {
aschiffler@7274
   332
                    attachError = GetLastError();
aschiffler@7274
   333
                    if (attachError == ERROR_INVALID_HANDLE) {
aschiffler@7274
   334
                        SDL_SetError("Parent process has no console");
aschiffler@7274
   335
                        consoleAttached = -1;
aschiffler@7274
   336
                    } else if (attachError == ERROR_GEN_FAILURE) {
aschiffler@7274
   337
                         SDL_SetError("Could not attach to console of parent process");
aschiffler@7274
   338
                         consoleAttached = -1;
aschiffler@7274
   339
                    } else if (attachError == ERROR_ACCESS_DENIED) {  
aschiffler@7274
   340
                         /* Already attached */
aschiffler@7274
   341
                        consoleAttached = 1;
aschiffler@7274
   342
                    } else {
aschiffler@7274
   343
                        SDL_SetError("Error %d attaching console", attachError);
aschiffler@7274
   344
                        consoleAttached = -1;
aschiffler@7274
   345
                    }
aschiffler@7274
   346
                } else {
aschiffler@7274
   347
                    /* Newly attached */
aschiffler@7274
   348
                    consoleAttached = 1;
aschiffler@7274
   349
                }
aschiffler@7274
   350
			
aschiffler@7274
   351
                if (consoleAttached == 1) {
aschiffler@7274
   352
                        stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
aschiffler@7274
   353
                }
aschiffler@7274
   354
        }
slouken@5221
   355
slouken@7215
   356
        length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1;
slouken@5223
   357
        output = SDL_stack_alloc(char, length);
slouken@7215
   358
        SDL_snprintf(output, length, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@5221
   359
        tstr = WIN_UTF8ToString(output);
aschiffler@7274
   360
        
aschiffler@7292
   361
        /* Output to debugger */
aschiffler@7292
   362
        OutputDebugString(tstr);
aschiffler@7274
   363
       
aschiffler@7274
   364
        /* Screen output to stderr, if console was attached. */
aschiffler@7274
   365
        if (consoleAttached == 1) {
aschiffler@7274
   366
                if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) {
aschiffler@7274
   367
                    SDL_SetError("Error %d calling WriteConsole", GetLastError());
aschiffler@7274
   368
                }
aschiffler@7274
   369
                if (charsWritten == ERROR_NOT_ENOUGH_MEMORY) {
aschiffler@7274
   370
                    SDL_SetError("Insufficient heap memory to write message of size %d", length);
aschiffler@7274
   371
                }
aschiffler@7274
   372
        }
aschiffler@7274
   373
slouken@5221
   374
        SDL_free(tstr);
slouken@5223
   375
        SDL_stack_free(output);
slouken@5221
   376
    }
slouken@5221
   377
#elif defined(__ANDROID__)
slouken@5221
   378
    {
slouken@5221
   379
        char tag[32];
slouken@5221
   380
slouken@5221
   381
        SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
slouken@5221
   382
        __android_log_write(SDL_android_priority[priority], tag, message);
slouken@5221
   383
    }
slouken@7307
   384
#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
slouken@7307
   385
    /* Technically we don't need SDL_VIDEO_DRIVER_COCOA, but that's where this function is defined for now.
slouken@7307
   386
    */
slouken@6616
   387
    extern void SDL_NSLog(const char *text);
slouken@6616
   388
    {
slouken@6616
   389
        char *text;
slouken@6616
   390
slouken@6616
   391
        text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@6616
   392
        if (text) {
slouken@6616
   393
            SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message);
slouken@6616
   394
            SDL_NSLog(text);
slouken@6616
   395
            SDL_stack_free(text);
slouken@6616
   396
            return;
slouken@6616
   397
        }
slouken@6616
   398
    }
kimonline@7009
   399
#elif defined(__PSP__)
slouken@7191
   400
    {
slouken@7191
   401
        FILE*        pFile;
slouken@7191
   402
        pFile = fopen ("SDL_Log.txt", "a");
slouken@7350
   403
        fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@7191
   404
        fclose (pFile);
slouken@7191
   405
    }
slouken@5221
   406
#endif
slouken@5221
   407
#if HAVE_STDIO_H
slouken@5221
   408
    fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
slouken@5221
   409
#endif
slouken@5235
   410
}
slouken@5235
   411
slouken@5235
   412
void
slouken@5235
   413
SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
slouken@5235
   414
{
slouken@5235
   415
    if (callback) {
slouken@5235
   416
        *callback = SDL_log_function;
slouken@5235
   417
    }
slouken@5235
   418
    if (userdata) {
slouken@5235
   419
        *userdata = SDL_log_userdata;
slouken@5235
   420
    }
slouken@5235
   421
}
slouken@5235
   422
slouken@5235
   423
void
slouken@5235
   424
SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
slouken@5235
   425
{
slouken@5235
   426
    SDL_log_function = callback;
slouken@5235
   427
    SDL_log_userdata = userdata;
slouken@5221
   428
}
slouken@5221
   429
slouken@5221
   430
/* vi: set ts=4 sw=4 expandtab: */