1 /*
2     DSDL
3     Copyright (C) 2025 Inochi2D Project <luna@foxgirls.gay>
4 
5     This software is provided 'as-is', without any express or implied
6     warranty.  In no event will the authors be held liable for any damages
7     arising from the use of this software.
8 
9     Permission is granted to anyone to use this software for any purpose,
10     including commercial applications, and to alter it and redistribute it
11     freely, subject to the following restrictions:
12 
13     1. The origin of this software must not be misrepresented; you must not
14         claim that you wrote the original software. If you use this software
15         in a product, an acknowledgment in the product documentation would be
16         appreciated but is not required.
17     2. Altered source versions must be plainly marked as such, and must not be
18         misrepresented as being the original software.
19     3. This notice may not be removed or altered from any source distribution.
20 
21     ==========================================================================
22 
23     Simple DirectMedia Layer
24     Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
25 
26     This software is provided 'as-is', without any express or implied
27     warranty.  In no event will the authors be held liable for any damages
28     arising from the use of this software.
29 
30     Permission is granted to anyone to use this software for any purpose,
31     including commercial applications, and to alter it and redistribute it
32     freely, subject to the following restrictions:
33 
34     1. The origin of this software must not be misrepresented; you must not
35         claim that you wrote the original software. If you use this software
36         in a product, an acknowledgment in the product documentation would be
37         appreciated but is not required.
38     2. Altered source versions must be plainly marked as such, and must not be
39         misrepresented as being the original software.
40     3. This notice may not be removed or altered from any source distribution.
41 */
42 
43 /**
44     SDL Log Handling
45 
46     See_Also:
47         $(LINK2 https://wiki.libsdl.org/SDL3/CategoryLog, SDL3 Log Documentation)
48     
49     Copyright: © 2025 Inochi2D Project, © 1997-2025 Sam Lantinga
50     License: Subject to the terms of the Zlib License, as written in the LICENSE file.
51     Authors:
52         Maxwell Ruben
53 */
54 module sdl.log;
55 import core.stdc.stdarg : va_list;
56 
57 extern(C) nothrow @nogc:
58 
59 /**
60     The predefined log categories
61 
62     By default the application and gpu categories are enabled at the INFO
63     level, the assert category is enabled at the WARN level, test is enabled at
64     the VERBOSE level and all other categories are enabled at the ERROR level.
65 */
66 enum SDL_LogCategory {
67     SDL_LOG_CATEGORY_APPLICATION,
68     SDL_LOG_CATEGORY_ERROR,
69     SDL_LOG_CATEGORY_ASSERT,
70     SDL_LOG_CATEGORY_SYSTEM,
71     SDL_LOG_CATEGORY_AUDIO,
72     SDL_LOG_CATEGORY_VIDEO,
73     SDL_LOG_CATEGORY_RENDER,
74     SDL_LOG_CATEGORY_INPUT,
75     SDL_LOG_CATEGORY_TEST,
76     SDL_LOG_CATEGORY_GPU,
77 
78     /**
79         Reserved for future SDL library use
80     */
81     SDL_LOG_CATEGORY_RESERVED2,
82     SDL_LOG_CATEGORY_RESERVED3,
83     SDL_LOG_CATEGORY_RESERVED4,
84     SDL_LOG_CATEGORY_RESERVED5,
85     SDL_LOG_CATEGORY_RESERVED6,
86     SDL_LOG_CATEGORY_RESERVED7,
87     SDL_LOG_CATEGORY_RESERVED8,
88     SDL_LOG_CATEGORY_RESERVED9,
89     SDL_LOG_CATEGORY_RESERVED10,
90 
91     /**
92         Beyond this point is reserved for application use, e.g.
93         enum {
94             MYAPP_CATEGORY_AWESOME1 = SDL_LOG_CATEGORY_CUSTOM,
95             MYAPP_CATEGORY_AWESOME2,
96             MYAPP_CATEGORY_AWESOME3,
97             ...
98         };
99     */
100     SDL_LOG_CATEGORY_CUSTOM,
101 }
102 
103 /**
104     The predefined log priorities
105 */
106 enum SDL_LogPriority {
107     SDL_LOG_PRIORITY_INVALID,
108     SDL_LOG_PRIORITY_TRACE,
109     SDL_LOG_PRIORITY_VERBOSE,
110     SDL_LOG_PRIORITY_DEBUG,
111     SDL_LOG_PRIORITY_INFO,
112     SDL_LOG_PRIORITY_WARN,
113     SDL_LOG_PRIORITY_ERROR,
114     SDL_LOG_PRIORITY_CRITICAL,
115     SDL_LOG_PRIORITY_COUNT,
116 }
117 
118 /**
119     Set the priority of all log categories.
120     
121     Params:
122         priority = the SDL_LogPriority to assign.
123     
124     Threadsafety:
125         It is safe to call this function from any thread.
126 
127     See_Also:    
128         $(D SDL_ResetLogPriorities)
129         $(D SDL_SetLogPriority)
130 */
131 extern void SDL_SetLogPriorities(SDL_LogPriority priority);
132 
133 /**
134     Set the priority of a particular log category.
135     
136     Params:
137         category = the category to assign a priority to.
138         priority = the SDL_LogPriority to assign.
139     
140     Threadsafety:
141         It is safe to call this function from any thread.
142     
143     See_Also:
144         $(D SDL_GetLogPriority)
145         $(D SDL_ResetLogPriorities)
146         $(D SDL_SetLogPriorities)
147 */
148 extern void SDL_SetLogPriority(int category, SDL_LogPriority priority);
149 
150 /**
151     Get the priority of a particular log category.
152    
153     Params:
154         category = the category to query.
155 
156     Returns:
157         the SDL_LogPriority for the requested category.
158    
159     Threadsafety:
160         It is safe to call this function from any thread.
161       
162     See_Also:
163         $(D SDL_SetLogPriority)
164 */
165 SDL_LogPriority SDL_GetLogPriority(int category);
166 
167 /**
168     Reset all priorities to default.
169     
170     This is called by SDL_Quit().
171     
172     Threadsafety:
173         It is safe to call this function from any thread.
174     
175     See_Also:
176         $(D SDL_SetLogPriorities)
177         $(D SDL_SetLogPriority)
178 */
179 void SDL_ResetLogPriorities();
180 
181 /**
182     Set the text prepended to log messages of a given priority.
183     
184     By default SDL_LOG_PRIORITY_INFO and below have no prefix, and
185     SDL_LOG_PRIORITY_WARN and higher have a prefix showing their priority, e.g.
186     "WARNING\: ".
187     
188     This function makes a copy of its string argument, **prefix**, so it is not
189     necessary to keep the value of **prefix** alive after the call returns.
190     
191     Params:
192         priority =  the SDL_LogPriority to modify.
193         prefix =    the prefix to use for that log priority, or NULL to use no
194                     prefix.
195     Returns:
196         true on success or false on failure; call SDL_GetError() for more
197              information.
198     
199     Threadsafety:
200         It is safe to call this function from any thread.
201     
202     See_Also:
203         $(D SDL_SetLogPriorities)
204         $(D SDL_SetLogPriority)
205 */
206 bool SDL_SetLogPriorityPrefix(SDL_LogPriority priority, const (char)* prefix);
207 
208 /**
209     Log a message with SDL_LOG_CATEGORY_APPLICATION and SDL_LOG_PRIORITY_INFO.
210     
211     Params:
212         fmt =   a printf() style message format string.
213         ... =   additional parameters matching % tokens in the `fmt` string, if
214                 any.
215     
216     Threadsafety:
217         It is safe to call this function from any thread.
218     
219     See_Also:
220         $(D SDL_LogCritical)
221         $(D SDL_LogDebug)
222         $(D SDL_LogError)
223         $(D SDL_LogInfo)
224         $(D SDL_LogMessage)
225         $(D SDL_LogMessageV)
226         $(D SDL_LogTrace)
227         $(D SDL_LogVerbose)
228         $(D SDL_LogWarn)
229 */
230 void SDL_Log(const(char)* fmt, ...);
231 
232 /**
233     Log a message with SDL_LOG_PRIORITY_TRACE.
234     
235     Params:
236         category =  the category of the message.
237         fmt =       a printf() style message format string.
238         ... =       additional parameters matching % tokens in the **fmt** string,
239                     if any.
240     
241     Threadsafety:
242         It is safe to call this function from any thread.
243     
244     See_Also:
245         $(D SDL_Log)
246         $(D SDL_LogCritical)
247         $(D SDL_LogDebug)
248         $(D SDL_LogError)
249         $(D SDL_LogInfo)
250         $(D SDL_LogMessage)
251         $(D SDL_LogMessageV)
252         $(D SDL_LogTrace)
253         $(D SDL_LogVerbose)
254         $(D SDL_LogWarn)
255 */
256 void SDL_LogTrace(int category, const(char)* fmt, ...);
257 
258 /**
259     Log a message with SDL_LOG_PRIORITY_VERBOSE.
260     
261     Params:
262         category =  the category of the message.
263         fmt =       a printf() style message format string.
264         ... =       additional parameters matching % tokens in the **fmt** string,
265                     if any.
266     
267     Threadsafety:
268         It is safe to call this function from any thread.
269     
270     See_Also:
271         $(D SDL_Log)
272         $(D SDL_LogCritical)
273         $(D SDL_LogDebug)
274         $(D SDL_LogError)
275         $(D SDL_LogInfo)
276         $(D SDL_LogMessage)
277         $(D SDL_LogMessageV)
278         $(D SDL_LogWarn)
279 */
280 void SDL_LogVerbose(int category, const(char)* fmt, ...);
281 
282 /**
283     Log a message with SDL_LOG_PRIORITY_DEBUG.
284     
285     Params:
286         category =  the category of the message.
287         fmt =       a printf() style message format string.
288         ... =       additional parameters matching % tokens in the **fmt** string,
289                     if any.
290     
291     Threadsafety:
292         It is safe to call this function from any thread.
293     
294     See_Also:
295         $(D SDL_Log)
296         $(D SDL_LogCritical)
297         $(D SDL_LogError)
298         $(D SDL_LogInfo)
299         $(D SDL_LogMessage)
300         $(D SDL_LogMessageV)
301         $(D SDL_LogTrace)
302         $(D SDL_LogVerbose)
303         $(D SDL_LogWarn)
304 */
305 void SDL_LogDebug(int category, const(char)* fmt, ...);
306 
307 /**
308     Log a message with SDL_LOG_PRIORITY_INFO.
309     
310     Params:
311         category =  the category of the message.
312         fmt =       a printf() style message format string.
313         ... =       additional parameters matching % tokens in the **fmt** string,
314                     if any.
315     
316     Threadsafety:
317         It is safe to call this function from any thread.
318     
319     See_Also:
320         $(D SDL_Log)
321         $(D SDL_LogCritical)
322         $(D SDL_LogDebug)
323         $(D SDL_LogError)
324         $(D SDL_LogMessage)
325         $(D SDL_LogMessageV)
326         $(D SDL_LogTrace)
327         $(D SDL_LogVerbose)
328         $(D SDL_LogWarn)
329 */
330 void SDL_LogInfo(int category, const(char)* fmt, ...);
331 
332 /**
333     Log a message with SDL_LOG_PRIORITY_WARN.
334     
335     Params:
336         category =  the category of the message.
337         fmt =       a printf() style message format string.
338         ... =       additional parameters matching % tokens in the **fmt** string,
339                     if any.
340     
341     Threadsafety:
342         It is safe to call this function from any thread.
343     
344     See_Also:
345         $(D SDL_Log)
346         $(D SDL_LogCritical)
347         $(D SDL_LogDebug)
348         $(D SDL_LogError)
349         $(D SDL_LogInfo)
350         $(D SDL_LogMessage)
351         $(D SDL_LogMessageV)
352         $(D SDL_LogTrace)
353         $(D SDL_LogVerbose)
354 */
355 void SDL_LogWarn(int category, const(char)* fmt, ...);
356 
357 /**
358     Log a message with SDL_LOG_PRIORITY_ERROR.
359     
360     Params:
361         category =  the category of the message.
362         fmt =       a printf() style message format string.
363         ... =       additional parameters matching % tokens in the **fmt** string,
364                     if any.
365     
366     Threadsafety:
367         It is safe to call this function from any thread.
368     
369     See_Also:
370         $(D SDL_Log)
371         $(D SDL_LogCritical)
372         $(D SDL_LogDebug)
373         $(D SDL_LogInfo)
374         $(D SDL_LogMessage)
375         $(D SDL_LogMessageV)
376         $(D SDL_LogTrace)
377         $(D SDL_LogVerbose)
378         $(D SDL_LogWarn)
379 */
380 void SDL_LogError(int category, const(char)* fmt, ...);
381 
382 /**
383     Log a message with SDL_LOG_PRIORITY_CRITICAL.
384     
385     Params:
386         category =  the category of the message.
387         fmt =       a printf() style message format string.
388         ... =       additional parameters matching % tokens in the **fmt** string,
389                     if any.
390     
391     Threadsafety:
392         It is safe to call this function from any thread.
393     
394     See_Also:
395         $(D SDL_Log)
396         $(D SDL_LogDebug)
397         $(D SDL_LogError)
398         $(D SDL_LogInfo)
399         $(D SDL_LogMessage)
400         $(D SDL_LogMessageV)
401         $(D SDL_LogTrace)
402         $(D SDL_LogVerbose)
403         $(D SDL_LogWarn)
404 */
405 void SDL_LogCritical(int category, const(char)* fmt, ...);
406 
407 /**
408     Log a message with the specified category and priority.
409     
410     Params:
411         category =  the category of the message.
412         priority =  the priority of the message.
413         fmt =       a printf() style message format string.
414         ... =       additional parameters matching % tokens in the **fmt** string,
415                     if any.
416     
417     Threadsafety:
418         It is safe to call this function from any thread.
419     
420     See_Also:
421         $(D SDL_Log)
422         $(D SDL_LogCritical)
423         $(D SDL_LogDebug)
424         $(D SDL_LogError)
425         $(D SDL_LogInfo)
426         $(D SDL_LogMessageV)
427         $(D SDL_LogTrace)
428         $(D SDL_LogVerbose)
429         $(D SDL_LogWarn)
430 */
431 void SDL_LogMessage(int category, SDL_LogPriority priority, const(char)* fmt, ...);
432 
433 /**
434     Log a message with the specified category and priority.
435     
436     Params:
437         category =  the category of the message.
438         priority =  the priority of the message.
439         fmt =       a printf() style message format string.
440         ap =        a variable argument list.
441     
442     Threadsafety:
443         It is safe to call this function from any thread.
444     
445     See_Also:
446         $(D SDL_Log)
447         $(D SDL_LogCritical)
448         $(D SDL_LogDebug)
449         $(D SDL_LogError)
450         $(D SDL_LogInfo)
451         $(D SDL_LogMessage)
452         $(D SDL_LogTrace)
453         $(D SDL_LogVerbose)
454         $(D SDL_LogWarn)
455 */
456 void SDL_LogMessageV(int category, SDL_LogPriority priority, const(char)* fmt, va_list ap);
457 
458 /**
459     The prototype for the log output callback function.
460     
461     This function is called by SDL when there is new text to be logged. A mutex
462     is held so that this function is never called by more than one thread at
463     once.
464     
465     Params:
466         userdata =  what was passed as `userdata` to
467                     SDL_SetLogOutputFunction().
468         category =  the category of the message.
469         priority =  the priority of the message.
470         message =   the message being output.
471 */
472 alias SDL_LogOutputFunction = void function(void* userdata, int category, SDL_LogPriority priority, const(char)* message);
473 
474 /**
475     Get the default log output function.
476     
477     Returns:
478         the default log output callback.
479     
480     Threadsafety:
481         It is safe to call this function from any thread.
482     
483     See_Also:
484         $(D SDL_SetLogOutputFunction)
485         $(D SDL_GetLogOutputFunction)
486 */
487 SDL_LogOutputFunction SDL_GetDefaultLogOutputFunction();
488 
489 /**
490     Get the current log output function.
491     
492     Params:
493         callback =  an SDL_LogOutputFunction filled in with the current log
494                     callback.
495         userdata =  a pointer filled in with the pointer that is passed to
496                     `callback`.
497     
498     Threadsafety:
499         It is safe to call this function from any thread.
500     
501     See_Also:
502         $(D SDL_GetDefaultLogOutputFunction)
503         $(D SDL_SetLogOutputFunction)
504 */
505 void SDL_GetLogOutputFunction(SDL_LogOutputFunction* callback, void** userdata);
506 
507 
508 /**
509     Replace the default log output function with one of your own.
510     
511     Params:
512         callback = an SDL_LogOutputFunction to call instead of the default.
513         userdata = a pointer that is passed to `callback`.
514     
515     Threadsafety:
516         It is safe to call this function from any thread.
517     
518     See_Also:
519         $(D SDL_GetDefaultLogOutputFunction)
520         $(D SDL_GetLogOutputFunction)
521 */
522 void SDL_SetLogOutputFunction(SDL_LogOutputFunction callback, void* userdata);