SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_log.h
1#ifndef SDL3PP_LOG_H_
2#define SDL3PP_LOG_H_
3
4#include <format>
5#include <SDL3/SDL_log.h>
6#include "SDL3pp_stdinc.h"
7
8namespace SDL {
72using LogPriority = SDL_LogPriority;
73
75 SDL_LOG_PRIORITY_INVALID;
76
77constexpr LogPriority LOG_PRIORITY_TRACE = SDL_LOG_PRIORITY_TRACE;
78
80 SDL_LOG_PRIORITY_VERBOSE;
81
82constexpr LogPriority LOG_PRIORITY_DEBUG = SDL_LOG_PRIORITY_DEBUG;
83
84constexpr LogPriority LOG_PRIORITY_INFO = SDL_LOG_PRIORITY_INFO;
85
86constexpr LogPriority LOG_PRIORITY_WARN = SDL_LOG_PRIORITY_WARN;
87
88constexpr LogPriority LOG_PRIORITY_ERROR = SDL_LOG_PRIORITY_ERROR;
89
91 SDL_LOG_PRIORITY_CRITICAL;
92
93constexpr LogPriority LOG_PRIORITY_COUNT = SDL_LOG_PRIORITY_COUNT;
94
96
119{
120 int m_category;
121
122public:
124 constexpr explicit LogCategory(int category)
125 : m_category(category)
126 {
127 }
128
130 constexpr LogCategory(SDL_LogCategory category = SDL_LOG_CATEGORY_APPLICATION)
131 : m_category(category)
132 {
133 }
134
136 constexpr operator int() { return m_category; }
137
139 constexpr operator SDL_LogCategory() { return SDL_LogCategory(m_category); }
140
142 constexpr auto operator<=>(const LogCategory& other) const = default;
143
156 static void SetLogPriorities(LogPriority priority)
157 {
158 return SDL_SetLogPriorities(priority);
159 }
160
175 {
176 return SDL_SetLogPriority(m_category, priority);
177 }
178
190 LogPriority GetLogPriority() const { return SDL_GetLogPriority(m_category); }
191
204 static void ResetLogPriorities() { return SDL_ResetLogPriorities(); }
205
226 void LogUnformatted(LogPriority priority, StringParam message) const
227 {
228 return SDL_LogMessage(m_category, priority, "%s", (const char*)(message));
229 }
230
258 template<class... ARGS>
259 void Log(LogPriority priority, std::string_view fmt, ARGS... args) const
260 {
261 return LogUnformatted(priority,
262 std::vformat(fmt, std::make_format_args(args...)));
263 }
264
290 template<class... ARGS>
291 void LogTrace(std::string_view fmt, ARGS&&... args) const
292 {
293 return Log(SDL_LOG_PRIORITY_TRACE, fmt, std::forward<ARGS>(args)...);
294 }
295
322 template<class... ARGS>
323 void LogVerbose(std::string_view fmt, ARGS&&... args) const
324 {
325 return Log(SDL_LOG_PRIORITY_VERBOSE, fmt, std::forward<ARGS>(args)...);
326 }
327
354 template<class... ARGS>
355 void LogDebug(std::string_view fmt, ARGS&&... args) const
356 {
357 return Log(SDL_LOG_PRIORITY_DEBUG, fmt, std::forward<ARGS>(args)...);
358 }
359
386 template<class... ARGS>
387 void LogInfo(std::string_view fmt, ARGS&&... args) const
388 {
389 return Log(SDL_LOG_PRIORITY_INFO, fmt, std::forward<ARGS>(args)...);
390 }
391
418 template<class... ARGS>
419 void LogWarn(std::string_view fmt, ARGS&&... args) const
420 {
421 return Log(SDL_LOG_PRIORITY_WARN, fmt, std::forward<ARGS>(args)...);
422 }
423
450 template<class... ARGS>
451 void LogError(std::string_view fmt, ARGS&&... args) const
452 {
453 return Log(SDL_LOG_PRIORITY_ERROR, fmt, std::forward<ARGS>(args)...);
454 }
455
482 template<class... ARGS>
483 void LogCritical(std::string_view fmt, ARGS&&... args) const
484 {
485 return Log(SDL_LOG_PRIORITY_CRITICAL, fmt, std::forward<ARGS>(args)...);
486 }
487};
488
490 SDL_LOG_CATEGORY_APPLICATION;
491
492constexpr LogCategory LOG_CATEGORY_ERROR = SDL_LOG_CATEGORY_ERROR;
493
494constexpr LogCategory LOG_CATEGORY_ASSERT = SDL_LOG_CATEGORY_ASSERT;
495
496constexpr LogCategory LOG_CATEGORY_SYSTEM = SDL_LOG_CATEGORY_SYSTEM;
497
498constexpr LogCategory LOG_CATEGORY_AUDIO = SDL_LOG_CATEGORY_AUDIO;
499
500constexpr LogCategory LOG_CATEGORY_VIDEO = SDL_LOG_CATEGORY_VIDEO;
501
502constexpr LogCategory LOG_CATEGORY_RENDER = SDL_LOG_CATEGORY_RENDER;
503
504constexpr LogCategory LOG_CATEGORY_INPUT = SDL_LOG_CATEGORY_INPUT;
505
506constexpr LogCategory LOG_CATEGORY_TEST = SDL_LOG_CATEGORY_TEST;
507
508constexpr LogCategory LOG_CATEGORY_GPU = SDL_LOG_CATEGORY_GPU;
509
511 SDL_LOG_CATEGORY_RESERVED2;
512
514 SDL_LOG_CATEGORY_RESERVED3;
515
517 SDL_LOG_CATEGORY_RESERVED4;
518
520 SDL_LOG_CATEGORY_RESERVED5;
521
523 SDL_LOG_CATEGORY_RESERVED6;
524
526 SDL_LOG_CATEGORY_RESERVED7;
527
529 SDL_LOG_CATEGORY_RESERVED8;
530
532 SDL_LOG_CATEGORY_RESERVED9;
533
535 SDL_LOG_CATEGORY_RESERVED10;
536
537constexpr LogCategory LOG_CATEGORY_CUSTOM = SDL_LOG_CATEGORY_CUSTOM;
538
540
560inline void SetLogPriorityPrefix(LogPriority priority, StringParam prefix)
561{
562 CheckError(SDL_SetLogPriorityPrefix(priority, prefix));
563}
564
592template<class... ARGS>
593inline void Log(std::string_view fmt, ARGS&&... args)
594{
596 LOG_PRIORITY_INFO, fmt, std::forward<ARGS>(args)...);
597}
598
620inline void LogUnformatted(StringParam message)
621{
622 SDL_Log("%s", static_cast<const char*>(message));
623}
624
640using LogOutputFunction = SDL_LogOutputFunction;
641
659using LogOutputCB = std::function<void(LogCategory, LogPriority, const char*)>;
660
674{
675 return SDL_GetDefaultLogOutputFunction();
676}
677
692inline void GetLogOutputFunction(LogOutputFunction* callback, void** userdata)
693{
694 return SDL_GetLogOutputFunction(callback, userdata);
695}
696
713{
716 void* userdata;
717 GetLogOutputFunction(&cb, &userdata);
718 if (userdata == nullptr) {
719 return [cb](LogCategory c, LogPriority p, StringParam m) {
720 cb(nullptr, c, p, m);
721 };
722 }
723 if (auto cb = Wrapper::at(userdata)) return cb;
724 return [cb, userdata](LogCategory c, LogPriority p, StringParam m) {
725 cb(userdata, c, p, m);
726 };
727}
728
743inline void SetLogOutputFunction(LogOutputFunction callback, void* userdata)
744{
746 return SDL_SetLogOutputFunction(callback, userdata);
747}
748
766{
768 SDL_SetLogOutputFunction(
769 [](
770 void* userdata, int category, LogPriority priority, const char* message) {
771 return Wrapper::Call(userdata, LogCategory{category}, priority, message);
772 },
773 Wrapper::Wrap(std::move(callback)));
774}
775
787{
789}
790
792
793} // namespace SDL
794
795#endif /* SDL3PP_LOG_H_ */
The predefined log categories.
Definition SDL3pp_log.h:119
void SetLogPriority(LogPriority priority)
Set the priority of a particular log category.
Definition SDL3pp_log.h:174
void LogWarn(std::string_view fmt, ARGS &&... args) const
Log a message with SDL_LOG_PRIORITY_WARN.
Definition SDL3pp_log.h:419
void LogUnformatted(LogPriority priority, StringParam message) const
Log an unformatted message with the specified priority.
Definition SDL3pp_log.h:226
void LogInfo(std::string_view fmt, ARGS &&... args) const
Log a message with SDL_LOG_PRIORITY_INFO.
Definition SDL3pp_log.h:387
constexpr LogCategory(int category)
Constructor from int.
Definition SDL3pp_log.h:124
void LogVerbose(std::string_view fmt, ARGS &&... args) const
Log a message with SDL_LOG_PRIORITY_VERBOSE.
Definition SDL3pp_log.h:323
void LogTrace(std::string_view fmt, ARGS &&... args) const
Log a message with SDL_LOG_PRIORITY_TRACE.
Definition SDL3pp_log.h:291
void LogCritical(std::string_view fmt, ARGS &&... args) const
Log a message with SDL_LOG_PRIORITY_CRITICAL.
Definition SDL3pp_log.h:483
LogPriority GetLogPriority() const
Get the priority of a particular log category.
Definition SDL3pp_log.h:190
void Log(LogPriority priority, std::string_view fmt, ARGS... args) const
Log a message with the specified priority.
Definition SDL3pp_log.h:259
void LogError(std::string_view fmt, ARGS &&... args) const
Log a message with SDL_LOG_PRIORITY_ERROR.
Definition SDL3pp_log.h:451
void LogDebug(std::string_view fmt, ARGS &&... args) const
Log a message with SDL_LOG_PRIORITY_DEBUG.
Definition SDL3pp_log.h:355
constexpr auto operator<=>(const LogCategory &other) const =default
Comparison operator.
static void ResetLogPriorities()
Reset all priorities to default.
Definition SDL3pp_log.h:204
constexpr LogCategory(SDL_LogCategory category=SDL_LOG_CATEGORY_APPLICATION)
Constructor from SDL_LogCategory.
Definition SDL3pp_log.h:130
static void SetLogPriorities(LogPriority priority)
Set the priority of all log categories.
Definition SDL3pp_log.h:156
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
constexpr LogCategory LOG_CATEGORY_ASSERT
ASSERT.
Definition SDL3pp_log.h:494
constexpr LogCategory LOG_CATEGORY_AUDIO
AUDIO.
Definition SDL3pp_log.h:498
constexpr LogCategory LOG_CATEGORY_VIDEO
VIDEO.
Definition SDL3pp_log.h:500
constexpr LogCategory LOG_CATEGORY_RESERVED10
RESERVED10.
Definition SDL3pp_log.h:534
void SetLogPriorityPrefix(LogPriority priority, StringParam prefix)
Set the text prepended to log messages of a given priority.
Definition SDL3pp_log.h:560
constexpr LogPriority LOG_PRIORITY_INVALID
INVALID.
Definition SDL3pp_log.h:74
void SetLogOutputFunction(LogOutputFunction callback, void *userdata)
Replace the default log output function with one of your own.
Definition SDL3pp_log.h:743
constexpr LogCategory LOG_CATEGORY_INPUT
INPUT.
Definition SDL3pp_log.h:504
LogOutputCB GetLogOutputFunction()
Get the current log output function.
Definition SDL3pp_log.h:712
constexpr LogCategory LOG_CATEGORY_ERROR
ERROR.
Definition SDL3pp_log.h:492
constexpr LogCategory LOG_CATEGORY_RESERVED5
RESERVED5.
Definition SDL3pp_log.h:519
void LogUnformatted(StringParam message)
Log an unformatted message with LOG_CATEGORY_APPLICATION and LOG_PRIORITY_INFO.
Definition SDL3pp_log.h:620
void ResetLogOutputFunction()
Replace the current log output function with the default one.
Definition SDL3pp_log.h:786
constexpr LogPriority LOG_PRIORITY_CRITICAL
CRITICAL.
Definition SDL3pp_log.h:90
constexpr LogCategory LOG_CATEGORY_RESERVED8
RESERVED8.
Definition SDL3pp_log.h:528
SDL_LogPriority LogPriority
The predefined log priorities.
Definition SDL3pp_log.h:72
constexpr LogCategory LOG_CATEGORY_RESERVED7
RESERVED7.
Definition SDL3pp_log.h:525
constexpr LogPriority LOG_PRIORITY_WARN
WARN.
Definition SDL3pp_log.h:86
constexpr LogCategory LOG_CATEGORY_TEST
TEST.
Definition SDL3pp_log.h:506
constexpr LogCategory LOG_CATEGORY_RESERVED2
RESERVED2.
Definition SDL3pp_log.h:510
constexpr LogPriority LOG_PRIORITY_DEBUG
DEBUG.
Definition SDL3pp_log.h:82
void Log(std::string_view fmt, ARGS &&... args)
Log a message with LOG_CATEGORY_APPLICATION and LOG_PRIORITY_INFO.
Definition SDL3pp_log.h:593
constexpr LogCategory LOG_CATEGORY_RENDER
RENDER.
Definition SDL3pp_log.h:502
constexpr LogCategory LOG_CATEGORY_APPLICATION
APPLICATION.
Definition SDL3pp_log.h:489
constexpr LogCategory LOG_CATEGORY_GPU
GPU.
Definition SDL3pp_log.h:508
std::function< void(LogCategory, LogPriority, const char *)> LogOutputCB
The prototype for the log output callback function.
Definition SDL3pp_log.h:659
constexpr LogCategory LOG_CATEGORY_SYSTEM
SYSTEM.
Definition SDL3pp_log.h:496
SDL_LogOutputFunction LogOutputFunction
The prototype for the log output callback function.
Definition SDL3pp_log.h:640
constexpr LogPriority LOG_PRIORITY_ERROR
ERROR.
Definition SDL3pp_log.h:88
constexpr LogPriority LOG_PRIORITY_COUNT
COUNT.
Definition SDL3pp_log.h:93
LogOutputFunction GetDefaultLogOutputFunction()
Get the default log output function.
Definition SDL3pp_log.h:673
constexpr LogPriority LOG_PRIORITY_VERBOSE
VERBOSE.
Definition SDL3pp_log.h:79
constexpr LogPriority LOG_PRIORITY_INFO
INFO.
Definition SDL3pp_log.h:84
constexpr LogCategory LOG_CATEGORY_RESERVED9
RESERVED9.
Definition SDL3pp_log.h:531
constexpr LogCategory LOG_CATEGORY_RESERVED4
RESERVED4.
Definition SDL3pp_log.h:516
constexpr LogPriority LOG_PRIORITY_TRACE
TRACE.
Definition SDL3pp_log.h:77
constexpr LogCategory LOG_CATEGORY_CUSTOM
CUSTOM.
Definition SDL3pp_log.h:537
constexpr LogCategory LOG_CATEGORY_RESERVED3
RESERVED3.
Definition SDL3pp_log.h:513
constexpr LogCategory LOG_CATEGORY_RESERVED6
RESERVED6.
Definition SDL3pp_log.h:522
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Stored Wrapper unique by type result callbacks.
Definition SDL3pp_callbackWrapper.h:242