Skip to content
Snippets Groups Projects

EventLog can now redirect events to the Unity Log.

Merged Erik Johnson requested to merge redirect-events-to-unity-debug-log into master
1 file
+ 16
2
Compare changes
  • Side-by-side
  • Inline
+ 16
2
@@ -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}";
}
}
}
Loading