Logging.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. private float prevLogTimestamp;
  9. private FileLogger logger;
  10. private Task previousLogUpdateTask = Task.CompletedTask;
  11. private void Awake()
  12. {
  13. gameObject.SetActive(false);
  14. }
  15. private void Start()
  16. {
  17. logger = FileLogger.Instance;
  18. }
  19. private void Update()
  20. {
  21. if(!previousLogUpdateTask.IsCompleted) return;
  22. var time = Time.time;
  23. var dif = time - prevLogTimestamp;
  24. if (dif >= writeInterval)
  25. {
  26. prevLogTimestamp = time;
  27. previousLogUpdateTask = logger.UpdateRegisteredLogs();
  28. }
  29. }
  30. private async void OnDestroy()
  31. {
  32. if (logger != null)
  33. {
  34. await logger.UpdateRegisteredLogs();
  35. }
  36. FileLogger.DestroyInstance();
  37. }
  38. }
  39. }