Skip to content
Snippets Groups Projects
Commit 3473ffca authored by Erik Johnson's avatar Erik Johnson
Browse files

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.
parent cb00b530
No related branches found
No related tags found
1 merge request!5EventLog can now redirect events to the Unity Log.
...@@ -10,6 +10,9 @@ namespace Shared.EventLog { ...@@ -10,6 +10,9 @@ namespace Shared.EventLog {
// Depends on ScriptableVariable package // Depends on ScriptableVariable package
[CreateAssetMenu(menuName = "Scriptable Objects/Event Log")] [CreateAssetMenu(menuName = "Scriptable Objects/Event Log")]
public class EventLog : ScriptableVariable { 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>>(); readonly List<KeyValuePair<string, string>> events = new List<KeyValuePair<string, string>>();
private DateTime startTime = DateTime.Now; private DateTime startTime = DateTime.Now;
...@@ -17,7 +20,12 @@ namespace Shared.EventLog { ...@@ -17,7 +20,12 @@ namespace Shared.EventLog {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Add an event to the log // Add an event to the log
public void Add(string key, string value) { 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 { ...@@ -34,7 +42,7 @@ namespace Shared.EventLog {
public void WriteToFile(string directory, string filename) { public void WriteToFile(string directory, string filename) {
string csv = string.Join( string csv = string.Join(
System.Environment.NewLine, System.Environment.NewLine,
events.Select(d => d.Key + "," + d.Value).ToArray() events.Select(d => EventToString(d)).ToArray()
); );
#if UNITY_ANDROID #if UNITY_ANDROID
...@@ -47,5 +55,11 @@ namespace Shared.EventLog { ...@@ -47,5 +55,11 @@ namespace Shared.EventLog {
directory += "/" + filename; directory += "/" + filename;
File.WriteAllText(directory, csv); 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}";
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment