From 564ad838f5b3078d5dbcc6b9ab95f94761c16b32 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 7 Feb 2011 16:45:40 -0800 Subject: [PATCH] Added a simple log message API --- Makefile.in | 1 + VisualC/SDL/SDL_VS2008.vcproj | 8 + VisualC/SDL/SDL_VS2010.vcxproj | 2 + VisualCE/SDL/SDL.vcproj | 42 ++- .../SDL/SDLiPhoneOS.xcodeproj/project.pbxproj | 12 + Xcode/SDL/SDL.xcodeproj/project.pbxproj | 12 + include/SDL.h | 1 + include/SDL_error.h | 2 +- include/SDL_log.h | 191 ++++++++++++ src/SDL.c | 21 +- src/SDL_error.c | 13 +- src/SDL_log.c | 273 ++++++++++++++++++ src/render/SDL_render.c | 4 + 13 files changed, 549 insertions(+), 33 deletions(-) create mode 100644 include/SDL_log.h create mode 100755 src/SDL_log.c diff --git a/Makefile.in b/Makefile.in index 569b64b3e..398e5f837 100644 --- a/Makefile.in +++ b/Makefile.in @@ -59,6 +59,7 @@ HDRS = \ SDL_keyboard.h \ SDL_keysym.h \ SDL_loadso.h \ + SDL_log.h \ SDL_main.h \ SDL_mouse.h \ SDL_mutex.h \ diff --git a/VisualC/SDL/SDL_VS2008.vcproj b/VisualC/SDL/SDL_VS2008.vcproj index 44799bfa7..98084683f 100644 --- a/VisualC/SDL/SDL_VS2008.vcproj +++ b/VisualC/SDL/SDL_VS2008.vcproj @@ -457,6 +457,10 @@ RelativePath="..\..\include\SDL_loadso.h" > + + @@ -918,6 +922,10 @@ RelativePath="..\..\src\video\SDL_leaks.h" > + + diff --git a/VisualC/SDL/SDL_VS2010.vcxproj b/VisualC/SDL/SDL_VS2010.vcxproj index 390c45e81..c964c4d5e 100644 --- a/VisualC/SDL/SDL_VS2010.vcxproj +++ b/VisualC/SDL/SDL_VS2010.vcxproj @@ -249,6 +249,7 @@ echo #define SDL_REVISION "hg-0:baadf00d" >"$(ProjectDir)\..\..\include\SDL_r + @@ -390,6 +391,7 @@ echo #define SDL_REVISION "hg-0:baadf00d" >"$(ProjectDir)\..\..\include\SDL_r + diff --git a/VisualCE/SDL/SDL.vcproj b/VisualCE/SDL/SDL.vcproj index 49fabbd30..dec165349 100644 --- a/VisualCE/SDL/SDL.vcproj +++ b/VisualCE/SDL/SDL.vcproj @@ -877,6 +877,10 @@ RelativePath="..\..\src\haptic\SDL_haptic.c" > + + @@ -889,6 +893,10 @@ RelativePath="..\..\src\events\SDL_keyboard.c" > + + @@ -938,7 +946,7 @@ > + + + + + + + + + + + + + + @@ -1270,6 +1306,10 @@ RelativePath="..\..\src\video\SDL_rect_c.h" > + + diff --git a/Xcode-iPhoneOS/SDL/SDLiPhoneOS.xcodeproj/project.pbxproj b/Xcode-iPhoneOS/SDL/SDLiPhoneOS.xcodeproj/project.pbxproj index e55c67d46..57623d94f 100755 --- a/Xcode-iPhoneOS/SDL/SDLiPhoneOS.xcodeproj/project.pbxproj +++ b/Xcode-iPhoneOS/SDL/SDLiPhoneOS.xcodeproj/project.pbxproj @@ -99,6 +99,9 @@ 04BA9D6611EF474A00B60E01 /* SDL_touch.c in Sources */ = {isa = PBXBuildFile; fileRef = 04BA9D6211EF474A00B60E01 /* SDL_touch.c */; }; 04BA9D7D11EF497E00B60E01 /* SDL_gesture.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BA9D7B11EF497E00B60E01 /* SDL_gesture.h */; settings = {ATTRIBUTES = (Public, ); }; }; 04BA9D7E11EF497E00B60E01 /* SDL_touch.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BA9D7C11EF497E00B60E01 /* SDL_touch.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 04BAC0991300C0F70055DE28 /* SDL_log.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BAC0981300C0F70055DE28 /* SDL_log.h */; }; + 04BAC09C1300C1290055DE28 /* SDL_assert_c.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BAC09A1300C1290055DE28 /* SDL_assert_c.h */; }; + 04BAC09D1300C1290055DE28 /* SDL_log.c in Sources */ = {isa = PBXBuildFile; fileRef = 04BAC09B1300C1290055DE28 /* SDL_log.c */; }; 04EC8B521025D12900431D42 /* SDL_config_iphoneos.h in Headers */ = {isa = PBXBuildFile; fileRef = 04EC8B501025D12900431D42 /* SDL_config_iphoneos.h */; settings = {ATTRIBUTES = (Public, ); }; }; 04F2AF541104ABC300D6DDF7 /* SDL_assert.h in Headers */ = {isa = PBXBuildFile; fileRef = 04F2AF531104ABC300D6DDF7 /* SDL_assert.h */; settings = {ATTRIBUTES = (Public, ); }; }; 04F2AF561104ABD200D6DDF7 /* SDL_assert.c in Sources */ = {isa = PBXBuildFile; fileRef = 04F2AF551104ABD200D6DDF7 /* SDL_assert.c */; }; @@ -362,6 +365,9 @@ 04BA9D6211EF474A00B60E01 /* SDL_touch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_touch.c; sourceTree = ""; }; 04BA9D7B11EF497E00B60E01 /* SDL_gesture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_gesture.h; path = ../../include/SDL_gesture.h; sourceTree = SOURCE_ROOT; }; 04BA9D7C11EF497E00B60E01 /* SDL_touch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_touch.h; path = ../../include/SDL_touch.h; sourceTree = SOURCE_ROOT; }; + 04BAC0981300C0F70055DE28 /* SDL_log.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_log.h; path = ../../include/SDL_log.h; sourceTree = SOURCE_ROOT; }; + 04BAC09A1300C1290055DE28 /* SDL_assert_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_assert_c.h; path = ../../src/SDL_assert_c.h; sourceTree = SOURCE_ROOT; }; + 04BAC09B1300C1290055DE28 /* SDL_log.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDL_log.c; path = ../../src/SDL_log.c; sourceTree = SOURCE_ROOT; }; 04EC8B501025D12900431D42 /* SDL_config_iphoneos.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_config_iphoneos.h; path = ../../include/SDL_config_iphoneos.h; sourceTree = SOURCE_ROOT; }; 04F2AF531104ABC300D6DDF7 /* SDL_assert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_assert.h; path = ../../include/SDL_assert.h; sourceTree = SOURCE_ROOT; }; 04F2AF551104ABD200D6DDF7 /* SDL_assert.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDL_assert.c; path = ../../src/SDL_assert.c; sourceTree = SOURCE_ROOT; }; @@ -930,6 +936,7 @@ FD99B8DF0DD52EB400FB1D6B /* SDL_keyboard.h */, FD99B8E00DD52EB400FB1D6B /* SDL_keysym.h */, FD99B8E10DD52EB400FB1D6B /* SDL_loadso.h */, + 04BAC0981300C0F70055DE28 /* SDL_log.h */, FD99B8E20DD52EB400FB1D6B /* SDL_main.h */, FD99B8E30DD52EB400FB1D6B /* SDL_mouse.h */, FD99B8E40DD52EB400FB1D6B /* SDL_mutex.h */, @@ -978,12 +985,14 @@ FD99BA1E0DD52EDC00FB1D6B /* timer */, FDA682420DF2374D00F98A1A /* video */, 04F2AF551104ABD200D6DDF7 /* SDL_assert.c */, + 04BAC09A1300C1290055DE28 /* SDL_assert_c.h */, FD99B9D30DD52EDC00FB1D6B /* SDL_compat.c */, FD99B9D40DD52EDC00FB1D6B /* SDL_error_c.h */, FD99B9D50DD52EDC00FB1D6B /* SDL_error.c */, FD99B9D60DD52EDC00FB1D6B /* SDL_fatal.c */, FD99B9D70DD52EDC00FB1D6B /* SDL_fatal.h */, 0442EC5412FE1C3F004C9285 /* SDL_hints.c */, + 04BAC09B1300C1290055DE28 /* SDL_log.c */, FD99B9D80DD52EDC00FB1D6B /* SDL.c */, ); name = "Library Source"; @@ -1260,6 +1269,8 @@ 0442EC5012FE1C1E004C9285 /* SDL_render_sw_c.h in Headers */, 0402A85A12FE70C600CECEE3 /* SDL_shaders_gles2.h in Headers */, 04043B8D12FEA9350076DB1F /* SDL_opengles2.h in Headers */, + 04BAC0991300C0F70055DE28 /* SDL_log.h in Headers */, + 04BAC09C1300C1290055DE28 /* SDL_assert_c.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1509,6 +1520,7 @@ 0442EC5512FE1C3F004C9285 /* SDL_hints.c in Sources */, 0402A85812FE70C600CECEE3 /* SDL_render_gles2.c in Sources */, 0402A85912FE70C600CECEE3 /* SDL_shaders_gles2.c in Sources */, + 04BAC09D1300C1290055DE28 /* SDL_log.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj index 029ee3d25..5272e06c9 100755 --- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj +++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj @@ -155,6 +155,10 @@ 044E5F8611E6051C0076F181 /* SDL_clipboard.h in Headers */ = {isa = PBXBuildFile; fileRef = 044E5F8411E6051C0076F181 /* SDL_clipboard.h */; }; 0469A10B12EE4BF100B846D6 /* SDL_blendmode.h in Headers */ = {isa = PBXBuildFile; fileRef = 0469A10912EE4BF100B846D6 /* SDL_blendmode.h */; settings = {ATTRIBUTES = (Public, ); }; }; 0469A10D12EE4BF100B846D6 /* SDL_blendmode.h in Headers */ = {isa = PBXBuildFile; fileRef = 0469A10912EE4BF100B846D6 /* SDL_blendmode.h */; }; + 04BAC0B31300C1CB0055DE28 /* SDL_log.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BAC0B21300C1CB0055DE28 /* SDL_log.h */; }; + 04BAC0B41300C1CB0055DE28 /* SDL_log.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BAC0B21300C1CB0055DE28 /* SDL_log.h */; }; + 04BAC0C81300C2160055DE28 /* SDL_log.c in Sources */ = {isa = PBXBuildFile; fileRef = 04BAC0C71300C2160055DE28 /* SDL_log.c */; }; + 04BAC0C91300C2160055DE28 /* SDL_log.c in Sources */ = {isa = PBXBuildFile; fileRef = 04BAC0C71300C2160055DE28 /* SDL_log.c */; }; 04BD000812E6671800899322 /* SDL_diskaudio.c in Sources */ = {isa = PBXBuildFile; fileRef = 04BDFD8812E6671700899322 /* SDL_diskaudio.c */; }; 04BD000912E6671800899322 /* SDL_diskaudio.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BDFD8912E6671700899322 /* SDL_diskaudio.h */; }; 04BD001012E6671800899322 /* SDL_dummyaudio.c in Sources */ = {isa = PBXBuildFile; fileRef = 04BDFD9412E6671700899322 /* SDL_dummyaudio.c */; }; @@ -709,6 +713,8 @@ 0442EC5E12FE1C75004C9285 /* SDL_hints.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDL_hints.c; path = ../../src/SDL_hints.c; sourceTree = SOURCE_ROOT; }; 044E5F8411E6051C0076F181 /* SDL_clipboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_clipboard.h; path = ../../include/SDL_clipboard.h; sourceTree = SOURCE_ROOT; }; 0469A10912EE4BF100B846D6 /* SDL_blendmode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_blendmode.h; path = ../../include/SDL_blendmode.h; sourceTree = SOURCE_ROOT; }; + 04BAC0B21300C1CB0055DE28 /* SDL_log.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_log.h; path = ../../include/SDL_log.h; sourceTree = SOURCE_ROOT; }; + 04BAC0C71300C2160055DE28 /* SDL_log.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDL_log.c; path = ../../src/SDL_log.c; sourceTree = SOURCE_ROOT; }; 04BDFD7412E6671700899322 /* SDL_atomic.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_atomic.c; sourceTree = ""; }; 04BDFD7512E6671700899322 /* SDL_spinlock.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_spinlock.c; sourceTree = ""; }; 04BDFD8812E6671700899322 /* SDL_diskaudio.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_diskaudio.c; sourceTree = ""; }; @@ -1137,6 +1143,7 @@ 0C5AF5F101191D2B7F000001 /* SDL_keyboard.h */, 0C5AF5F201191D2B7F000001 /* SDL_keysym.h */, B29A290D04E5B28700A80002 /* SDL_loadso.h */, + 04BAC0B21300C1CB0055DE28 /* SDL_log.h */, 0C5AF5F301191D2B7F000001 /* SDL_main.h */, 0C5AF5F401191D2B7F000001 /* SDL_mouse.h */, 0C5AF5F501191D2B7F000001 /* SDL_mutex.h */, @@ -1722,6 +1729,7 @@ 04BDFE5A12E6671700899322 /* SDL_fatal.c */, 04BDFE5B12E6671700899322 /* SDL_fatal.h */, 0442EC5E12FE1C75004C9285 /* SDL_hints.c */, + 04BAC0C71300C2160055DE28 /* SDL_log.c */, 04BDFE5C12E6671700899322 /* SDL.c */, ); name = "Library Source"; @@ -1932,6 +1940,7 @@ 0442EC1C12FE1BCB004C9285 /* SDL_render_sw_c.h in Headers */, 0442EC5B12FE1C60004C9285 /* SDL_x11framebuffer.h in Headers */, 04043BBB12FEB1BE0076DB1F /* SDL_glfuncs.h in Headers */, + 04BAC0B31300C1CB0055DE28 /* SDL_log.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2061,6 +2070,7 @@ 0442EC1E12FE1BCB004C9285 /* SDL_render_sw_c.h in Headers */, 0442EC5D12FE1C60004C9285 /* SDL_x11framebuffer.h in Headers */, 04043BBC12FEB1BE0076DB1F /* SDL_glfuncs.h in Headers */, + 04BAC0B41300C1CB0055DE28 /* SDL_log.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2431,6 +2441,7 @@ 0442EC1D12FE1BCB004C9285 /* SDL_render_sw.c in Sources */, 0442EC5A12FE1C60004C9285 /* SDL_x11framebuffer.c in Sources */, 0442EC5F12FE1C75004C9285 /* SDL_hints.c in Sources */, + 04BAC0C81300C2160055DE28 /* SDL_log.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2550,6 +2561,7 @@ 0442EC1F12FE1BCB004C9285 /* SDL_render_sw.c in Sources */, 0442EC5C12FE1C60004C9285 /* SDL_x11framebuffer.c in Sources */, 0442EC6012FE1C75004C9285 /* SDL_hints.c in Sources */, + 04BAC0C91300C2160055DE28 /* SDL_log.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/include/SDL.h b/include/SDL.h index 483064f2e..2847f4b56 100644 --- a/include/SDL.h +++ b/include/SDL.h @@ -81,6 +81,7 @@ #include "SDL_events.h" #include "SDL_hints.h" #include "SDL_loadso.h" +#include "SDL_log.h" #include "SDL_mutex.h" #include "SDL_power.h" #include "SDL_render.h" diff --git a/include/SDL_error.h b/include/SDL_error.h index 5aa5c9162..407690f4b 100644 --- a/include/SDL_error.h +++ b/include/SDL_error.h @@ -48,7 +48,7 @@ extern DECLSPEC void SDLCALL SDL_ClearError(void); * \name Internal error functions * * \internal - * Private error message function - used internally. + * Private error reporting function - used internally. */ /*@{*/ #define SDL_OutOfMemory() SDL_Error(SDL_ENOMEM) diff --git a/include/SDL_log.h b/include/SDL_log.h new file mode 100644 index 000000000..512dd3d22 --- /dev/null +++ b/include/SDL_log.h @@ -0,0 +1,191 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/** + * \file SDL_log.h + * + * Simple log messages with categories and priorities. + * + * By default logs are quiet, but if you're debugging SDL you might want: + * + * SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN); + * + * Here's where the messages go on different platforms: + * Windows: debug output stream + * Android: log output + * Others: standard error output (stderr) + */ + +#ifndef _SDL_log_h +#define _SDL_log_h + +#include "SDL_stdinc.h" + +#include "begin_code.h" +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C" { +/* *INDENT-ON* */ +#endif + + +/** + * \brief The maximum size of a log message + * + * Messages longer than the maximum size will be truncated + */ +#define SDL_MAX_LOG_MESSAGE 4096 + +/** + * \brief The predefined log categories + * + * By default the application category is enabled at the INFO level, + * and all other categories are enabled at the CRITICAL level. + */ +enum +{ + SDL_LOG_CATEGORY_APPLICATION, + SDL_LOG_CATEGORY_ERROR, + SDL_LOG_CATEGORY_SYSTEM, + SDL_LOG_CATEGORY_AUDIO, + SDL_LOG_CATEGORY_VIDEO, + SDL_LOG_CATEGORY_RENDER, + SDL_LOG_CATEGORY_INPUT, + + /* Reserved for future SDL library use */ + SDL_LOG_CATEGORY_RESERVED1, + SDL_LOG_CATEGORY_RESERVED2, + SDL_LOG_CATEGORY_RESERVED3, + SDL_LOG_CATEGORY_RESERVED4, + SDL_LOG_CATEGORY_RESERVED5, + SDL_LOG_CATEGORY_RESERVED6, + SDL_LOG_CATEGORY_RESERVED7, + SDL_LOG_CATEGORY_RESERVED8, + SDL_LOG_CATEGORY_RESERVED9, + SDL_LOG_CATEGORY_RESERVED10, + + /* Beyond this point is reserved for application use, e.g. + enum { + MYAPP_CATEGORY_AWESOME1 = SDL_LOG_CATEGORY_CUSTOM, + MYAPP_CATEGORY_AWESOME2, + MYAPP_CATEGORY_AWESOME3, + ... + }; + */ + SDL_LOG_CATEGORY_CUSTOM, +}; + +/** + * \brief The predefined log priorities + */ +typedef enum +{ + SDL_LOG_PRIORITY_VERBOSE = 1, + SDL_LOG_PRIORITY_DEBUG, + SDL_LOG_PRIORITY_INFO, + SDL_LOG_PRIORITY_WARN, + SDL_LOG_PRIORITY_ERROR, + SDL_LOG_PRIORITY_CRITICAL, + SDL_NUM_LOG_PRIORITIES +} SDL_LogPriority; + + +/** + * \brief Set the priority of all log categories + */ +extern DECLSPEC void SDLCALL SDL_LogSetAllPriority(SDL_LogPriority priority); + +/** + * \brief Set the priority of a particular log category + */ +extern DECLSPEC void SDLCALL SDL_LogSetPriority(int category, + SDL_LogPriority priority); + +/** + * \brief Set the priority of a particular log category + */ +extern DECLSPEC SDL_LogPriority SDLCALL SDL_LogGetPriority(int category); + +/** + * \brief Reset all priorities to default. + * + * \note This is called in SDL_Quit(). + */ +extern DECLSPEC void SDLCALL SDL_LogResetPriorities(void); + +/** + * \brief Log a message with SDL_LOG_CATEGORY_APPLICATION and SDL_LOG_PRIORITY_INFO + */ +extern DECLSPEC void SDLCALL SDL_Log(const char *fmt, ...); + +/** + * \brief Log a message with SDL_LOG_PRIORITY_VERBOSE + */ +extern DECLSPEC void SDLCALL SDL_LogVerbose(int category, const char *fmt, ...); + +/** + * \brief Log a message with SDL_LOG_PRIORITY_INFO + */ +extern DECLSPEC void SDLCALL SDL_LogInfo(int category, const char *fmt, ...); + +/** + * \brief Log a message with SDL_LOG_PRIORITY_WARN + */ +extern DECLSPEC void SDLCALL SDL_LogWarn(int category, const char *fmt, ...); + +/** + * \brief Log a message with SDL_LOG_PRIORITY_ERROR + */ +extern DECLSPEC void SDLCALL SDL_LogError(int category, const char *fmt, ...); + +/** + * \brief Log a message with SDL_LOG_PRIORITY_CRITICAL + */ +extern DECLSPEC void SDLCALL SDL_LogCritical(int category, const char *fmt, ...); + +/** + * \brief Log a message with the specified category and priority. + */ +extern DECLSPEC void SDLCALL SDL_LogMessage(int category, + SDL_LogPriority priority, + const char *fmt, ...); + +/** + * \brief Log a message with the specified category and priority. + */ +extern DECLSPEC void SDLCALL SDL_LogMessageV(int category, + SDL_LogPriority priority, + const char *fmt, va_list ap); + + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif +#include "close_code.h" + +#endif /* _SDL_log_h */ + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/SDL.c b/src/SDL.c index e6109bc34..16e22f5ab 100644 --- a/src/SDL.c +++ b/src/SDL.c @@ -215,22 +215,12 @@ void SDL_Quit(void) { /* Quit all subsystems */ -#ifdef DEBUG_BUILD - printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n"); - fflush(stdout); -#endif - #if defined(__WIN32__) SDL_HelperWindowDestroy(); #endif SDL_QuitSubSystem(SDL_INIT_EVERYTHING); #ifdef CHECK_LEAKS -#ifdef DEBUG_BUILD - printf("[SDL_Quit] : CHECK_LEAKS\n"); - fflush(stdout); -#endif - /* !!! FIXME: make this an assertion. */ /* Print the number of surfaces not freed */ if (surfaces_allocated != 0) { @@ -238,22 +228,13 @@ SDL_Quit(void) surfaces_allocated); } #endif -#ifdef DEBUG_BUILD - printf("[SDL_Quit] : SDL_UninstallParachute()\n"); - fflush(stdout); -#endif /* Uninstall any parachute signal handlers */ SDL_UninstallParachute(); SDL_ClearHints(); SDL_AssertionsQuit(); - -#ifdef DEBUG_BUILD - printf("[SDL_Quit] : Returning!\n"); - fflush(stdout); -#endif - + SDL_LogResetPriorities(); } /* Get the library version number */ diff --git a/src/SDL_error.c b/src/SDL_error.c index a695fa0a4..5c7457837 100644 --- a/src/SDL_error.c +++ b/src/SDL_error.c @@ -23,14 +23,10 @@ /* Simple error handling in SDL */ -#ifdef __ANDROID__ -#include -#endif - +#include "SDL_log.h" #include "SDL_error.h" #include "SDL_error_c.h" -/*#define DEBUG_ERROR*/ /* Routine to get the thread-specific error variable */ #if SDL_THREADS_DISABLED @@ -113,12 +109,7 @@ SDL_SetError(const char *fmt, ...) va_end(ap); /* If we are in debug mode, print out an error message */ -#ifdef DEBUG_ERROR - fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError()); -#ifdef __ANDROID__ - __android_log_print(ANDROID_LOG_INFO, "SDL", "ERROR: %s", SDL_GetError()); -#endif -#endif /* DEBUG_ERROR */ + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError()); } /* This function has a bit more overhead than most error functions diff --git a/src/SDL_log.c b/src/SDL_log.c new file mode 100755 index 000000000..b17557d0d --- /dev/null +++ b/src/SDL_log.c @@ -0,0 +1,273 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +/* Simple log messages in SDL */ + +#include "SDL_log.h" + +#if HAVE_STDIO_H +#include +#endif + +#if defined(__WIN32__) +#include "core/windows/SDL_windows.h" +#elif defined(__ANDROID__) +#include +#endif + +#define DEFAULT_PRIORITY SDL_LOG_PRIORITY_CRITICAL +#define DEFAULT_APPLICATION_PRIORITY SDL_LOG_PRIORITY_INFO + +typedef struct SDL_LogLevel +{ + int category; + SDL_LogPriority priority; + struct SDL_LogLevel *next; +} SDL_LogLevel; + +static SDL_LogLevel *SDL_loglevels; +static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY; +static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY; + +static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = { + "VERBOSE", + "DEBUG", + "INFO", + "WARN", + "ERROR", + "CRITICAL" +}; + +#ifdef __ANDROID__ +static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = { + "APP", + "ERROR", + "SYSTEM", + "AUDIO", + "VIDEO", + "RENDER", + "INPUT" +}; + +static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = { + ANDROID_LOG_VERBOSE, + ANDROID_LOG_DEBUG, + ANDROID_LOG_INFO, + ANDROID_LOG_WARN, + ANDROID_LOG_ERROR, + ANDROID_LOG_FATAL +}; +#endif /* __ANDROID__ */ + + +void +SDL_LogSetAllPriority(SDL_LogPriority priority) +{ + SDL_LogLevel *entry; + + for (entry = SDL_loglevels; entry; entry = entry->next) { + entry->priority = priority; + } + SDL_application_priority = SDL_default_priority = priority; +} + +void +SDL_LogSetPriority(int category, SDL_LogPriority priority) +{ + SDL_LogLevel *entry; + + for (entry = SDL_loglevels; entry; entry = entry->next) { + if (entry->category == category) { + entry->priority = priority; + return; + } + } + + /* Create a new entry */ + entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry)); + if (entry) { + entry->category = category; + entry->priority = priority; + entry->next = SDL_loglevels; + SDL_loglevels = entry; + } +} + +SDL_LogPriority +SDL_LogGetPriority(int category) +{ + SDL_LogLevel *entry; + + for (entry = SDL_loglevels; entry; entry = entry->next) { + if (entry->category == category) { + return entry->priority; + } + } + + if (category == SDL_LOG_CATEGORY_APPLICATION) { + return SDL_application_priority; + } else { + return SDL_default_priority; + } +} + +void +SDL_LogResetPriorities(void) +{ + SDL_LogLevel *entry; + + while (SDL_loglevels) { + entry = SDL_loglevels; + SDL_loglevels = entry->next; + SDL_free(entry); + } + + SDL_application_priority = DEFAULT_APPLICATION_PRIORITY; + SDL_default_priority = DEFAULT_PRIORITY; +} + +void +SDL_Log(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); + va_end(ap); +} + +void +SDL_LogVerbose(int category, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap); + va_end(ap); +} + +void +SDL_LogInfo(int category, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap); + va_end(ap); +} + +void +SDL_LogWarn(int category, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap); + va_end(ap); +} + +void +SDL_LogError(int category, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap); + va_end(ap); +} + +void +SDL_LogCritical(int category, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap); + va_end(ap); +} + +void +SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + SDL_LogMessageV(category, priority, fmt, ap); + va_end(ap); +} + +#ifdef __ANDROID__ +static const char * +GetCategoryPrefix(int category) +{ + if (category < SDL_LOG_CATEGORY_RESERVED1) { + return SDL_category_prefixes[category]; + } + if (category < SDL_LOG_CATEGORY_CUSTOM) { + return "RESERVED"; + } + return "CUSTOM"; +} +#endif /* __ANDROID__ */ + +void +SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap) +{ + char message[SDL_MAX_LOG_MESSAGE]; + + /* Make sure we don't exceed array bounds */ + if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) { + return; + } + + /* See if we want to do anything with this message */ + if (priority < SDL_LogGetPriority(category)) { + return; + } + + SDL_vsnprintf(message, SDL_arraysize(message), fmt, ap); + +#if defined(__WIN32__) + { + char output[32+SDL_MAX_LOG_MESSAGE]; + LPTSTR tstr; + + SDL_snprintf(output, SDL_arraysize(output), "%s: %s", SDL_priority_prefixes[priority], message); + tstr = WIN_UTF8ToString(output); + OutputDebugString(tstr); + SDL_free(tstr); + } +#elif defined(__ANDROID__) + { + char tag[32]; + + SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category)); + __android_log_write(SDL_android_priority[priority], tag, message); + } +#endif +#if HAVE_STDIO_H + fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message); +#endif +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c index c6396e6b7..d24be3dc8 100644 --- a/src/render/SDL_render.c +++ b/src/render/SDL_render.c @@ -24,6 +24,7 @@ /* The SDL 2D rendering system */ #include "SDL_hints.h" +#include "SDL_log.h" #include "SDL_render.h" #include "SDL_sysrender.h" #include "../video/SDL_pixels_c.h" @@ -159,6 +160,9 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags) renderer->window = window; SDL_AddEventWatch(SDL_RendererEventWatch, renderer); + + SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, + "Created renderer: %s", renderer->info.name); } return renderer; }