Skip to content

Commit

Permalink
Simplifying internal Logging methods to route through a single method
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelFB committed Feb 11, 2024
1 parent 2ee9846 commit 18cefdb
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 109 deletions.
22 changes: 15 additions & 7 deletions Framework/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public enum FosterEventType : int
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FosterLogFn(IntPtr msg);
public delegate void FosterLogFn(IntPtr msg, int type);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FosterWriteFn(IntPtr context, IntPtr data, int size);
Expand Down Expand Up @@ -191,21 +191,29 @@ public static void FreeUTF8(IntPtr ptr)
Marshal.FreeHGlobal(ptr);
}

private static void HandleLog(IntPtr msg, int type)
{
switch (type)
{
case 0: Log.Info(msg); break;
case 1: Log.Warning(msg); break;
case 2: Log.Error(msg); break;
default: Log.Info(msg); break;
}
}

// need to store static references otherwise the delegates will get collected
private static readonly FosterLogFn logInfo = Log.Info;
private static readonly FosterLogFn logWarn = Log.Warning;
private static readonly FosterLogFn logErr = Log.Error;
private static readonly FosterLogFn handleLog = HandleLog;

static Platform()
{
// initialize logging immediately
FosterRegisterLogMethods(logInfo, logWarn, logErr, 0);
FosterSetLogCallback(handleLog, 0);
}

[DllImport(DLL)]
public static extern void FosterStartup(FosterDesc desc);
[DllImport(DLL)]
public static extern void FosterRegisterLogMethods(FosterLogFn info, FosterLogFn warn, FosterLogFn error, int level);
public static extern void FosterSetLogCallback(FosterLogFn logFn, int level);
[DllImport(DLL)]
public static extern void FosterBeginFrame();
[DllImport(DLL)]
Expand Down
7 changes: 6 additions & 1 deletion Framework/Utility/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ namespace Foster.Framework;

public static class Log
{
public delegate void LogFn(ReadOnlySpan<char> text);

// TODO: this can potentially be written to from other threads
// The user shouldn't have access to this directly as they need to lock
// around it. Instead there should be some safe way to iterate over it or
// request lines from it. Ideally without creating tons of garbage.
public static readonly StringBuilder Logs = new();

public delegate void LogFn(ReadOnlySpan<char> text);
public static LogFn? OnInfo;
public static LogFn? OnWarn;
public static LogFn? OnError;
Expand Down
21 changes: 14 additions & 7 deletions Platform/include/foster_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,19 @@ typedef enum FosterIndexFormat
FOSTER_INDEX_FORMAT_THIRTY_TWO
} FosterIndexFormat;

typedef enum FosterLogging
typedef enum FosterLogLevel
{
FOSTER_LOGGING_DEFAULT,
FOSTER_LOGGING_ALL,
FOSTER_LOGGING_NONE
} FosterLogging;
FOSTER_LOG_LEVEL_INFO,
FOSTER_LOG_LEVEL_WARNING,
FOSTER_LOG_LEVEL_ERROR
} FosterLogLevel;

typedef enum FosterLogFilter
{
FOSTER_LOG_FILTER_DEFAULT,
FOSTER_LOG_FILTER_VERBOSE,
FOSTER_LOG_FILTER_IGNORE_ALL
} FosterLogFilter;

typedef enum FosterImageWriteFormat
{
Expand All @@ -415,7 +422,7 @@ typedef enum FosterEventType
FOSTER_EVENT_TYPE_CONTROLLER_AXIS,
} FosterEventType;

typedef void (FOSTER_CALL * FosterLogFn)(const char *msg);
typedef void (FOSTER_CALL * FosterLogFn)(const char *msg, FosterLogLevel level);
typedef void (FOSTER_CALL * FosterWriteFn)(void *context, void *data, int size);

typedef struct FosterTexture FosterTexture;
Expand Down Expand Up @@ -566,7 +573,7 @@ extern "C" {

FOSTER_API void FosterStartup(FosterDesc desc);

FOSTER_API void FosterRegisterLogMethods(FosterLogFn logInfo, FosterLogFn logWarn, FosterLogFn logErr, FosterLogging level);
FOSTER_API void FosterSetLogCallback(FosterLogFn logFn, FosterLogFilter filter);

FOSTER_API void FosterBeginFrame();

Expand Down
Binary file modified Platform/libs/lib64/libFosterPlatform.so
Binary file not shown.
16 changes: 7 additions & 9 deletions Platform/src/foster_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "foster_renderer.h"
#include <SDL.h>

#define FOSTER_LOG_INFO(...) FosterLog(FOSTER_LOG_LEVEL_INFO, __VA_ARGS__)
#define FOSTER_LOG_WARN(...) FosterLog(FOSTER_LOG_LEVEL_WARNING, __VA_ARGS__)
#define FOSTER_LOG_ERROR(...) FosterLog(FOSTER_LOG_LEVEL_ERROR, __VA_ARGS__)

// foster global state
typedef struct
{
Expand All @@ -18,19 +22,13 @@ typedef struct
SDL_GameController* gamepads[FOSTER_MAX_CONTROLLERS];
char* clipboardText;
char* userPath;
FosterLogFn logInfo;
FosterLogFn logWarn;
FosterLogFn logError;
FosterLogging logLevel;
FosterLogFn logFn;
FosterLogFilter logFilter;
FosterBool polledMouseMovement;
} FosterState;

FosterState* FosterGetState();

void FosterLogInfo(const char* fmt, ...);

void FosterLogWarn(const char* fmt, ...);

void FosterLogError(const char* fmt, ...);
void FosterLog(FosterLogLevel level, const char* fmt, ...);

#endif
81 changes: 24 additions & 57 deletions Platform/src/foster_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
(((flags) & (flag)) != 0)

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

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

FosterKeys FosterGetKeyFromSDL(SDL_Scancode key);
FosterButtons FosterGetButtonFromSDL(SDL_GameControllerButton button);
Expand All @@ -38,18 +38,18 @@ void FosterLog_SDL(void *userdata, int category, SDL_LogPriority priority, const
{
case SDL_LOG_PRIORITY_VERBOSE:
case SDL_LOG_PRIORITY_DEBUG:
if (fstate.logLevel == FOSTER_LOGGING_ALL)
FosterLogInfo("%s", message);
if (fstate.logFilter == FOSTER_LOG_FILTER_VERBOSE)
FOSTER_LOG_INFO("%s", message);
break;
case SDL_LOG_PRIORITY_INFO:
FosterLogInfo("%s", message);
FOSTER_LOG_INFO("%s", message);
break;
case SDL_LOG_PRIORITY_WARN:
FosterLogWarn("%s", message);
FOSTER_LOG_WARN("%s", message);
break;
case SDL_LOG_PRIORITY_ERROR:
case SDL_LOG_PRIORITY_CRITICAL:
FosterLogError("%s", message);
FOSTER_LOG_ERROR("%s", message);
break;
}
}
Expand All @@ -67,18 +67,17 @@ void FosterStartup(FosterDesc desc)

if (fstate.desc.width <= 0 || fstate.desc.height <= 0)
{
FosterLogError("Foster invalid application width/height (%i, %i)", desc.width, desc.height);
FOSTER_LOG_ERROR("Foster invalid application width/height (%i, %i)", desc.width, desc.height);
return;
}

// Get SDL version
SDL_version version;
SDL_GetVersion(&version);
FosterLogInfo("SDL: v%i.%i.%i", version.major, version.minor, version.patch);
FOSTER_LOG_INFO("SDL: v%i.%i.%i", version.major, version.minor, version.patch);

// track SDL output
if (fstate.logLevel != FOSTER_LOGGING_NONE &&
(fstate.logInfo || fstate.logWarn || fstate.logError))
if (fstate.logFilter != FOSTER_LOG_FILTER_IGNORE_ALL && fstate.logFn)
{
SDL_LogSetOutputFunction(FosterLog_SDL, NULL);
}
Expand All @@ -96,14 +95,14 @@ void FosterStartup(FosterDesc desc)
int sdl_init_flags = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
if (SDL_Init(sdl_init_flags) != 0)
{
FosterLogError("Foster SDL_Init Failed: %s", SDL_GetError());
FOSTER_LOG_ERROR("Foster SDL_Init Failed: %s", SDL_GetError());
return;
}

// determine renderer type
if (!FosterGetDevice(fstate.desc.renderer, &fstate.device))
{
FosterLogError("Foster Failed to get Renderer Device");
FOSTER_LOG_ERROR("Foster Failed to get Renderer Device");
return;
}

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

if (fstate.window == NULL)
{
FosterLogError("Foster SDL_CreateWindow Failed: %s", SDL_GetError());
FOSTER_LOG_ERROR("Foster SDL_CreateWindow Failed: %s", SDL_GetError());
return;
}

Expand All @@ -133,7 +132,7 @@ void FosterStartup(FosterDesc desc)
{
if (!fstate.device.initialize())
{
FosterLogError("Foster Failed to initialize Renderer Device");
FOSTER_LOG_ERROR("Foster Failed to initialize Renderer Device");
fstate.running = false;
SDL_DestroyWindow(fstate.window);
return;
Expand All @@ -145,12 +144,10 @@ void FosterStartup(FosterDesc desc)
SDL_ShowWindow(fstate.window);
}

void FosterRegisterLogMethods(FosterLogFn logInfo, FosterLogFn logWarn, FosterLogFn logError, FosterLogging logLevel)
void FosterSetLogCallback(FosterLogFn logFn, FosterLogFilter logFiler)
{
fstate.logInfo = logInfo;
fstate.logWarn = logWarn;
fstate.logError = logError;
fstate.logLevel = logLevel;
fstate.logFn = logFn;
fstate.logFilter = logFiler;
}

void FosterBeginFrame()
Expand Down Expand Up @@ -433,7 +430,7 @@ void FosterSetFlags(FosterFlags flags)
{
int result = SDL_GL_SetSwapInterval(FOSTER_CHECK(flags, FOSTER_FLAG_VSYNC) ? 1 : 0);
if (result != 0)
FosterLogWarn("Setting V-Sync Failed: %s", SDL_GetError());
FOSTER_LOG_WARN("Setting V-Sync Failed: %s", SDL_GetError());
}

fstate.flags = flags;
Expand Down Expand Up @@ -488,15 +485,15 @@ FosterFont* FosterFontInit(unsigned char* data, int length)
{
if (stbtt_GetNumberOfFonts(data) <= 0)
{
FosterLogError("Unable to parse Font File");
FOSTER_LOG_ERROR("Unable to parse Font File");
return NULL;
}

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

if (stbtt_InitFont(info, data, 0) == 0)
{
FosterLogError("Unable to parse Font File");
FOSTER_LOG_ERROR("Unable to parse Font File");
SDL_free(info);
return NULL;
}
Expand Down Expand Up @@ -705,10 +702,10 @@ void FosterClear(FosterClearCommand* clear)
fstate.device.clear(clear);
}

void FosterLogInfo(const char* fmt, ...)
void FosterLog(FosterLogLevel level, const char* fmt, ...)
{
if (fstate.logLevel == FOSTER_LOGGING_NONE ||
fstate.logInfo == NULL)
if (fstate.logFilter == FOSTER_LOG_FILTER_IGNORE_ALL ||
fstate.logFn == NULL)
return;

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

fstate.logInfo(msg);
}

void FosterLogWarn(const char* fmt, ...)
{
if (fstate.logLevel == FOSTER_LOGGING_NONE ||
fstate.logWarn == NULL)
return;

char msg[FOSTER_MAX_MESSAGE_SIZE];
va_list ap;
va_start(ap, fmt);
SDL_vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);

fstate.logWarn(msg);
}

void FosterLogError(const char* fmt, ...)
{
if (fstate.logLevel == FOSTER_LOGGING_NONE ||
fstate.logError == NULL)
return;

char msg[FOSTER_MAX_MESSAGE_SIZE];
va_list ap;
va_start(ap, fmt);
SDL_vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);

fstate.logError(msg);
fstate.logFn(msg, level);
}

int FosterFindJoystickIndexSDL(SDL_Joystick** joysticks, SDL_JoystickID instanceID)
Expand Down
Loading

0 comments on commit 18cefdb

Please sign in to comment.