41
41
*/
42
42
public final class DataLogManager {
43
43
private static DataLog m_log ;
44
+ private static boolean m_stopped ;
44
45
private static String m_logDir ;
45
46
private static boolean m_filenameOverride ;
46
- private static final Thread m_thread ;
47
+ private static Thread m_thread ;
47
48
private static final ZoneId m_utc = ZoneId .of ("UTC" );
48
49
private static final DateTimeFormatter m_timeFormatter =
49
50
DateTimeFormatter .ofPattern ("yyyyMMdd_HHmmss" ).withZone (m_utc );
@@ -59,11 +60,6 @@ public final class DataLogManager {
59
60
60
61
private DataLogManager () {}
61
62
62
- static {
63
- m_thread = new Thread (DataLogManager ::logMain , "DataLogDS" );
64
- m_thread .setDaemon (true );
65
- }
66
-
67
63
/** Start data log manager with default directory location. */
68
64
public static synchronized void start () {
69
65
start ("" , "" , 0.25 );
@@ -100,33 +96,52 @@ public static synchronized void start(String dir, String filename) {
100
96
* tradeoff
101
97
*/
102
98
public static synchronized void start (String dir , String filename , double period ) {
103
- if (m_log != null ) {
104
- return ;
105
- }
106
- m_logDir = makeLogDir (dir );
107
- m_filenameOverride = !filename .isEmpty ();
99
+ if (m_log == null ) {
100
+ m_logDir = makeLogDir (dir );
101
+ m_filenameOverride = !filename .isEmpty ();
108
102
109
- // Delete all previously existing FRC_TBD_*.wpilog files. These only exist when the robot
110
- // never connects to the DS, so they are very unlikely to have useful data and just clutter
111
- // the filesystem.
112
- File [] files =
113
- new File (m_logDir )
114
- .listFiles ((d , name ) -> name .startsWith ("FRC_TBD_" ) && name .endsWith (".wpilog" ));
115
- if (files != null ) {
116
- for (File file : files ) {
117
- if (!file .delete ()) {
118
- System .err .println ("DataLogManager: could not delete " + file );
103
+ // Delete all previously existing FRC_TBD_*.wpilog files. These only exist when the robot
104
+ // never connects to the DS, so they are very unlikely to have useful data and just clutter
105
+ // the filesystem.
106
+ File [] files =
107
+ new File (m_logDir )
108
+ .listFiles ((d , name ) -> name .startsWith ("FRC_TBD_" ) && name .endsWith (".wpilog" ));
109
+ if (files != null ) {
110
+ for (File file : files ) {
111
+ if (!file .delete ()) {
112
+ System .err .println ("DataLogManager: could not delete " + file );
113
+ }
119
114
}
120
115
}
116
+ m_log = new DataLog (m_logDir , makeLogFilename (filename ), period );
117
+ m_messageLog = new StringLogEntry (m_log , "messages" );
118
+
119
+ // Log all NT entries and connections
120
+ if (m_ntLoggerEnabled ) {
121
+ startNtLog ();
122
+ }
123
+ } else if (m_stopped ) {
124
+ m_log .setFilename (makeLogFilename (filename ));
125
+ m_log .resume ();
126
+ m_stopped = false ;
121
127
}
122
128
123
- m_log = new DataLog (m_logDir , makeLogFilename (filename ), period );
124
- m_messageLog = new StringLogEntry (m_log , "messages" );
125
- m_thread .start ();
129
+ if (m_thread == null ) {
130
+ m_thread = new Thread (DataLogManager ::logMain , "DataLogDS" );
131
+ m_thread .setDaemon (true );
132
+ m_thread .start ();
133
+ }
134
+ }
126
135
127
- // Log all NT entries and connections
128
- if (m_ntLoggerEnabled ) {
129
- startNtLog ();
136
+ /** Stop data log manager. */
137
+ public static synchronized void stop () {
138
+ if (m_thread != null ) {
139
+ m_thread .interrupt ();
140
+ m_thread = null ;
141
+ }
142
+ if (m_log != null ) {
143
+ m_log .stop ();
144
+ m_stopped = true ;
130
145
}
131
146
}
132
147
0 commit comments