2
0

Logging.cs 1.3 KB

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