From e3211fded27eebc2d8012baea72eb71cbd52ca8c Mon Sep 17 00:00:00 2001 From: Rostislav Kirillov Date: Sat, 6 Jan 2024 05:30:44 +0400 Subject: [PATCH] feat: logs with time --- src/Backend/Debug/DebugBackend.bf | 4 +- src/Misc/Logger.bf | 77 +++++++++++++++++++++++-------- src/Win32/Memory.bf | 15 ++++++ 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/Backend/Debug/DebugBackend.bf b/src/Backend/Debug/DebugBackend.bf index b2acf67..ca06637 100644 --- a/src/Backend/Debug/DebugBackend.bf +++ b/src/Backend/Debug/DebugBackend.bf @@ -205,8 +205,8 @@ namespace RfgNetworking.Backend.Debug SteamControllerWrapper.Init(steamApiContext.Controller, steamApiContext.Controller.Vtable); steamApiContext.Controller = &SteamControllerWrapper; } - Logger.Write(scope $"SW_CCSys_DynamicInit(CallbackCounterAndContext* callbackCounterAndContext: 0x{(int)(void*)callbackCounterAndContext:X})"); - Logger.Write(scope $" -> CSteamAPIContext*(0x{(int)(void*)steamApiContext:X})"); + Logger.WriteLine(scope $"SW_CCSys_DynamicInit(CallbackCounterAndContext* callbackCounterAndContext: 0x{(int)(void*)callbackCounterAndContext:X})"); + Logger.WriteLine(scope $" -> CSteamAPIContext*(0x{(int)(void*)steamApiContext:X})"); Logger.Flush(); Logger.WriteLine(""); } diff --git a/src/Misc/Logger.bf b/src/Misc/Logger.bf index 0739d83..279a914 100644 --- a/src/Misc/Logger.bf +++ b/src/Misc/Logger.bf @@ -7,40 +7,81 @@ namespace RfgNetworking.Misc { public static bool AutoFlush = true; - private static FileStream _fileStream; - private static StreamWriter _streamWriter = null; + private static Windows.FileHandle handle; public static void Init() { - _fileStream = new .(); - _fileStream.Open("_RfgNetworkApiWrapper.log", FileMode.Create, .Write, .Read); - _streamWriter = new .(_fileStream, .UTF8, 4096, false); + var name = "_RfgNetworkApiWrapper.log"; + + handle = Windows.SafeCreateFile(name, Windows.GENERIC_WRITE, FileShare.ReadWrite, null, FileMode.OpenOrCreate, 128, Windows.Handle.NullHandle); } public static void Shutdown() { - _streamWriter.WriteLine("\n********************"); - _streamWriter.WriteLine("Log closed!"); - _streamWriter.WriteLine("********************"); - delete _streamWriter; - delete _fileStream; + WriteLine("********************"); + WriteLine("Log closed"); + WriteLine("********************"); + } + private static void DumbFormat(String s, RfgNetworking.Win32.SystemTime* t){ + var sMonth = scope String(); + t.Month.ToString(sMonth); + if(sMonth.Length == 1){ + sMonth.Insert(0, "0"); + } + var sDay= scope String(); + t.Day.ToString(sDay); + if(sDay.Length == 1){ + sDay.Insert(0, "0"); + } + var sHour= scope String(); + t.Hour.ToString(sHour); + if(sHour.Length == 1){ + sHour.Insert(0, "0"); + } + var sMinute= scope String(); + t.Minute.ToString(sMinute); + if(sMinute.Length == 1){ + sMinute.Insert(0, "0"); + } + var sSecond= scope String(); + t.Second.ToString(sSecond); + if(sSecond.Length == 1){ + sSecond.Insert(0, "0"); + } + + s.AppendF("{}-{}-{}-{}:{}:{}", t.Year, sMonth, sDay, sHour, sMinute, sSecond); + return; + } + public static void WriteLine(StringView fmt, params Object[] args) { - _streamWriter.WriteLine(fmt, params args); - if (AutoFlush) - _streamWriter.Flush(); - } + //var d = scope String(); + //var c = new System.Globalization.CultureInfo("en-US"); + //System.Globalization.CultureInfo.mDefaultCultureInfo = c; + //System.Globalization.CultureInfo.CurrentCulture = c; + //System.Globalization.CultureInfo.[Friend]sUserDefaultCulture = c; - public static void Write(StringView fmt, params Object[] args) - { - _streamWriter.Write(fmt, params args); + + var t = new RfgNetworking.Win32.SystemTime(); + RfgNetworking.Win32.Win32.GetLocalTime(t); + var str = scope String(); + var time = scope String(); + DumbFormat(time, t); + str.AppendF("[{}][sw_api] ", time); + str.AppendF(fmt, args); + str.Append("\n"); + var strPtr = (char8*)str; + var ptr = (uint8*)strPtr; + + int32 x = 0; + Windows.SetFilePointer(handle, 0, null, Windows.FILE_END); + Windows.WriteFile(handle, ptr, str.Length, out x, null); } public static void Flush() { - _streamWriter.Flush(); } } } \ No newline at end of file diff --git a/src/Win32/Memory.bf b/src/Win32/Memory.bf index 8b885c8..8901ad9 100644 --- a/src/Win32/Memory.bf +++ b/src/Win32/Memory.bf @@ -50,5 +50,20 @@ namespace RfgNetworking.Win32 { [Import("kernel32.lib"), CLink, CallingConvention(.Stdcall)] public static extern BOOL VirtualProtect(void* lpAddress, uint dwSize, PAGE_PROTECTION_FLAGS flNewProtect, out PAGE_PROTECTION_FLAGS lpflOldProtect); + + [Import("kernel32.lib"), CLink, CallingConvention(.Stdcall)] + public static extern void GetLocalTime(SystemTime* lpSystemTime); } + + [CRepr] + public struct SystemTime{ + public uint16 Year; + public uint16 Month; + public uint16 DayOfWeek; + public uint16 Day; + public uint16 Hour; + public uint16 Minute; + public uint16 Second; + public uint16 Milliseconds; + } } \ No newline at end of file