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
60
62using LogCategoryRaw = SDL_LogCategory;
63
64// Forward decl
65struct LogCategory;
66
74
80using LogPriority = SDL_LogPriority;
81
83 SDL_LOG_PRIORITY_INVALID;
84
85constexpr LogPriority LOG_PRIORITY_TRACE = SDL_LOG_PRIORITY_TRACE;
86
88 SDL_LOG_PRIORITY_VERBOSE;
89
90constexpr LogPriority LOG_PRIORITY_DEBUG = SDL_LOG_PRIORITY_DEBUG;
91
92constexpr LogPriority LOG_PRIORITY_INFO = SDL_LOG_PRIORITY_INFO;
93
94constexpr LogPriority LOG_PRIORITY_WARN = SDL_LOG_PRIORITY_WARN;
95
96constexpr LogPriority LOG_PRIORITY_ERROR = SDL_LOG_PRIORITY_ERROR;
97
99 SDL_LOG_PRIORITY_CRITICAL;
100
101constexpr LogPriority LOG_PRIORITY_COUNT = SDL_LOG_PRIORITY_COUNT;
102
104
112
127{
128 LogCategoryRaw m_category;
129
130public:
136 constexpr LogCategory(
137 LogCategoryRaw category = SDL_LOG_CATEGORY_APPLICATION) noexcept
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 noexcept { 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
238 template<class... ARGS>
239 void LogMessage(LogPriority priority,
240 std::string_view fmt,
241 ARGS... args) const;
242
267 template<class... ARGS>
268 void LogTrace(std::string_view fmt, ARGS&&... args) const;
269
294 template<class... ARGS>
295 void LogVerbose(std::string_view fmt, ARGS&&... args) const;
296
322 template<class... ARGS>
323 void LogDebug(std::string_view fmt, ARGS&&... args) const;
324
350 template<class... ARGS>
351 void LogInfo(std::string_view fmt, ARGS&&... args) const;
352
378 template<class... ARGS>
379 void LogWarn(std::string_view fmt, ARGS&&... args) const;
380
406 template<class... ARGS>
407 void LogError(std::string_view fmt, ARGS&&... args) const;
408
434 template<class... ARGS>
435 void LogCritical(std::string_view fmt, ARGS&&... args) const;
436};
437
439 SDL_LOG_CATEGORY_APPLICATION;
440
441constexpr LogCategory LOG_CATEGORY_ERROR = SDL_LOG_CATEGORY_ERROR;
442
443constexpr LogCategory LOG_CATEGORY_ASSERT = SDL_LOG_CATEGORY_ASSERT;
444
445constexpr LogCategory LOG_CATEGORY_SYSTEM = SDL_LOG_CATEGORY_SYSTEM;
446
447constexpr LogCategory LOG_CATEGORY_AUDIO = SDL_LOG_CATEGORY_AUDIO;
448
449constexpr LogCategory LOG_CATEGORY_VIDEO = SDL_LOG_CATEGORY_VIDEO;
450
451constexpr LogCategory LOG_CATEGORY_RENDER = SDL_LOG_CATEGORY_RENDER;
452
453constexpr LogCategory LOG_CATEGORY_INPUT = SDL_LOG_CATEGORY_INPUT;
454
455constexpr LogCategory LOG_CATEGORY_TEST = SDL_LOG_CATEGORY_TEST;
456
457constexpr LogCategory LOG_CATEGORY_GPU = SDL_LOG_CATEGORY_GPU;
458
460 SDL_LOG_CATEGORY_RESERVED2;
461
463 SDL_LOG_CATEGORY_RESERVED3;
464
466 SDL_LOG_CATEGORY_RESERVED4;
467
469 SDL_LOG_CATEGORY_RESERVED5;
470
472 SDL_LOG_CATEGORY_RESERVED6;
473
475 SDL_LOG_CATEGORY_RESERVED7;
476
478 SDL_LOG_CATEGORY_RESERVED8;
479
481 SDL_LOG_CATEGORY_RESERVED9;
482
484 SDL_LOG_CATEGORY_RESERVED10;
485
486constexpr LogCategory LOG_CATEGORY_CUSTOM = SDL_LOG_CATEGORY_CUSTOM;
487
500inline void SetLogPriorities(LogPriority priority)
501{
502 SDL_SetLogPriorities(priority);
503}
504
519inline void SetLogPriority(int category, LogPriority priority)
520{
521 SDL_SetLogPriority(category, priority);
522}
523
524inline void LogCategory::SetLogPriority(LogPriority priority) const
525{
526 SDL::SetLogPriority(m_category, priority);
527}
528
541inline LogPriority GetLogPriority(int category)
542{
543 return SDL_GetLogPriority(category);
544}
545
547{
548 return SDL::GetLogPriority(m_category);
549}
550
563inline void ResetLogPriorities() { SDL_ResetLogPriorities(); }
564
586inline void SetLogPriorityPrefix(LogPriority priority, StringParam prefix)
587{
588 CheckError(SDL_SetLogPriorityPrefix(priority, prefix));
589}
590
613inline void LogUnformatted(LogCategory category,
614 LogPriority priority,
615 StringParam message)
616{
617 SDL_LogMessage(category, priority, "%s", static_cast<const char*>(message));
618}
619
639inline void LogUnformatted(StringParam message)
640{
641 SDL_Log("%s", static_cast<const char*>(message));
642}
643
671template<class... ARGS>
672inline void Log(std::string_view fmt, ARGS&&... args)
673{
674 LOG_CATEGORY_APPLICATION.LogInfo(fmt, std::forward<ARGS>(args)...);
675}
676
699template<class... ARGS>
700inline void LogMessage(LogCategory category,
701 LogPriority priority,
702 std::string_view fmt,
703 ARGS... args)
704{
706 category, priority, std::vformat(fmt, std::make_format_args(args...)));
707}
708
709template<class... ARGS>
711 std::string_view fmt,
712 ARGS... args) const
713{
714 SDL::LogMessage(m_category, priority, fmt, args...);
715}
716
739template<class... ARGS>
740inline void LogTrace(LogCategory category, std::string_view fmt, ARGS&&... args)
741{
742 LogMessage(category, LOG_PRIORITY_TRACE, fmt, args...);
743}
744
745template<class... ARGS>
746inline void LogCategory::LogTrace(std::string_view fmt, ARGS&&... args) const
747{
748 SDL::LogTrace(m_category, fmt, args...);
749}
750
773template<class... ARGS>
774inline void LogVerbose(LogCategory category,
775 std::string_view fmt,
776 ARGS&&... args)
777{
778 LogMessage(category, LOG_PRIORITY_VERBOSE, fmt, args...);
779}
780
781template<class... ARGS>
782inline void LogCategory::LogVerbose(std::string_view fmt, ARGS&&... args) const
783{
784 SDL::LogVerbose(m_category, fmt, args...);
785}
786
809template<class... ARGS>
810inline void LogDebug(LogCategory category, std::string_view fmt, ARGS&&... args)
811{
812 LogMessage(category, LOG_PRIORITY_DEBUG, fmt, args...);
813}
814
815template<class... ARGS>
816inline void LogCategory::LogDebug(std::string_view fmt, ARGS&&... args) const
817{
818 SDL::LogDebug(m_category, fmt, args...);
819}
820
843template<class... ARGS>
844inline void LogInfo(LogCategory category, std::string_view fmt, ARGS&&... args)
845{
846 LogMessage(category, LOG_PRIORITY_INFO, fmt, args...);
847}
848
849template<class... ARGS>
850inline void LogCategory::LogInfo(std::string_view fmt, ARGS&&... args) const
851{
852 SDL::LogInfo(m_category, fmt, args...);
853}
854
877template<class... ARGS>
878inline void LogWarn(LogCategory category, std::string_view fmt, ARGS&&... args)
879{
880 LogMessage(category, SDL_LOG_PRIORITY_WARN, fmt, args...);
881}
882
883template<class... ARGS>
884inline void LogCategory::LogWarn(std::string_view fmt, ARGS&&... args) const
885{
886 SDL::LogWarn(m_category, fmt, args...);
887}
888
911template<class... ARGS>
912inline void LogError(LogCategory category, std::string_view fmt, ARGS&&... args)
913{
914 LogMessage(category, SDL_LOG_PRIORITY_ERROR, fmt, args...);
915}
916
917template<class... ARGS>
918inline void LogCategory::LogError(std::string_view fmt, ARGS&&... args) const
919{
920 SDL::LogError(m_category, fmt, args...);
921}
922
945template<class... ARGS>
946inline void LogCritical(LogCategory category,
947 std::string_view fmt,
948 ARGS&&... args)
949{
950 LogMessage(category, SDL_LOG_PRIORITY_CRITICAL, fmt, args...);
951}
952
953template<class... ARGS>
954inline void LogCategory::LogCritical(std::string_view fmt, ARGS&&... args) const
955{
956 SDL::LogCritical(m_category, fmt, args...);
957}
958
973using LogOutputFunction = void(SDLCALL*)(void* userdata,
974 int category,
975 LogPriority priority,
976 const char* message);
977
996 void(int category, LogPriority priority, const char* message)>;
997
1012{
1013 return SDL_GetDefaultLogOutputFunction();
1014}
1015
1030inline void GetLogOutputFunction(LogOutputFunction* callback, void** userdata)
1031{
1032 SDL_GetLogOutputFunction(callback, userdata);
1033}
1034
1049inline void SetLogOutputFunction(LogOutputFunction callback, void* userdata)
1050{
1051 SDL_SetLogOutputFunction(callback, userdata);
1052}
1053
1068{
1069 SetLogOutputFunction(callback.wrapper, callback.data);
1070}
1071
1083{
1085}
1086
1088
1089} // namespace SDL
1090
1091#endif /* SDL3PP_LOG_H_ */
The predefined log categories.
Definition SDL3pp_log.h:127
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:58
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
void SetLogPriorities(LogPriority priority)
Set the priority of all log categories.
Definition SDL3pp_log.h:500
constexpr LogCategory LOG_CATEGORY_ASSERT
ASSERT.
Definition SDL3pp_log.h:443
constexpr LogCategory LOG_CATEGORY_AUDIO
AUDIO.
Definition SDL3pp_log.h:447
void LogWarn(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_WARN.
Definition SDL3pp_log.h:884
constexpr LogCategory LOG_CATEGORY_VIDEO
VIDEO.
Definition SDL3pp_log.h:449
SDL_LogCategory LogCategoryRaw
Alias to raw representation for LogCategory.
Definition SDL3pp_log.h:62
constexpr LogCategory LOG_CATEGORY_RESERVED10
RESERVED10.
Definition SDL3pp_log.h:483
void LogVerbose(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_VERBOSE.
Definition SDL3pp_log.h:774
void SetLogPriorityPrefix(LogPriority priority, StringParam prefix)
Set the text prepended to log messages of a given priority.
Definition SDL3pp_log.h:586
LogPriority GetLogPriority(int category)
Get the priority of a particular log category.
Definition SDL3pp_log.h:541
void SetLogPriority(int category, LogPriority priority)
Set the priority of a particular log category.
Definition SDL3pp_log.h:519
void LogTrace(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_TRACE.
Definition SDL3pp_log.h:740
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:82
void SetLogOutputFunction(LogOutputFunction callback, void *userdata)
Replace the default log output function with one of your own.
Definition SDL3pp_log.h:1049
constexpr LogCategory LOG_CATEGORY_INPUT
INPUT.
Definition SDL3pp_log.h:453
constexpr LogCategory LOG_CATEGORY_ERROR
ERROR.
Definition SDL3pp_log.h:441
void LogInfo(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_INFO.
Definition SDL3pp_log.h:850
constexpr LogCategory(int category)
Wraps LogCategory.
Definition SDL3pp_log.h:147
constexpr LogCategory LOG_CATEGORY_RESERVED5
RESERVED5.
Definition SDL3pp_log.h:468
void LogVerbose(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_VERBOSE.
Definition SDL3pp_log.h:782
void LogCritical(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_CRITICAL.
Definition SDL3pp_log.h:946
void LogTrace(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_TRACE.
Definition SDL3pp_log.h:746
void ResetLogOutputFunction()
Replace the current log output function with the default one.
Definition SDL3pp_log.h:1082
constexpr LogPriority LOG_PRIORITY_CRITICAL
CRITICAL.
Definition SDL3pp_log.h:98
void LogCritical(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_CRITICAL.
Definition SDL3pp_log.h:954
void LogDebug(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_DEBUG.
Definition SDL3pp_log.h:810
constexpr LogCategory LOG_CATEGORY_RESERVED8
RESERVED8.
Definition SDL3pp_log.h:477
LogPriority GetLogPriority() const
Get the priority of a particular log category.
Definition SDL3pp_log.h:546
MakeFrontCallback< void(int category, LogPriority priority, const char *message)> LogOutputCB
The prototype for the log output callback function.
Definition SDL3pp_log.h:995
void LogError(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_ERROR.
Definition SDL3pp_log.h:918
constexpr LogCategory LOG_CATEGORY_RESERVED7
RESERVED7.
Definition SDL3pp_log.h:474
constexpr LogPriority LOG_PRIORITY_WARN
WARN.
Definition SDL3pp_log.h:94
void LogUnformatted(LogCategory category, LogPriority priority, StringParam message)
Log an unformatted message with LOG_CATEGORY_APPLICATION and LOG_PRIORITY_INFO.
Definition SDL3pp_log.h:613
void LogMessage(LogPriority priority, std::string_view fmt, ARGS... args) const
Log a message with the specified priority.
Definition SDL3pp_log.h:710
void LogDebug(std::string_view fmt, ARGS &&... args) const
Log a message with LOG_PRIORITY_DEBUG.
Definition SDL3pp_log.h:816
void LogError(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_ERROR.
Definition SDL3pp_log.h:912
constexpr LogCategory LOG_CATEGORY_TEST
TEST.
Definition SDL3pp_log.h:455
constexpr LogCategory LOG_CATEGORY_RESERVED2
RESERVED2.
Definition SDL3pp_log.h:459
constexpr LogPriority LOG_PRIORITY_DEBUG
DEBUG.
Definition SDL3pp_log.h:90
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:700
void Log(std::string_view fmt, ARGS &&... args)
Log a message with LOG_CATEGORY_APPLICATION and LOG_PRIORITY_INFO.
Definition SDL3pp_log.h:672
constexpr LogCategory LOG_CATEGORY_RENDER
RENDER.
Definition SDL3pp_log.h:451
constexpr LogCategory LOG_CATEGORY_APPLICATION
APPLICATION.
Definition SDL3pp_log.h:438
void GetLogOutputFunction(LogOutputFunction *callback, void **userdata)
Get the current log output function.
Definition SDL3pp_log.h:1030
constexpr LogCategory LOG_CATEGORY_GPU
GPU.
Definition SDL3pp_log.h:457
void LogInfo(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_INFO.
Definition SDL3pp_log.h:844
constexpr LogCategory LOG_CATEGORY_SYSTEM
SYSTEM.
Definition SDL3pp_log.h:445
void SetLogPriority(LogPriority priority) const
Set the priority of a particular log category.
Definition SDL3pp_log.h:524
void(SDLCALL *)(void *userdata, int category, LogPriority priority, const char *message) LogOutputFunction
The prototype for the log output callback function.
Definition SDL3pp_log.h:973
void ResetLogPriorities()
Reset all priorities to default.
Definition SDL3pp_log.h:563
constexpr LogPriority LOG_PRIORITY_ERROR
ERROR.
Definition SDL3pp_log.h:96
constexpr LogPriority LOG_PRIORITY_COUNT
COUNT.
Definition SDL3pp_log.h:101
void LogWarn(LogCategory category, std::string_view fmt, ARGS &&... args)
Log a message with LOG_PRIORITY_WARN.
Definition SDL3pp_log.h:878
SDL_LogPriority LogPriority
The predefined log priorities.
Definition SDL3pp_log.h:80
LogOutputFunction GetDefaultLogOutputFunction()
Get the default log output function.
Definition SDL3pp_log.h:1011
constexpr LogPriority LOG_PRIORITY_VERBOSE
VERBOSE.
Definition SDL3pp_log.h:87
constexpr LogCategory(LogCategoryRaw category=SDL_LOG_CATEGORY_APPLICATION) noexcept
Wraps LogCategory.
Definition SDL3pp_log.h:136
constexpr LogPriority LOG_PRIORITY_INFO
INFO.
Definition SDL3pp_log.h:92
constexpr LogCategory LOG_CATEGORY_RESERVED9
RESERVED9.
Definition SDL3pp_log.h:480
constexpr LogCategory LOG_CATEGORY_RESERVED4
RESERVED4.
Definition SDL3pp_log.h:465
constexpr LogPriority LOG_PRIORITY_TRACE
TRACE.
Definition SDL3pp_log.h:85
constexpr LogCategory LOG_CATEGORY_CUSTOM
CUSTOM.
Definition SDL3pp_log.h:486
constexpr LogCategory LOG_CATEGORY_RESERVED3
RESERVED3.
Definition SDL3pp_log.h:462
constexpr LogCategory LOG_CATEGORY_RESERVED6
RESERVED6.
Definition SDL3pp_log.h:471
Main include header for the SDL3pp library.
Definition SDL3pp_callbackWrapper.h:169