From 3c7b7801846303f1d9a15a103c17decbebe5f2fd Mon Sep 17 00:00:00 2001
From: jehret <ehret@vr.rwth-aachen.de>
Date: Wed, 17 Jan 2024 13:46:55 +0100
Subject: [PATCH 1/2] remove all streams on session end, so that logging into a
 not created stream while editor is open does not accidently delete everything
 logged in the session before

---
 Source/UniversalLogging/Private/UniversalLogging.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Source/UniversalLogging/Private/UniversalLogging.cpp b/Source/UniversalLogging/Private/UniversalLogging.cpp
index 3fce5e3..12ff4de 100644
--- a/Source/UniversalLogging/Private/UniversalLogging.cpp
+++ b/Source/UniversalLogging/Private/UniversalLogging.cpp
@@ -71,6 +71,8 @@ void UniversalLoggingImpl::OnSessionEnd(const bool)
   {
     Elem.Value->Close();
   }
+
+  Streams.Empty();
 }
 
 void UniversalLoggingImpl::OnPostActorTick(UWorld* World, ELevelTick LevelTick, float WhateverThisIsDeltaTimeMaybe)
-- 
GitLab


From 38d3784f1870af98c9a537bf0a14269b386f6fe5 Mon Sep 17 00:00:00 2001
From: jehret <ehret@vr.rwth-aachen.de>
Date: Wed, 17 Jan 2024 13:52:08 +0100
Subject: [PATCH 2/2] allow closing log streams, e.g., to free the file handle
 or to be sure, that it is closed and noone will write into it anymore

---
 .../Private/UniLogBlueprintFunctionLibrary.cpp           | 5 +++++
 Source/UniversalLogging/Private/UniversalLogging.cpp     | 9 +++++++++
 Source/UniversalLogging/Private/UniversalLogging.h       | 1 +
 Source/UniversalLogging/Public/IUniversalLogging.h       | 6 ++++++
 .../Public/UniLogBlueprintFunctionLibrary.h              | 3 +++
 5 files changed, 24 insertions(+)

diff --git a/Source/UniversalLogging/Private/UniLogBlueprintFunctionLibrary.cpp b/Source/UniversalLogging/Private/UniLogBlueprintFunctionLibrary.cpp
index da7f1f9..a38fa30 100644
--- a/Source/UniversalLogging/Private/UniLogBlueprintFunctionLibrary.cpp
+++ b/Source/UniversalLogging/Private/UniLogBlueprintFunctionLibrary.cpp
@@ -62,3 +62,8 @@ void UUniLogBlueprintFunctionLibrary::ModifyLogStream(const FString StreamName,
   LogStream->SetLogOnScreenOnMaster(bLogOnScreenOnMaster);
   LogStream->SetLogOnScreenOnSlaves(bLogOnScreenOnSlaves);
 }
+
+void UUniLogBlueprintFunctionLibrary::CloseLogStream(const FString StreamName)
+{
+  UniLog.CloseLogStream(StreamName);
+}
\ No newline at end of file
diff --git a/Source/UniversalLogging/Private/UniversalLogging.cpp b/Source/UniversalLogging/Private/UniversalLogging.cpp
index 12ff4de..0414fec 100644
--- a/Source/UniversalLogging/Private/UniversalLogging.cpp
+++ b/Source/UniversalLogging/Private/UniversalLogging.cpp
@@ -108,6 +108,15 @@ ILogStream* UniversalLoggingImpl::NewLogStream(const FString StreamName, const F
   return Streams[StreamName].Get();
 }
 
+void UniversalLoggingImpl::CloseLogStream(const FString StreamName)
+{
+  if(Streams.Contains(StreamName))
+  {
+    Streams[StreamName]->Close();
+    Streams.Remove(StreamName);
+  }
+}
+
 ILogStream * UniversalLoggingImpl::GetLogStream(const FString StreamName)
 {
   if (Streams.Contains(StreamName))
diff --git a/Source/UniversalLogging/Private/UniversalLogging.h b/Source/UniversalLogging/Private/UniversalLogging.h
index d6a98cc..0c5674d 100644
--- a/Source/UniversalLogging/Private/UniversalLogging.h
+++ b/Source/UniversalLogging/Private/UniversalLogging.h
@@ -25,6 +25,7 @@ public:
   ILogStream* NewLogStream(const FString StreamName, const FString Filepath, const FString Filename,
                            bool bPer_Session = false, const bool bLogOnMaster = true,
                            const bool bLogOnSlaves = false) override;
+  void CloseLogStream(const FString StreamName) override;
   ILogStream* GetLogStream(const FString StreamName) override;
   ILogStream* GetDefaultLogStream() override;
   FString GetSessionIdentifier() override;
diff --git a/Source/UniversalLogging/Public/IUniversalLogging.h b/Source/UniversalLogging/Public/IUniversalLogging.h
index b76c7ec..707b882 100644
--- a/Source/UniversalLogging/Public/IUniversalLogging.h
+++ b/Source/UniversalLogging/Public/IUniversalLogging.h
@@ -47,6 +47,12 @@ public:
                                    bool bPer_Session = false, const bool bLogOnMaster = true,
                                    const bool bLogOnSlaves = false) = 0;
 
+  /**
+  * Closes a specific named log stream.
+  * This is done by default when a session stops, but can also be done manually if needed
+  */
+  virtual void CloseLogStream(const FString StreamName) = 0;
+
   /**
   * Getter for log streams.
   *
diff --git a/Source/UniversalLogging/Public/UniLogBlueprintFunctionLibrary.h b/Source/UniversalLogging/Public/UniLogBlueprintFunctionLibrary.h
index ce5d6e4..ad47730 100644
--- a/Source/UniversalLogging/Public/UniLogBlueprintFunctionLibrary.h
+++ b/Source/UniversalLogging/Public/UniLogBlueprintFunctionLibrary.h
@@ -37,4 +37,7 @@ class UUniLogBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
                                 FColor OnScreenBackgroundColor = FColor(0, 0, 0, 128), float OnScreenSize = 1.0,
                                 float OnScreenDuration = 5.0, bool bLogToDefaultLog = false, bool bLogOnScreenOnMaster = true,
                                 bool bLogOnScreenOnSlaves = false);
+
+  UFUNCTION(BlueprintCallable, Category = "UniLog")
+    static void CloseLogStream(const FString StreamName);
 };
-- 
GitLab