From f6c5e86d450e862bb57a606d5e17b9ce0103120f Mon Sep 17 00:00:00 2001 From: AndreasSasDev Date: Wed, 8 May 2024 20:04:26 +0200 Subject: [PATCH] Add Example Serilog as TraceLogCallback --- Examples.Logging/CustomSerilogLogging.cs | 66 ++++++++++++++++++++++++ Examples.Logging/Examples.Logging.csproj | 29 +++++++++++ Raylib-cs.sln | 6 +++ 3 files changed, 101 insertions(+) create mode 100644 Examples.Logging/CustomSerilogLogging.cs create mode 100644 Examples.Logging/Examples.Logging.csproj diff --git a/Examples.Logging/CustomSerilogLogging.cs b/Examples.Logging/CustomSerilogLogging.cs new file mode 100644 index 0000000..19ab074 --- /dev/null +++ b/Examples.Logging/CustomSerilogLogging.cs @@ -0,0 +1,66 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Serilog; +using static Raylib_cs.Raylib; + +namespace Examples.Logging; + +public static class CustomSerilogLogging +{ + private static readonly ILogger Logger = new LoggerConfiguration() + .WriteTo.Console() + .CreateLogger(); + + private static unsafe delegate* unmanaged[Cdecl] GetPointer() => &LogCallback; + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] + public static unsafe void LogCallback(int msgType, sbyte* text, sbyte* args) { + // Retrieve the log message from Raylib + string message = Raylib_cs.Logging.GetLogMessage(new IntPtr(text), new IntPtr(args)); + + // Handle the message through Serilog + Logger.Information(message); + } + + public static int Main() + { + Logger.Information("Using Serilog"); + + // Attach Logger to callback + //-------------------------------------------------------------------------------------- + unsafe { + SetTraceLogCallback(GetPointer()); + } + + Logger.Information("Logger has been set to Raylib-cs TraceLogCallback"); + + // raylib Window Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib custom Logger example - basic window"); + + // Main game loop + while (!WindowShouldClose()) + { + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + ClearBackground(Color.RayWhite); + + DrawText("Hello World!", 190, 200, 20, Color.Black); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); + //-------------------------------------------------------------------------------------- + + return 0; + } +} + diff --git a/Examples.Logging/Examples.Logging.csproj b/Examples.Logging/Examples.Logging.csproj new file mode 100644 index 0000000..3b9cf63 --- /dev/null +++ b/Examples.Logging/Examples.Logging.csproj @@ -0,0 +1,29 @@ + + + + Exe + net6.0 + true + 12 + enable + + + + + + + + + + + + + + + + + + + + + diff --git a/Raylib-cs.sln b/Raylib-cs.sln index 015e8d1..fd3fe74 100644 --- a/Raylib-cs.sln +++ b/Raylib-cs.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raylib-cs.Tests", "Raylib-c EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{1E2A5986-3F11-457F-AF97-D0C08D0060BA}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.Logging", "Examples.Logging\Examples.Logging.csproj", "{5081623C-4756-4AF0-A0CC-61FE394FF7B2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,9 @@ Global {1E2A5986-3F11-457F-AF97-D0C08D0060BA}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E2A5986-3F11-457F-AF97-D0C08D0060BA}.Release|Any CPU.ActiveCfg = Release|Any CPU {1E2A5986-3F11-457F-AF97-D0C08D0060BA}.Release|Any CPU.Build.0 = Release|Any CPU + {5081623C-4756-4AF0-A0CC-61FE394FF7B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5081623C-4756-4AF0-A0CC-61FE394FF7B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5081623C-4756-4AF0-A0CC-61FE394FF7B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5081623C-4756-4AF0-A0CC-61FE394FF7B2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal