Skip to content

Commit 18cefdb

Browse files
committed
Simplifying internal Logging methods to route through a single method
1 parent 2ee9846 commit 18cefdb

File tree

7 files changed

+94
-109
lines changed

7 files changed

+94
-109
lines changed

Framework/Platform.cs

+15-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public enum FosterEventType : int
3333
}
3434

3535
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
36-
public delegate void FosterLogFn(IntPtr msg);
36+
public delegate void FosterLogFn(IntPtr msg, int type);
3737

3838
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
3939
public delegate void FosterWriteFn(IntPtr context, IntPtr data, int size);
@@ -191,21 +191,29 @@ public static void FreeUTF8(IntPtr ptr)
191191
Marshal.FreeHGlobal(ptr);
192192
}
193193

194+
private static void HandleLog(IntPtr msg, int type)
195+
{
196+
switch (type)
197+
{
198+
case 0: Log.Info(msg); break;
199+
case 1: Log.Warning(msg); break;
200+
case 2: Log.Error(msg); break;
201+
default: Log.Info(msg); break;
202+
}
203+
}
204+
194205
// need to store static references otherwise the delegates will get collected
195-
private static readonly FosterLogFn logInfo = Log.Info;
196-
private static readonly FosterLogFn logWarn = Log.Warning;
197-
private static readonly FosterLogFn logErr = Log.Error;
206+
private static readonly FosterLogFn handleLog = HandleLog;
198207

199208
static Platform()
200209
{
201-
// initialize logging immediately
202-
FosterRegisterLogMethods(logInfo, logWarn, logErr, 0);
210+
FosterSetLogCallback(handleLog, 0);
203211
}
204212

205213
[DllImport(DLL)]
206214
public static extern void FosterStartup(FosterDesc desc);
207215
[DllImport(DLL)]
208-
public static extern void FosterRegisterLogMethods(FosterLogFn info, FosterLogFn warn, FosterLogFn error, int level);
216+
public static extern void FosterSetLogCallback(FosterLogFn logFn, int level);
209217
[DllImport(DLL)]
210218
public static extern void FosterBeginFrame();
211219
[DllImport(DLL)]

Framework/Utility/Log.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ namespace Foster.Framework;
44

55
public static class Log
66
{
7+
public delegate void LogFn(ReadOnlySpan<char> text);
8+
9+
// TODO: this can potentially be written to from other threads
10+
// The user shouldn't have access to this directly as they need to lock
11+
// around it. Instead there should be some safe way to iterate over it or
12+
// request lines from it. Ideally without creating tons of garbage.
713
public static readonly StringBuilder Logs = new();
814

9-
public delegate void LogFn(ReadOnlySpan<char> text);
1015
public static LogFn? OnInfo;
1116
public static LogFn? OnWarn;
1217
public static LogFn? OnError;

Platform/include/foster_platform.h

+14-7
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,19 @@ typedef enum FosterIndexFormat
387387
FOSTER_INDEX_FORMAT_THIRTY_TWO
388388
} FosterIndexFormat;
389389

390-
typedef enum FosterLogging
390+
typedef enum FosterLogLevel
391391
{
392-
FOSTER_LOGGING_DEFAULT,
393-
FOSTER_LOGGING_ALL,
394-
FOSTER_LOGGING_NONE
395-
} FosterLogging;
392+
FOSTER_LOG_LEVEL_INFO,
393+
FOSTER_LOG_LEVEL_WARNING,
394+
FOSTER_LOG_LEVEL_ERROR
395+
} FosterLogLevel;
396+
397+
typedef enum FosterLogFilter
398+
{
399+
FOSTER_LOG_FILTER_DEFAULT,
400+
FOSTER_LOG_FILTER_VERBOSE,
401+
FOSTER_LOG_FILTER_IGNORE_ALL
402+
} FosterLogFilter;
396403

397404
typedef enum FosterImageWriteFormat
398405
{
@@ -415,7 +422,7 @@ typedef enum FosterEventType
415422
FOSTER_EVENT_TYPE_CONTROLLER_AXIS,
416423
} FosterEventType;
417424

418-
typedef void (FOSTER_CALL * FosterLogFn)(const char *msg);
425+
typedef void (FOSTER_CALL * FosterLogFn)(const char *msg, FosterLogLevel level);
419426
typedef void (FOSTER_CALL * FosterWriteFn)(void *context, void *data, int size);
420427

421428
typedef struct FosterTexture FosterTexture;
@@ -566,7 +573,7 @@ extern "C" {
566573

567574
FOSTER_API void FosterStartup(FosterDesc desc);
568575

569-
FOSTER_API void FosterRegisterLogMethods(FosterLogFn logInfo, FosterLogFn logWarn, FosterLogFn logErr, FosterLogging level);
576+
FOSTER_API void FosterSetLogCallback(FosterLogFn logFn, FosterLogFilter filter);
570577

571578
FOSTER_API void FosterBeginFrame();
572579

153 KB
Binary file not shown.

Platform/src/foster_internal.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "foster_renderer.h"
66
#include <SDL.h>
77

8+
#define FOSTER_LOG_INFO(...) FosterLog(FOSTER_LOG_LEVEL_INFO, __VA_ARGS__)
9+
#define FOSTER_LOG_WARN(...) FosterLog(FOSTER_LOG_LEVEL_WARNING, __VA_ARGS__)
10+
#define FOSTER_LOG_ERROR(...) FosterLog(FOSTER_LOG_LEVEL_ERROR, __VA_ARGS__)
11+
812
// foster global state
913
typedef struct
1014
{
@@ -18,19 +22,13 @@ typedef struct
1822
SDL_GameController* gamepads[FOSTER_MAX_CONTROLLERS];
1923
char* clipboardText;
2024
char* userPath;
21-
FosterLogFn logInfo;
22-
FosterLogFn logWarn;
23-
FosterLogFn logError;
24-
FosterLogging logLevel;
25+
FosterLogFn logFn;
26+
FosterLogFilter logFilter;
2527
FosterBool polledMouseMovement;
2628
} FosterState;
2729

2830
FosterState* FosterGetState();
2931

30-
void FosterLogInfo(const char* fmt, ...);
31-
32-
void FosterLogWarn(const char* fmt, ...);
33-
34-
void FosterLogError(const char* fmt, ...);
32+
void FosterLog(FosterLogLevel level, const char* fmt, ...);
3533

3634
#endif

Platform/src/foster_platform.c

+24-57
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
(((flags) & (flag)) != 0)
1414

1515
#define FOSTER_ASSERT_RUNNING_RET(func, ret) \
16-
do { if (!fstate.running) { FosterLogError("Failed '%s', Foster is not running", #func); return ret; } } while(0)
16+
do { if (!fstate.running) { FOSTER_LOG_ERROR("Failed '%s', Foster is not running", #func); return ret; } } while(0)
1717

1818
#define FOSTER_ASSERT_RUNNING(func) \
19-
do { if (!fstate.running) { FosterLogError("Failed '%s', Foster is not running", #func); return; } } while(0)
19+
do { if (!fstate.running) { FOSTER_LOG_ERROR("Failed '%s', Foster is not running", #func); return; } } while(0)
2020

2121
FosterKeys FosterGetKeyFromSDL(SDL_Scancode key);
2222
FosterButtons FosterGetButtonFromSDL(SDL_GameControllerButton button);
@@ -38,18 +38,18 @@ void FosterLog_SDL(void *userdata, int category, SDL_LogPriority priority, const
3838
{
3939
case SDL_LOG_PRIORITY_VERBOSE:
4040
case SDL_LOG_PRIORITY_DEBUG:
41-
if (fstate.logLevel == FOSTER_LOGGING_ALL)
42-
FosterLogInfo("%s", message);
41+
if (fstate.logFilter == FOSTER_LOG_FILTER_VERBOSE)
42+
FOSTER_LOG_INFO("%s", message);
4343
break;
4444
case SDL_LOG_PRIORITY_INFO:
45-
FosterLogInfo("%s", message);
45+
FOSTER_LOG_INFO("%s", message);
4646
break;
4747
case SDL_LOG_PRIORITY_WARN:
48-
FosterLogWarn("%s", message);
48+
FOSTER_LOG_WARN("%s", message);
4949
break;
5050
case SDL_LOG_PRIORITY_ERROR:
5151
case SDL_LOG_PRIORITY_CRITICAL:
52-
FosterLogError("%s", message);
52+
FOSTER_LOG_ERROR("%s", message);
5353
break;
5454
}
5555
}
@@ -67,18 +67,17 @@ void FosterStartup(FosterDesc desc)
6767

6868
if (fstate.desc.width <= 0 || fstate.desc.height <= 0)
6969
{
70-
FosterLogError("Foster invalid application width/height (%i, %i)", desc.width, desc.height);
70+
FOSTER_LOG_ERROR("Foster invalid application width/height (%i, %i)", desc.width, desc.height);
7171
return;
7272
}
7373

7474
// Get SDL version
7575
SDL_version version;
7676
SDL_GetVersion(&version);
77-
FosterLogInfo("SDL: v%i.%i.%i", version.major, version.minor, version.patch);
77+
FOSTER_LOG_INFO("SDL: v%i.%i.%i", version.major, version.minor, version.patch);
7878

7979
// track SDL output
80-
if (fstate.logLevel != FOSTER_LOGGING_NONE &&
81-
(fstate.logInfo || fstate.logWarn || fstate.logError))
80+
if (fstate.logFilter != FOSTER_LOG_FILTER_IGNORE_ALL && fstate.logFn)
8281
{
8382
SDL_LogSetOutputFunction(FosterLog_SDL, NULL);
8483
}
@@ -96,14 +95,14 @@ void FosterStartup(FosterDesc desc)
9695
int sdl_init_flags = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
9796
if (SDL_Init(sdl_init_flags) != 0)
9897
{
99-
FosterLogError("Foster SDL_Init Failed: %s", SDL_GetError());
98+
FOSTER_LOG_ERROR("Foster SDL_Init Failed: %s", SDL_GetError());
10099
return;
101100
}
102101

103102
// determine renderer type
104103
if (!FosterGetDevice(fstate.desc.renderer, &fstate.device))
105104
{
106-
FosterLogError("Foster Failed to get Renderer Device");
105+
FOSTER_LOG_ERROR("Foster Failed to get Renderer Device");
107106
return;
108107
}
109108

@@ -122,7 +121,7 @@ void FosterStartup(FosterDesc desc)
122121

123122
if (fstate.window == NULL)
124123
{
125-
FosterLogError("Foster SDL_CreateWindow Failed: %s", SDL_GetError());
124+
FOSTER_LOG_ERROR("Foster SDL_CreateWindow Failed: %s", SDL_GetError());
126125
return;
127126
}
128127

@@ -133,7 +132,7 @@ void FosterStartup(FosterDesc desc)
133132
{
134133
if (!fstate.device.initialize())
135134
{
136-
FosterLogError("Foster Failed to initialize Renderer Device");
135+
FOSTER_LOG_ERROR("Foster Failed to initialize Renderer Device");
137136
fstate.running = false;
138137
SDL_DestroyWindow(fstate.window);
139138
return;
@@ -145,12 +144,10 @@ void FosterStartup(FosterDesc desc)
145144
SDL_ShowWindow(fstate.window);
146145
}
147146

148-
void FosterRegisterLogMethods(FosterLogFn logInfo, FosterLogFn logWarn, FosterLogFn logError, FosterLogging logLevel)
147+
void FosterSetLogCallback(FosterLogFn logFn, FosterLogFilter logFiler)
149148
{
150-
fstate.logInfo = logInfo;
151-
fstate.logWarn = logWarn;
152-
fstate.logError = logError;
153-
fstate.logLevel = logLevel;
149+
fstate.logFn = logFn;
150+
fstate.logFilter = logFiler;
154151
}
155152

156153
void FosterBeginFrame()
@@ -433,7 +430,7 @@ void FosterSetFlags(FosterFlags flags)
433430
{
434431
int result = SDL_GL_SetSwapInterval(FOSTER_CHECK(flags, FOSTER_FLAG_VSYNC) ? 1 : 0);
435432
if (result != 0)
436-
FosterLogWarn("Setting V-Sync Failed: %s", SDL_GetError());
433+
FOSTER_LOG_WARN("Setting V-Sync Failed: %s", SDL_GetError());
437434
}
438435

439436
fstate.flags = flags;
@@ -488,15 +485,15 @@ FosterFont* FosterFontInit(unsigned char* data, int length)
488485
{
489486
if (stbtt_GetNumberOfFonts(data) <= 0)
490487
{
491-
FosterLogError("Unable to parse Font File");
488+
FOSTER_LOG_ERROR("Unable to parse Font File");
492489
return NULL;
493490
}
494491

495492
stbtt_fontinfo* info = (stbtt_fontinfo*)SDL_malloc(sizeof(stbtt_fontinfo));
496493

497494
if (stbtt_InitFont(info, data, 0) == 0)
498495
{
499-
FosterLogError("Unable to parse Font File");
496+
FOSTER_LOG_ERROR("Unable to parse Font File");
500497
SDL_free(info);
501498
return NULL;
502499
}
@@ -705,10 +702,10 @@ void FosterClear(FosterClearCommand* clear)
705702
fstate.device.clear(clear);
706703
}
707704

708-
void FosterLogInfo(const char* fmt, ...)
705+
void FosterLog(FosterLogLevel level, const char* fmt, ...)
709706
{
710-
if (fstate.logLevel == FOSTER_LOGGING_NONE ||
711-
fstate.logInfo == NULL)
707+
if (fstate.logFilter == FOSTER_LOG_FILTER_IGNORE_ALL ||
708+
fstate.logFn == NULL)
712709
return;
713710

714711
char msg[FOSTER_MAX_MESSAGE_SIZE];
@@ -717,37 +714,7 @@ void FosterLogInfo(const char* fmt, ...)
717714
SDL_vsnprintf(msg, sizeof(msg), fmt, ap);
718715
va_end(ap);
719716

720-
fstate.logInfo(msg);
721-
}
722-
723-
void FosterLogWarn(const char* fmt, ...)
724-
{
725-
if (fstate.logLevel == FOSTER_LOGGING_NONE ||
726-
fstate.logWarn == NULL)
727-
return;
728-
729-
char msg[FOSTER_MAX_MESSAGE_SIZE];
730-
va_list ap;
731-
va_start(ap, fmt);
732-
SDL_vsnprintf(msg, sizeof(msg), fmt, ap);
733-
va_end(ap);
734-
735-
fstate.logWarn(msg);
736-
}
737-
738-
void FosterLogError(const char* fmt, ...)
739-
{
740-
if (fstate.logLevel == FOSTER_LOGGING_NONE ||
741-
fstate.logError == NULL)
742-
return;
743-
744-
char msg[FOSTER_MAX_MESSAGE_SIZE];
745-
va_list ap;
746-
va_start(ap, fmt);
747-
SDL_vsnprintf(msg, sizeof(msg), fmt, ap);
748-
va_end(ap);
749-
750-
fstate.logError(msg);
717+
fstate.logFn(msg, level);
751718
}
752719

753720
int FosterFindJoystickIndexSDL(SDL_Joystick** joysticks, SDL_JoystickID instanceID)

0 commit comments

Comments
 (0)