Mercurial > sdl-ios-xcode
comparison src/SDL_log.c @ 5226:2ee8112bfc6b
Added a simple log message API
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 07 Feb 2011 16:45:40 -0800 |
parents | |
children | ab0d7cecc0f6 |
comparison
equal
deleted
inserted
replaced
5225:1fbe1c202501 | 5226:2ee8112bfc6b |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997-2010 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Lesser General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2.1 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Lesser General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Lesser General Public | |
16 License along with this library; if not, write to the Free Software | |
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 #include "SDL_config.h" | |
23 | |
24 /* Simple log messages in SDL */ | |
25 | |
26 #include "SDL_log.h" | |
27 | |
28 #if HAVE_STDIO_H | |
29 #include <stdio.h> | |
30 #endif | |
31 | |
32 #if defined(__WIN32__) | |
33 #include "core/windows/SDL_windows.h" | |
34 #elif defined(__ANDROID__) | |
35 #include <android/log.h> | |
36 #endif | |
37 | |
38 #define DEFAULT_PRIORITY SDL_LOG_PRIORITY_CRITICAL | |
39 #define DEFAULT_APPLICATION_PRIORITY SDL_LOG_PRIORITY_INFO | |
40 | |
41 typedef struct SDL_LogLevel | |
42 { | |
43 int category; | |
44 SDL_LogPriority priority; | |
45 struct SDL_LogLevel *next; | |
46 } SDL_LogLevel; | |
47 | |
48 static SDL_LogLevel *SDL_loglevels; | |
49 static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY; | |
50 static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY; | |
51 | |
52 static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = { | |
53 "VERBOSE", | |
54 "DEBUG", | |
55 "INFO", | |
56 "WARN", | |
57 "ERROR", | |
58 "CRITICAL" | |
59 }; | |
60 | |
61 #ifdef __ANDROID__ | |
62 static const char *SDL_category_prefixes[SDL_LOG_CATEGORY_RESERVED1] = { | |
63 "APP", | |
64 "ERROR", | |
65 "SYSTEM", | |
66 "AUDIO", | |
67 "VIDEO", | |
68 "RENDER", | |
69 "INPUT" | |
70 }; | |
71 | |
72 static int SDL_android_priority[SDL_NUM_LOG_PRIORITIES] = { | |
73 ANDROID_LOG_VERBOSE, | |
74 ANDROID_LOG_DEBUG, | |
75 ANDROID_LOG_INFO, | |
76 ANDROID_LOG_WARN, | |
77 ANDROID_LOG_ERROR, | |
78 ANDROID_LOG_FATAL | |
79 }; | |
80 #endif /* __ANDROID__ */ | |
81 | |
82 | |
83 void | |
84 SDL_LogSetAllPriority(SDL_LogPriority priority) | |
85 { | |
86 SDL_LogLevel *entry; | |
87 | |
88 for (entry = SDL_loglevels; entry; entry = entry->next) { | |
89 entry->priority = priority; | |
90 } | |
91 SDL_application_priority = SDL_default_priority = priority; | |
92 } | |
93 | |
94 void | |
95 SDL_LogSetPriority(int category, SDL_LogPriority priority) | |
96 { | |
97 SDL_LogLevel *entry; | |
98 | |
99 for (entry = SDL_loglevels; entry; entry = entry->next) { | |
100 if (entry->category == category) { | |
101 entry->priority = priority; | |
102 return; | |
103 } | |
104 } | |
105 | |
106 /* Create a new entry */ | |
107 entry = (SDL_LogLevel *)SDL_malloc(sizeof(*entry)); | |
108 if (entry) { | |
109 entry->category = category; | |
110 entry->priority = priority; | |
111 entry->next = SDL_loglevels; | |
112 SDL_loglevels = entry; | |
113 } | |
114 } | |
115 | |
116 SDL_LogPriority | |
117 SDL_LogGetPriority(int category) | |
118 { | |
119 SDL_LogLevel *entry; | |
120 | |
121 for (entry = SDL_loglevels; entry; entry = entry->next) { | |
122 if (entry->category == category) { | |
123 return entry->priority; | |
124 } | |
125 } | |
126 | |
127 if (category == SDL_LOG_CATEGORY_APPLICATION) { | |
128 return SDL_application_priority; | |
129 } else { | |
130 return SDL_default_priority; | |
131 } | |
132 } | |
133 | |
134 void | |
135 SDL_LogResetPriorities(void) | |
136 { | |
137 SDL_LogLevel *entry; | |
138 | |
139 while (SDL_loglevels) { | |
140 entry = SDL_loglevels; | |
141 SDL_loglevels = entry->next; | |
142 SDL_free(entry); | |
143 } | |
144 | |
145 SDL_application_priority = DEFAULT_APPLICATION_PRIORITY; | |
146 SDL_default_priority = DEFAULT_PRIORITY; | |
147 } | |
148 | |
149 void | |
150 SDL_Log(const char *fmt, ...) | |
151 { | |
152 va_list ap; | |
153 | |
154 va_start(ap, fmt); | |
155 SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); | |
156 va_end(ap); | |
157 } | |
158 | |
159 void | |
160 SDL_LogVerbose(int category, const char *fmt, ...) | |
161 { | |
162 va_list ap; | |
163 | |
164 va_start(ap, fmt); | |
165 SDL_LogMessageV(category, SDL_LOG_PRIORITY_VERBOSE, fmt, ap); | |
166 va_end(ap); | |
167 } | |
168 | |
169 void | |
170 SDL_LogInfo(int category, const char *fmt, ...) | |
171 { | |
172 va_list ap; | |
173 | |
174 va_start(ap, fmt); | |
175 SDL_LogMessageV(category, SDL_LOG_PRIORITY_INFO, fmt, ap); | |
176 va_end(ap); | |
177 } | |
178 | |
179 void | |
180 SDL_LogWarn(int category, const char *fmt, ...) | |
181 { | |
182 va_list ap; | |
183 | |
184 va_start(ap, fmt); | |
185 SDL_LogMessageV(category, SDL_LOG_PRIORITY_WARN, fmt, ap); | |
186 va_end(ap); | |
187 } | |
188 | |
189 void | |
190 SDL_LogError(int category, const char *fmt, ...) | |
191 { | |
192 va_list ap; | |
193 | |
194 va_start(ap, fmt); | |
195 SDL_LogMessageV(category, SDL_LOG_PRIORITY_ERROR, fmt, ap); | |
196 va_end(ap); | |
197 } | |
198 | |
199 void | |
200 SDL_LogCritical(int category, const char *fmt, ...) | |
201 { | |
202 va_list ap; | |
203 | |
204 va_start(ap, fmt); | |
205 SDL_LogMessageV(category, SDL_LOG_PRIORITY_CRITICAL, fmt, ap); | |
206 va_end(ap); | |
207 } | |
208 | |
209 void | |
210 SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ...) | |
211 { | |
212 va_list ap; | |
213 | |
214 va_start(ap, fmt); | |
215 SDL_LogMessageV(category, priority, fmt, ap); | |
216 va_end(ap); | |
217 } | |
218 | |
219 #ifdef __ANDROID__ | |
220 static const char * | |
221 GetCategoryPrefix(int category) | |
222 { | |
223 if (category < SDL_LOG_CATEGORY_RESERVED1) { | |
224 return SDL_category_prefixes[category]; | |
225 } | |
226 if (category < SDL_LOG_CATEGORY_CUSTOM) { | |
227 return "RESERVED"; | |
228 } | |
229 return "CUSTOM"; | |
230 } | |
231 #endif /* __ANDROID__ */ | |
232 | |
233 void | |
234 SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap) | |
235 { | |
236 char message[SDL_MAX_LOG_MESSAGE]; | |
237 | |
238 /* Make sure we don't exceed array bounds */ | |
239 if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) { | |
240 return; | |
241 } | |
242 | |
243 /* See if we want to do anything with this message */ | |
244 if (priority < SDL_LogGetPriority(category)) { | |
245 return; | |
246 } | |
247 | |
248 SDL_vsnprintf(message, SDL_arraysize(message), fmt, ap); | |
249 | |
250 #if defined(__WIN32__) | |
251 { | |
252 char output[32+SDL_MAX_LOG_MESSAGE]; | |
253 LPTSTR tstr; | |
254 | |
255 SDL_snprintf(output, SDL_arraysize(output), "%s: %s", SDL_priority_prefixes[priority], message); | |
256 tstr = WIN_UTF8ToString(output); | |
257 OutputDebugString(tstr); | |
258 SDL_free(tstr); | |
259 } | |
260 #elif defined(__ANDROID__) | |
261 { | |
262 char tag[32]; | |
263 | |
264 SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category)); | |
265 __android_log_write(SDL_android_priority[priority], tag, message); | |
266 } | |
267 #endif | |
268 #if HAVE_STDIO_H | |
269 fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message); | |
270 #endif | |
271 } | |
272 | |
273 /* vi: set ts=4 sw=4 expandtab: */ |