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_callbackWrapper.h"
7#include "SDL3pp_error.h"
8#include "SDL3pp_strings.h"
9
10namespace SDL {
11
63using LogCategoryRaw = SDL_LogCategory;
64
65// Forward decl
66struct LogCategory;
67
81using LogPriority = SDL_LogPriority;
82
84 SDL_LOG_PRIORITY_INVALID;
85
86constexpr LogPriority LOG_PRIORITY_TRACE = SDL_LOG_PRIORITY_TRACE;
87
89 SDL_LOG_PRIORITY_VERBOSE;
90
91constexpr LogPriority LOG_PRIORITY_DEBUG = SDL_LOG_PRIORITY_DEBUG;
92
93constexpr LogPriority LOG_PRIORITY_INFO = SDL_LOG_PRIORITY_INFO;
94
95constexpr LogPriority LOG_PRIORITY_WARN = SDL_LOG_PRIORITY_WARN;
96
97constexpr LogPriority LOG_PRIORITY_ERROR = SDL_LOG_PRIORITY_ERROR;
98
100 SDL_LOG_PRIORITY_CRITICAL;
101
102constexpr LogPriority LOG_PRIORITY_COUNT = SDL_LOG_PRIORITY_COUNT;
103
105
128{
129 LogCategoryRaw m_category;
130
131public:
137 constexpr LogCategory(LogCategoryRaw category = SDL_LOG_CATEGORY_APPLICATION)
138 : m_category(category)
139 {
140 }
141
147 constexpr explicit LogCategory(int category)
148 : m_category(SDL_LogCategory(category))
149 {
150 }
151
157 constexpr operator LogCategoryRaw() const { return m_category; }
158
172 void SetLogPriority(LogPriority priority) const;
173
186
207 void LogUnformatted(LogPriority priority, StringParam message) const
208 {
209 SDL_LogMessage(m_category, priority, "%s", (const char*)(message));
210 }
211
237 template<class... ARGS>
238 void LogMessage(LogPriority priority,
239 std::string_view fmt,
240 ARGS... args) const;
241
266 template<class... ARGS>
267 void LogTrace(std::string_view fmt, ARGS&&... args) const;
268
292 template<class... ARGS>
293 void LogVerbose(std::string_view fmt, ARGS&&... args) const;
294
319 template<class... ARGS>
320 void LogDebug(std::string_view fmt, ARGS&&... args) const;
321
346 template<class... ARGS>
347 void LogInfo(std::string_view fmt, ARGS&&... args) const;
348
373 template<class... ARGS>
374 void LogWarn(std::string_view fmt, ARGS&&... args) const;
375
400 template<class... ARGS>
401 void LogError(std::string_view fmt, ARGS&&... args) const;
402
428 template<class... ARGS>
429 void LogCritical(std::string_view fmt, ARGS&&... args) const;
430};
431
433 SDL_LOG_CATEGORY_APPLICATION;
434
435constexpr LogCategory LOG_CATEGORY_ERROR = SDL_LOG_CATEGORY_ERROR;
436
437constexpr LogCategory LOG_CATEGORY_ASSERT = SDL_LOG_CATEGORY_ASSERT;
438
439constexpr LogCategory LOG_CATEGORY_SYSTEM = SDL_LOG_CATEGORY_SYSTEM;
440
441constexpr LogCategory LOG_CATEGORY_AUDIO = SDL_LOG_CATEGORY_AUDIO;
442
443constexpr LogCategory LOG_CATEGORY_VIDEO = SDL_LOG_CATEGORY_VIDEO;
444
445constexpr LogCategory LOG_CATEGORY_RENDER = SDL_LOG_CATEGORY_RENDER;
446
447constexpr LogCategory LOG_CATEGORY_INPUT = SDL_LOG_CATEGORY_INPUT;
448
449constexpr LogCategory LOG_CATEGORY_TEST = SDL_LOG_CATEGORY_TEST;
450
451constexpr LogCategory LOG_CATEGORY_GPU = SDL_LOG_CATEGORY_GPU;
452
454 SDL_LOG_CATEGORY_RESERVED2;
455
457 SDL_LOG_CATEGORY_RESERVED3;
458
460 SDL_LOG_CATEGORY_RESERVED4;
461
463 SDL_LOG_CATEGORY_RESERVED5;
464
466 SDL_LOG_CATEGORY_RESERVED6;
467
469 SDL_LOG_CATEGORY_RESERVED7;
470
472 SDL_LOG_CATEGORY_RESERVED8;
473
475 SDL_LOG_CATEGORY_RESERVED9;
476
478 SDL_LOG_CATEGORY_RESERVED10;
479
480constexpr LogCategory LOG_CATEGORY_CUSTOM = SDL_LOG_CATEGORY_CUSTOM;
481
494inline void SetLogPriorities(LogPriority priority)
495{
496 SDL_SetLogPriorities(priority);
497}
498
513inline void SetLogPriority(int category, LogPriority priority)
514{
515 SDL_SetLogPriority(category, priority);
516}
517
518inline void LogCategory::SetLogPriority(LogPriority priority) const
519{
520 SDL::SetLogPriority(m_category, priority);
521}
522
535inline LogPriority GetLogPriority(int category)
536{
537 return SDL_GetLogPriority(category);
538}
539
541{
542 return SDL::GetLogPriority(m_category);
543}
544
557inline void ResetLogPriorities() { SDL_ResetLogPriorities(); }
558
578inline void SetLogPriorityPrefix(LogPriority priority, StringParam prefix)
579{
580 CheckError(SDL_SetLogPriorityPrefix(priority, prefix));
581}
582
605inline void LogUnformatted(LogCategory category,
606 LogPriority priority,
607 StringParam message)
608{
609 SDL_LogMessage(category, priority, "%s", static_cast<const char*>(message));
610}
611
632inline void LogUnformatted(StringParam message)
633{
634 SDL_Log("%s", static_cast<const char*>(message));
635}
636
664template<class... ARGS>
665inline void Log(std::string_view fmt, ARGS&&... args)
666{
667 LOG_CATEGORY_APPLICATION.LogInfo(fmt, std::forward<ARGS>(args)...);
668}
669
693template<class... ARGS>
694inline void LogMessage(LogCategory category,
695 LogPriority priority,
696 std::string_view fmt,
697 ARGS... args)
698{
700 category, priority, std::vformat(fmt, std::make_format_args(args...)));
701}
702
703template<class... ARGS>
705 std::string_view fmt,
706 ARGS... args) const
707{
708 SDL::LogMessage(m_category, priority, fmt, args...);
709}
710
734template<class... ARGS>
735inline void LogTrace(LogCategory category, std::string_view fmt, ARGS&&... args)
736{
737 LogMessage(category, LOG_PRIORITY_TRACE, fmt, args...);
738}
739
740template<class... ARGS>
741inline void LogCategory::LogTrace(std::string_view fmt, ARGS&&... args) const
742{
743 SDL::LogTrace(m_category, fmt, args...);
744}
745
767template<class... ARGS>
768inline void LogVerbose(LogCategory category,
769 std::string_view fmt,
770 ARGS&&... args)
771{
772 LogMessage(category, LOG_PRIORITY_VERBOSE, fmt, args...);
773}
774
775template<class... ARGS>
776inline void LogCategory::LogVerbose(std::string_view fmt, ARGS&&... args) const
777{
778 SDL::LogVerbose(m_category, fmt, args...);
779}
780
803template<class... ARGS>
804inline void LogDebug(LogCategory category, std::string_view fmt, ARGS&&... args)
805{
806 LogMessage(category, LOG_PRIORITY_DEBUG, fmt, args...);
807}
808
809template<class... ARGS>
810inline void LogCategory::LogDebug(std::string_view fmt, ARGS&&... args) const
811{
812 SDL::LogDebug(m_category, fmt, args...);
813}
814
837template<class... ARGS>
838inline void LogInfo(LogCategory category, std::string_view fmt, ARGS&&... args)
839{
840 LogMessage(category, LOG_PRIORITY_INFO, fmt, args...);
841}
842
843template<class... ARGS>
844inline void LogCategory::LogInfo(std::string_view fmt, ARGS&&... args) const
845{
846 SDL::LogInfo(m_category, fmt, args...);
847}
848
871template<class... ARGS>
872inline void LogWarn(LogCategory category, std::string_view fmt, ARGS&&... args)
873{
874 LogMessage(category, SDL_LOG_PRIORITY_WARN, fmt, args...);
875}
876
877template<class... ARGS>
878inline void LogCategory::LogWarn(std::string_view fmt, ARGS&&... args) const
879{
880 SDL::LogWarn(m_category, fmt, args...);
881}
882
905template<class... ARGS>
906inline void LogError(LogCategory category, std::string_view fmt, ARGS&&... args)
907{
908 LogMessage(category, SDL_LOG_PRIORITY_ERROR, fmt, args...);
909}
910
911template<class... ARGS>
912inline void LogCategory::LogError(std::string_view fmt, ARGS&&... args) const
913{
914 SDL::LogError(m_category, fmt, args...);
915}
916
939template<class... ARGS>
940inline void LogCritical(LogCategory category,
941 std::string_view fmt,
942 ARGS&&... args)
943{
944 LogMessage(category, SDL_LOG_PRIORITY_CRITICAL, fmt, args...);
945}
946
947template<class... ARGS>
948inline void LogCategory::LogCritical(std::string_view fmt, ARGS&&... args) const
949{
950 SDL::LogCritical(m_category, fmt, args...);
951}
952
968using LogOutputFunction = SDL_LogOutputFunction;
969
987using LogOutputCB = std::function<void(LogCategory, LogPriority, const char*)>;
988
1002{
1003 return SDL_GetDefaultLogOutputFunction();
1004}
1005
1020inline void GetLogOutputFunction(LogOutputFunction* callback, void** userdata)
1021{
1022 SDL_GetLogOutputFunction(callback, userdata);
1023}
1024
1041{
1042 using Wrapper = UniqueCallbackWrapper<LogOutputCB>;
1044 void* userdata;
1045 GetLogOutputFunction(&cb, &userdata);
1046 if (userdata == nullptr) {
1047 return [cb](LogCategory c, LogPriority p, StringParam m) {
1048 cb(nullptr, c, p, m);
1049 };
1050 }
1051 if (auto cb = Wrapper::at(userdata)) return cb;
1052 return [cb, userdata](LogCategory c, LogPriority p, StringParam m) {
1053 cb(userdata, c, p, m);
1054 };
1055}
1056
1071inline void SetLogOutputFunction(LogOutputFunction callback, void* userdata)
1072{
1074 return SDL_SetLogOutputFunction(callback, userdata);
1075}
1076
1094{
1095 using Wrapper = UniqueCallbackWrapper<LogOutputCB>;
1096 SDL_SetLogOutputFunction(
1097 [](
1098 void* userdata, int category, LogPriority priority, const char* message) {
1099 return Wrapper::Call(userdata, LogCategory{category}, priority, message);
1100 },
1101 Wrapper::Wrap(std::move(callback)));
1102}
1103
1115{
1117}
1118
1120
1121} // namespace SDL
1122
1123#endif /* SDL3PP_LOG_H_ */
The predefined log categories.
Definition: SDL3pp_log.h:128
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:198
void SetLogPriorities(LogPriority priority)
Set the priority of all log categories.
Definition: SDL3pp_log.h:494
constexpr LogCategory LOG_CATEGORY_ASSERT
ASSERT.
Definition: SDL3pp_log.h:437
constexpr LogCategory LOG_CATEGORY_AUDIO
AUDIO.
Definition: SDL3pp_log.h:441
void LogWarn(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_WARN.
Definition: SDL3pp_log.h:878
constexpr LogCategory LOG_CATEGORY_VIDEO
VIDEO.
Definition: SDL3pp_log.h:443
SDL_LogCategory LogCategoryRaw
Alias to raw representation for LogCategory.
Definition: SDL3pp_log.h:63
constexpr LogCategory LOG_CATEGORY_RESERVED10
RESERVED10.
Definition: SDL3pp_log.h:477
void LogVerbose(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_VERBOSE.
Definition: SDL3pp_log.h:768
void SetLogPriorityPrefix(LogPriority priority, StringParam prefix)
Set the text prepended to log messages of a given priority.
Definition: SDL3pp_log.h:578
LogPriority GetLogPriority(int category)
Get the priority of a particular log category.
Definition: SDL3pp_log.h:535
void SetLogPriority(int category, LogPriority priority)
Set the priority of a particular log category.
Definition: SDL3pp_log.h:513
void LogTrace(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_TRACE.
Definition: SDL3pp_log.h:735
void LogUnformatted(LogPriority priority, StringParam message) const
Log an unformatted message with the specified priority.
Definition: SDL3pp_log.h:207
constexpr LogPriority LOG_PRIORITY_INVALID
INVALID.
Definition: SDL3pp_log.h:83
void SetLogOutputFunction(LogOutputFunction callback, void *userdata)
Replace the default log output function with one of your own.
Definition: SDL3pp_log.h:1071
constexpr LogCategory LOG_CATEGORY_INPUT
INPUT.
Definition: SDL3pp_log.h:447
LogOutputCB GetLogOutputFunction()
Get the current log output function.
Definition: SDL3pp_log.h:1040
constexpr LogCategory LOG_CATEGORY_ERROR
ERROR.
Definition: SDL3pp_log.h:435
void LogInfo(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_INFO.
Definition: SDL3pp_log.h:844
constexpr LogCategory(int category)
Wraps LogCategory.
Definition: SDL3pp_log.h:147
constexpr LogCategory LOG_CATEGORY_RESERVED5
RESERVED5.
Definition: SDL3pp_log.h:462
void LogVerbose(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_VERBOSE.
Definition: SDL3pp_log.h:776
void LogCritical(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_CRITICAL.
Definition: SDL3pp_log.h:940
void LogTrace(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_TRACE.
Definition: SDL3pp_log.h:741
void ResetLogOutputFunction()
Replace the current log output function with the default one.
Definition: SDL3pp_log.h:1114
constexpr LogPriority LOG_PRIORITY_CRITICAL
CRITICAL.
Definition: SDL3pp_log.h:99
void LogCritical(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_CRITICAL.
Definition: SDL3pp_log.h:948
void LogDebug(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_DEBUG.
Definition: SDL3pp_log.h:804
constexpr LogCategory LOG_CATEGORY_RESERVED8
RESERVED8.
Definition: SDL3pp_log.h:471
LogPriority GetLogPriority() const
Get the priority of a particular log category.
Definition: SDL3pp_log.h:540
SDL_LogPriority LogPriority
The predefined log priorities.
Definition: SDL3pp_log.h:81
void LogError(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_ERROR.
Definition: SDL3pp_log.h:912
constexpr LogCategory LOG_CATEGORY_RESERVED7
RESERVED7.
Definition: SDL3pp_log.h:468
constexpr LogPriority LOG_PRIORITY_WARN
WARN.
Definition: SDL3pp_log.h:95
void LogUnformatted(LogCategory category, LogPriority priority, StringParam message)
Log an unformatted message with LOG_CATEGORY_APPLICATION and LOG_PRIORITY_INFO.
Definition: SDL3pp_log.h:605
void LogMessage(LogPriority priority, std::string_view fmt, ARGS... args) const
Log a message with the specified priority.
Definition: SDL3pp_log.h:704
void LogDebug(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_DEBUG.
Definition: SDL3pp_log.h:810
void LogError(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_ERROR.
Definition: SDL3pp_log.h:906
constexpr LogCategory LOG_CATEGORY_TEST
TEST.
Definition: SDL3pp_log.h:449
constexpr LogCategory LOG_CATEGORY_RESERVED2
RESERVED2.
Definition: SDL3pp_log.h:453
constexpr LogPriority LOG_PRIORITY_DEBUG
DEBUG.
Definition: SDL3pp_log.h:91
void LogMessage(LogCategory category, LogPriority priority, std::string_view fmt, ARGS... args)
Log a message with the specified category and priority.
Definition: SDL3pp_log.h:694
void Log(std::string_view fmt, ARGS &&... args)
Log a message with LOG_CATEGORY_APPLICATION and LOG_PRIORITY_INFO.
Definition: SDL3pp_log.h:665
constexpr LogCategory LOG_CATEGORY_RENDER
RENDER.
Definition: SDL3pp_log.h:445
constexpr LogCategory LOG_CATEGORY_APPLICATION
APPLICATION.
Definition: SDL3pp_log.h:432
constexpr LogCategory LOG_CATEGORY_GPU
GPU.
Definition: SDL3pp_log.h:451
std::function< void(LogCategory, LogPriority, const char *)> LogOutputCB
The prototype for the log output callback function.
Definition: SDL3pp_log.h:987
void LogInfo(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_INFO.
Definition: SDL3pp_log.h:838
constexpr LogCategory LOG_CATEGORY_SYSTEM
SYSTEM.
Definition: SDL3pp_log.h:439
void SetLogPriority(LogPriority priority) const
Set the priority of a particular log category.
Definition: SDL3pp_log.h:518
SDL_LogOutputFunction LogOutputFunction
The prototype for the log output callback function.
Definition: SDL3pp_log.h:968
void ResetLogPriorities()
Reset all priorities to default.
Definition: SDL3pp_log.h:557
constexpr LogPriority LOG_PRIORITY_ERROR
ERROR.
Definition: SDL3pp_log.h:97
constexpr LogPriority LOG_PRIORITY_COUNT
COUNT.
Definition: SDL3pp_log.h:102
void LogWarn(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_WARN.
Definition: SDL3pp_log.h:872
LogOutputFunction GetDefaultLogOutputFunction()
Get the default log output function.
Definition: SDL3pp_log.h:1001
constexpr LogPriority LOG_PRIORITY_VERBOSE
VERBOSE.
Definition: SDL3pp_log.h:88
constexpr LogPriority LOG_PRIORITY_INFO
INFO.
Definition: SDL3pp_log.h:93
constexpr LogCategory LOG_CATEGORY_RESERVED9
RESERVED9.
Definition: SDL3pp_log.h:474
constexpr LogCategory LOG_CATEGORY_RESERVED4
RESERVED4.
Definition: SDL3pp_log.h:459
constexpr LogPriority LOG_PRIORITY_TRACE
TRACE.
Definition: SDL3pp_log.h:86
constexpr LogCategory LOG_CATEGORY_CUSTOM
CUSTOM.
Definition: SDL3pp_log.h:480
constexpr LogCategory(LogCategoryRaw category=SDL_LOG_CATEGORY_APPLICATION)
Wraps LogCategory.
Definition: SDL3pp_log.h:137
constexpr LogCategory LOG_CATEGORY_RESERVED3
RESERVED3.
Definition: SDL3pp_log.h:456
constexpr LogCategory LOG_CATEGORY_RESERVED6
RESERVED6.
Definition: SDL3pp_log.h:465
Main include header for the SDL3pp library.
Stored Wrapper unique by type result callbacks.
Definition: SDL3pp_callbackWrapper.h:242