File tree Expand file tree Collapse file tree 2 files changed +48
-19
lines changed
Expand file tree Collapse file tree 2 files changed +48
-19
lines changed Original file line number Diff line number Diff line change 99#include <stdlib.h>
1010#include <string.h>
1111
12+ static constexpr size_t LOG_MESSAGE_CAPACITY = 256 ;
13+
14+ typedef struct {
15+ LogLevel level ;
16+ char text [LOG_MESSAGE_CAPACITY ];
17+ } LogMessage ;
18+
19+ typedef struct {
20+ size_t capacity ;
21+ LogMessage * messages ;
22+ size_t head ;
23+ size_t tail ;
24+ SDL_Mutex * mtx ;
25+ SDL_Condition * cond ;
26+ bool quit ;
27+ } LogQueue ;
28+
1229static bool logger_ready = false;
1330static LogLevel active_log_level = LogLevel_Info ;
1431static SDL_Thread * logger_thread = nullptr ;
@@ -65,6 +82,24 @@ static int logger_thread_fn([[maybe_unused]] void *const data)
6582 return 0 ;
6683}
6784
85+ bool LogLevel_from_str (const char * const str , LogLevel * const out )
86+ {
87+ if (strcmp (str , "trace" ) == 0 )
88+ * out = LogLevel_Trace ;
89+ else if (strcmp (str , "debug" ) == 0 )
90+ * out = LogLevel_Debug ;
91+ else if (strcmp (str , "info" ) == 0 )
92+ * out = LogLevel_Info ;
93+ else if (strcmp (str , "warn" ) == 0 )
94+ * out = LogLevel_Warn ;
95+ else if (strcmp (str , "error" ) == 0 )
96+ * out = LogLevel_Error ;
97+ else
98+ return false;
99+
100+ return true;
101+ }
102+
68103void logger_init (const LogLevel log_level )
69104{
70105 active_log_level = log_level ;
Original file line number Diff line number Diff line change 55#include <SDL3/SDL.h>
66#include <stdarg.h>
77#include <stddef.h>
8- #include <stdio.h>
9-
10- static constexpr size_t LOG_MESSAGE_CAPACITY = 256 ;
118
129typedef enum : u8 {
1310 LogLevel_Trace = 4 ,
@@ -17,22 +14,19 @@ typedef enum : u8 {
1714 LogLevel_Error = 0 ,
1815} LogLevel ;
1916
20- typedef struct {
21- LogLevel level ;
22- char text [LOG_MESSAGE_CAPACITY ];
23- } LogMessage ;
24-
25- typedef struct {
26- size_t capacity ;
27- LogMessage * messages ;
28- size_t head ;
29- size_t tail ;
30- SDL_Mutex * mtx ;
31- SDL_Condition * cond ;
32- bool quit ;
33- } LogQueue ;
34-
35- LogLevel LogLevel_from_str (const char * str );
17+ /**
18+ * \brief Converts a human-readable log level string into a LogLevel variant.
19+ *
20+ * For example, "debug" is converted to LogLevel_Debug.
21+ *
22+ * \param str a non-null string to convert into a LogLevel variant.
23+ * \param out the place to store the result at.
24+ *
25+ * \return whether the conversion was successful or not.
26+ *
27+ * \sa LogLevel
28+ */
29+ bool LogLevel_from_str (const char * str , LogLevel * out );
3630
3731/**
3832 * \brief Initializes logging.
You can’t perform that action at this time.
0 commit comments