2
0

SensorDataLogger.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Logging.Base;
  5. using UnityEngine;
  6. namespace Logging.Data
  7. {
  8. public abstract class SensorDataLogger<T> : MonoBehaviour, ILogable, ISerializableLog<T> where T : ISerializable
  9. {
  10. public virtual void Awake()
  11. {
  12. gameObject.SetActive(GameManager.LOG_TO_FILE);
  13. }
  14. public virtual void Start()
  15. {
  16. BufferLines = new Dictionary<long, string[]>();
  17. FileLogger.Instance.RegisterLogable(this);
  18. }
  19. public abstract string Key { get; }
  20. public IEnumerable<string> HeaderNames =>
  21. typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Select(f => f.Name)
  22. .Where(f => !f.Equals("timestamp"));
  23. public Dictionary<long, string[]> BufferLines { get; private set; }
  24. public void ClearBuffer()
  25. {
  26. BufferLines.Clear();
  27. }
  28. public void Log(T value)
  29. {
  30. var data = value.Serialize();
  31. BufferLines[data.Key] = data.Value;
  32. }
  33. public abstract IEnumerable<T> ReadLog(IEnumerable<IEnumerable<string>> lines);
  34. }
  35. }