1
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 abstract string Key { get; }
  11. public IEnumerable<string> HeaderNames =>
  12. typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Select(f => f.Name)
  13. .Where(f => !f.Equals("timestamp"));
  14. public Dictionary<long, string[]> BufferLines { get; private set; }
  15. public virtual void Awake()
  16. {
  17. gameObject.SetActive(GameManager.LOG_TO_FILE);
  18. }
  19. public virtual void Start()
  20. {
  21. BufferLines = new Dictionary<long, string[]>();
  22. FileLogger.Instance.RegisterLogable(this);
  23. }
  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. }