src/SDL_log.c
changeset 5221 2ee8112bfc6b
child 5223 ab0d7cecc0f6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL_log.c	Mon Feb 07 16:45:40 2011 -0800
     1.3 @@ -0,0 +1,273 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/* Simple log messages in SDL */
    1.28 +
    1.29 +#include "SDL_log.h"
    1.30 +
    1.31 +#if HAVE_STDIO_H
    1.32 +#include <stdio.h>
    1.33 +#endif
    1.34 +
    1.35 +#if defined(__WIN32__)
    1.36 +#include "core/windows/SDL_windows.h"
    1.37 +#elif defined(__ANDROID__)
    1.38 +#include <android/log.h>
    1.39 +#endif
    1.40 +
    1.41 +#define DEFAULT_PRIORITY                SDL_LOG_PRIORITY_CRITICAL
    1.42 +#define DEFAULT_APPLICATION_PRIORITY    SDL_LOG_PRIORITY_INFO
    1.43 +
    1.44 +typedef struct SDL_LogLevel
    1.45 +{
    1.46 +    int category;
    1.47 +    SDL_LogPriority priority;
    1.48 +    struct SDL_LogLevel *next;
    1.49 +} SDL_LogLevel;
    1.50 +
    1.51 +static SDL_LogLevel *SDL_loglevels;
    1.52 +static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
    1.53 +static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
    1.54 +
    1.55 +static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
    1.56 +    "VERBOSE",
    1.57 +    "DEBUG",
    1.58 +    "INFO",
    1.59 +    "WARN",
    1.60 +    "ERROR",
    1.61 +    "CRITICAL"
    1.62 +};
    1.63 +
    1.64 +#ifdef __ANDROID__
    1.65 +static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = {
    1.66 +    "APP",
    1.67 +    "ERROR",
    1.68 +    "SYSTEM",
    1.69 +    "AUDIO",
    1.70 +    "VIDEO",
    1.71 +    "RENDER",
    1.72 +    "INPUT"
    1.73 +};
    1.74 +
    1.75 +static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = {
    1.76 +    ANDROID_LOG_VERBOSE,
    1.77 +    ANDROID_LOG_DEBUG,
    1.78 +    ANDROID_LOG_INFO,
    1.79 +    ANDROID_LOG_WARN,
    1.80 +    ANDROID_LOG_ERROR,
    1.81 +    ANDROID_LOG_FATAL
    1.82 +};
    1.83 +#endif /* __ANDROID__ */
    1.84 +
    1.85 +
    1.86 +void
    1.87 +SDL_LogSetAllPriority(SDL_LogPriority priority)
    1.88 +{
    1.89 +    SDL_LogLevel *entry;
    1.90 +
    1.91 +    for (entry = SDL_loglevels; entry; entry = entry->next) {
    1.92 +        entry->priority = priority;
    1.93 +    }
    1.94 +    SDL_application_priority = SDL_default_priority = priority;
    1.95 +}
    1.96 +
    1.97 +void
    1.98 +SDL_LogSetPriority(int category, SDL_LogPriority priority)
    1.99 +{
   1.100 +    SDL_LogLevel *entry;
   1.101 +
   1.102 +    for (entry = SDL_loglevels; entry; entry = entry->next) {
   1.103 +        if (entry->category == category) {
   1.104 +            entry->priority = priority;
   1.105 +            return;
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    /* Create a new entry */
   1.110 +    entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry));
   1.111 +    if (entry) {
   1.112 +        entry->category = category;
   1.113 +        entry->priority = priority;
   1.114 +        entry->next = SDL_loglevels;
   1.115 +        SDL_loglevels = entry;
   1.116 +    }
   1.117 +}
   1.118 +
   1.119 +SDL_LogPriority
   1.120 +SDL_LogGetPriority(int category)
   1.121 +{
   1.122 +    SDL_LogLevel *entry;
   1.123 +
   1.124 +    for (entry = SDL_loglevels; entry; entry = entry->next) {
   1.125 +        if (entry->category == category) {
   1.126 +            return entry->priority;
   1.127 +        }
   1.128 +    }
   1.129 +
   1.130 +    if (category == SDL_LOG_CATEGORY_APPLICATION) {
   1.131 +        return SDL_application_priority;
   1.132 +    } else {
   1.133 +        return SDL_default_priority;
   1.134 +    }
   1.135 +}
   1.136 +
   1.137 +void
   1.138 +SDL_LogResetPriorities(void)
   1.139 +{
   1.140 +    SDL_LogLevel *entry;
   1.141 +
   1.142 +    while (SDL_loglevels) {
   1.143 +        entry = SDL_loglevels;
   1.144 +        SDL_loglevels = entry->next;
   1.145 +        SDL_free(entry);
   1.146 +    }
   1.147 +
   1.148 +    SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
   1.149 +    SDL_default_priority = DEFAULT_PRIORITY;
   1.150 +}
   1.151 +
   1.152 +void
   1.153 +SDL_Log(const char *fmt, ...)
   1.154 +{
   1.155 +    va_list ap;
   1.156 +
   1.157 +    va_start(ap, fmt);
   1.158 +    SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
   1.159 +    va_end(ap);
   1.160 +}
   1.161 +
   1.162 +void
   1.163 +SDL_LogVerbose(int category, const char *fmt, ...)
   1.164 +{
   1.165 +    va_list ap;
   1.166 +
   1.167 +    va_start(ap, fmt);
   1.168 +    SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap);
   1.169 +    va_end(ap);
   1.170 +}
   1.171 +
   1.172 +void
   1.173 +SDL_LogInfo(int category, const char *fmt, ...)
   1.174 +{
   1.175 +    va_list ap;
   1.176 +
   1.177 +    va_start(ap, fmt);
   1.178 +    SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap);
   1.179 +    va_end(ap);
   1.180 +}
   1.181 +
   1.182 +void
   1.183 +SDL_LogWarn(int category, const char *fmt, ...)
   1.184 +{
   1.185 +    va_list ap;
   1.186 +
   1.187 +    va_start(ap, fmt);
   1.188 +    SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap);
   1.189 +    va_end(ap);
   1.190 +}
   1.191 +
   1.192 +void
   1.193 +SDL_LogError(int category, const char *fmt, ...)
   1.194 +{
   1.195 +    va_list ap;
   1.196 +
   1.197 +    va_start(ap, fmt);
   1.198 +    SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap);
   1.199 +    va_end(ap);
   1.200 +}
   1.201 +
   1.202 +void
   1.203 +SDL_LogCritical(int category, const char *fmt, ...)
   1.204 +{
   1.205 +    va_list ap;
   1.206 +
   1.207 +    va_start(ap, fmt);
   1.208 +    SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap);
   1.209 +    va_end(ap);
   1.210 +}
   1.211 +
   1.212 +void
   1.213 +SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...)
   1.214 +{
   1.215 +    va_list ap;
   1.216 +
   1.217 +    va_start(ap, fmt);
   1.218 +    SDL_LogMessageV(category, priority, fmt, ap);
   1.219 +    va_end(ap);
   1.220 +}
   1.221 +
   1.222 +#ifdef __ANDROID__
   1.223 +static const char *
   1.224 +GetCategoryPrefix(int category)
   1.225 +{
   1.226 +    if (category < SDL_LOG_CATEGORY_RESERVED1) {
   1.227 +        return SDL_category_prefixes[category];
   1.228 +    }
   1.229 +    if (category < SDL_LOG_CATEGORY_CUSTOM) {
   1.230 +        return "RESERVED";
   1.231 +    }
   1.232 +    return "CUSTOM";
   1.233 +}
   1.234 +#endif /* __ANDROID__ */
   1.235 +
   1.236 +void
   1.237 +SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
   1.238 +{
   1.239 +    char message[SDL_MAX_LOG_MESSAGE];
   1.240 +
   1.241 +    /* Make sure we don't exceed array bounds */
   1.242 +    if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
   1.243 +        return;
   1.244 +    }
   1.245 +
   1.246 +    /* See if we want to do anything with this message */
   1.247 +    if (priority < SDL_LogGetPriority(category)) {
   1.248 +        return;
   1.249 +    }
   1.250 +
   1.251 +    SDL_vsnprintf(message, SDL_arraysize(message), fmt, ap);
   1.252 +
   1.253 +#if defined(__WIN32__)
   1.254 +    {
   1.255 +        char output[32+SDL_MAX_LOG_MESSAGE];
   1.256 +        LPTSTR tstr;
   1.257 +
   1.258 +        SDL_snprintf(output, SDL_arraysize(output), "%s: %s", SDL_priority_prefixes[priority], message);
   1.259 +        tstr = WIN_UTF8ToString(output);
   1.260 +        OutputDebugString(tstr);
   1.261 +        SDL_free(tstr);
   1.262 +    }
   1.263 +#elif defined(__ANDROID__)
   1.264 +    {
   1.265 +        char tag[32];
   1.266 +
   1.267 +        SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
   1.268 +        __android_log_write(SDL_android_priority[priority], tag, message);
   1.269 +    }
   1.270 +#endif
   1.271 +#if HAVE_STDIO_H
   1.272 +    fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
   1.273 +#endif
   1.274 +}
   1.275 +
   1.276 +/* vi: set ts=4 sw=4 expandtab: */