From 3473ffca1fa5fd6b0d4be8efee849b493e82b903 Mon Sep 17 00:00:00 2001
From: Erik Johnson <rejohnso@nps.edu>
Date: Tue, 12 May 2020 17:24:26 -0700
Subject: [PATCH] EventLog can now redirect events to the Unity Log.

Added option to copy added events to be displayed in the Unity Log. Kind of useful to view the events as they come in.
---
 Event Logger/EventLog.cs | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/Event Logger/EventLog.cs b/Event Logger/EventLog.cs
index a1ed765..375e339 100644
--- a/Event Logger/EventLog.cs	
+++ b/Event Logger/EventLog.cs	
@@ -10,6 +10,9 @@ namespace Shared.EventLog {
   // Depends on ScriptableVariable package
   [CreateAssetMenu(menuName = "Scriptable Objects/Event Log")]
   public class EventLog : ScriptableVariable {
+    [Tooltip("Redirect all incoming events to the Unity Log or not")]
+    public bool redirectToUnityLog = false;
+    
     readonly List<KeyValuePair<string, string>> events = new List<KeyValuePair<string, string>>();
 
     private DateTime startTime = DateTime.Now;
@@ -17,7 +20,12 @@ namespace Shared.EventLog {
     // ---------------------------------------------------------------------------
     // Add an event to the log
     public void Add(string key, string value) {
-      events.Add(new KeyValuePair<string, string>(DateTime.Now.Subtract(startTime).TotalSeconds.ToString() + "," + key, value));
+      var evt = new KeyValuePair<string, string>(DateTime.Now.Subtract(startTime).TotalSeconds.ToString() + "," + key, value);
+      events.Add(evt);
+      
+      if (redirectToUnityLog) {
+        Debug.Log(EventToString(evt));
+      }
     }
 
     // ---------------------------------------------------------------------------
@@ -34,7 +42,7 @@ namespace Shared.EventLog {
     public void WriteToFile(string directory, string filename) {
       string csv = string.Join(
         System.Environment.NewLine,
-        events.Select(d => d.Key + "," + d.Value).ToArray()
+        events.Select(d => EventToString(d)).ToArray()
       );
 
 #if UNITY_ANDROID
@@ -47,5 +55,11 @@ namespace Shared.EventLog {
       directory += "/" + filename;
       File.WriteAllText(directory, csv);
     }
+
+    // ---------------------------------------------------------------------------
+    //Utility used to convert an Event data to a string, suitable for display.
+    private string EventToString(KeyValuePair<string, string> evt) {
+      return $"{evt.Key},{evt.Value}";
+    }
   }
 }
-- 
GitLab