Logging.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Threading.Tasks;
  2. using UnityEngine;
  3. namespace Logging.Base
  4. {
  5. public class Logging : MonoBehaviour
  6. {
  7. public float writeInterval = 1f;
  8. public SubjectManager subjectManager;
  9. public ConditionManager conditionManager;
  10. private FileLogger logger;
  11. private Task previousLogUpdateTask = Task.CompletedTask;
  12. private float prevLogTimestamp;
  13. private void Awake()
  14. {
  15. gameObject.SetActive(false);
  16. }
  17. private void Start()
  18. {
  19. logger = FileLogger.Instance;
  20. logger.Condition = $"Condition_{conditionManager.activeCondition}";
  21. logger.SubFolderName = $"Subject_{subjectManager.subjectIdentifier}";
  22. }
  23. private void Update()
  24. {
  25. if (!previousLogUpdateTask.IsCompleted) return;
  26. var time = Time.time;
  27. var dif = time - prevLogTimestamp;
  28. if (dif >= writeInterval)
  29. {
  30. prevLogTimestamp = time;
  31. previousLogUpdateTask = logger.UpdateRegisteredLogs();
  32. }
  33. }
  34. private async void OnDestroy()
  35. {
  36. if (logger != null) await logger.UpdateRegisteredLogs();
  37. FileLogger.DestroyInstance();
  38. }
  39. }
  40. }