-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoggingManager.cs
More file actions
133 lines (112 loc) · 3.98 KB
/
LoggingManager.cs
File metadata and controls
133 lines (112 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Logger = UnityDataLogging.Utility.Logger;
namespace UnityDataLogging
{
public class LoggingManager : MonoBehaviour // By Alex van den Berg - https://github.com/Avdbergnmf/UnityDataLogging
{
// This is the central logging manager. It has functionality to start & stop logging of all the loggers in the List<Logger>, as well as creating their files etc.
[Header("Main Settings")]
public List<Logger> loggers = new List<Logger>();
[Tooltip("When enabled, will start all loggers at the startup. Otherwise startRepetition should be called from an external script (or using the editorcontrols).")]
[SerializeField] bool logAtStartup = false;
[Tooltip("If logging at the startup, after how much time should the loggers start? This prevents race conditions so that all loggers are properly initialized.")]
[SerializeField] float startupLogWaitTime = 0.0f;
[Header("Editor Controls")]
[Tooltip("Write single log by toggling this bool in the editor. Useful mostly for testing.")]
[SerializeField] bool writeSingleRecord = false;
[Tooltip("Start logging by toggling this bool in the editor. Useful mostly for testing.")]
[SerializeField] bool startLoggingNow = false;
[Tooltip("Stop logging by toggling this bool in the editor. Useful mostly for testing.")]
[SerializeField] bool stopLoggingNow = false;
// Using this function you can stop and start the loggers from the editor by clicking the connected bools. This is only useful for testing really.
private void OnValidate()
{
if (writeSingleRecord)
{
print("Writing a single log.");
writeSingleRecord = false;
WriteSingleLog();
}
if (startLoggingNow)
{
print("Starting all connected loggers");
startLoggingNow = false;
StartRecording();
}
if (stopLoggingNow)
{
print("Stopping all connected loggers");
stopLoggingNow = false;
StopRecording();
}
}
private void Start()
{
if (logAtStartup)
StartCoroutine(LateStart(startupLogWaitTime));
}
IEnumerator LateStart(float waitTime)
{
yield return new WaitForSeconds(waitTime);
StartRecording();
}
private void Update()
{
WriteAll(); // call all loggers to write
}
#region Configuration
public void AddLogger(Logger logger)
{
loggers.Add(logger);
}
public void RmLogger(Logger logger)
{
loggers.Remove(logger);
}
#endregion
#region WritingFunctions
private void WriteSingleLog()
{
foreach (var log in loggers)
{
log.WriteSingleRecord();
}
}
void WriteAll(bool forceWrite = false)
{
foreach (var log in loggers)
{
log.Write(forceWrite);
}
}
#endregion
#region LoggingControl
private void StartRecording() // Call this function to create the logs and start the timers for all attached loggers
{
foreach (var log in loggers)
log.StartRecording(); // trial number
}
private void StopRecording()
{
foreach (var log in loggers)
log.StopRecording();
}
#endregion
#region Shutdown
private void OnApplicationQuit()
{
StopRecording();
}
private void OnDisable()
{
StopRecording();
}
private void OnDestroy()
{
StopRecording();
}
#endregion
}
}