001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenix6;
008
009import java.io.IOException;
010import java.nio.ByteBuffer;
011import java.util.HashMap;
012import java.util.HashSet;
013import java.util.Map;
014import java.util.Set;
015import java.util.concurrent.ConcurrentHashMap;
016import java.util.concurrent.locks.Lock;
017import java.util.concurrent.locks.ReentrantLock;
018
019import com.ctre.phoenix6.jni.ErrorReportingJNI;
020import com.ctre.phoenix6.jni.SignalLoggerJNI;
021import edu.wpi.first.units.Measure;
022import edu.wpi.first.units.Unit;
023import edu.wpi.first.util.protobuf.Protobuf;
024import edu.wpi.first.util.protobuf.ProtobufBuffer;
025import edu.wpi.first.util.struct.Struct;
026import edu.wpi.first.util.struct.StructBuffer;
027import us.hebi.quickbuf.Descriptors.FileDescriptor;
028
029/**
030 * Static class for controlling the Phoenix 6 signal logger.
031 * <p>
032 * This logs all the signals from the CAN buses into .hoot files. Each file name starts with the
033 * CANivore serial number or "rio" for the roboRIO CAN bus, followed by the timestamp. In the
034 * header of a hoot file, the CANivore name and firmware version are logged in plain text.
035 * <p>
036 * During an FRC match, the log file will be renamed to include the event name, match type, and
037 * match number at the start of the file name. The match type will be 'P' for practice matches,
038 * 'Q' for qualification matches, and 'E' for elimination matches.
039 * <p>
040 * During Hoot Replay, the signal logger always runs while replay is running. All custom signals
041 * written during replay will be automatically placed under {@code hoot_replay/}. Additionally,
042 * the log will contain all status signals and custom signals from the original log.
043 */
044public class SignalLogger {
045    /**
046     * Sets the destination for logging, restarting logger if the path changed.
047     * <p>
048     * If this is not called or the path is left empty, the default path will be used. The
049     * default path on the roboRIO is a logs folder on the first USB flash drive found, or
050     * /home/lvuser/logs if none is available. The default path on all other platforms is
051     * a logs folder in the current working directory.
052     * <p>
053     * Typical use for this routine is to use a removable USB flash drive for logging.
054     * <p>
055     * This is ignored during Hoot Replay, where the hoot log will always be written to a
056     * subfolder next to the log being replayed.
057     *
058     * @param path Folder path for the log files; path must exist
059     * @return Status of setting the path and restarting the log
060     */
061    public static StatusCode setPath(String path) {
062        var retval = StatusCode.valueOf(SignalLoggerJNI.JNI_SetLoggerPath(path));
063
064        if (!retval.isOK()) {
065            ErrorReportingJNI.reportStatusCode(retval.value, "ctre.phoenix6.SignalLogger.setPath");
066        }
067
068        return retval;
069    }
070
071    /**
072     * Starts logging status signals. Starts regardless of auto logging status.
073     * <p>
074     * If using a roboRIO 1, we recommend setting the logging path to an external drive
075     * using {@link #setPath} to avoid running out of internal storage space.
076     * <p>
077     * This is ignored during Hoot Replay, where logging is automatically started when
078     * Hoot Replay starts running or restarts.
079     *
080     * @return Status of starting the logger
081     */
082    public static StatusCode start() {
083        return StatusCode.valueOf(SignalLoggerJNI.JNI_StartLogger());
084    }
085
086    /**
087     * Stops logging status signals. Stops regardless of auto logging status.
088     * <p>
089     * This is ignored during Hoot Replay, where logging is automatically stopped when
090     * Hoot Replay is stopped or reaches the end of the file.
091     *
092     * @return Status of stopping the logger
093     */
094    public static StatusCode stop() {
095        return StatusCode.valueOf(SignalLoggerJNI.JNI_StopLogger());
096    }
097
098    /**
099     * Enables or disables auto logging.
100     * <p>
101     * Auto logging is enabled by default on the roboRIO and disabled by default on
102     * other systems. Additionally, on a roboRIO 1, auto logging will only be active
103     * if a USB flash drive is present.
104     * <p>
105     * When auto logging is enabled, logging is started by any of the following
106     * (whichever occurs first):
107     *
108     * <ul>
109     *   <li> It has been at least 1 second since program startup (allowing for calls
110     *        to {@link #setPath}), and the robot is enabled.
111     *   <li> It has been at least 5 seconds since program startup (allowing for calls
112     *        to {@link #setPath}), and the Driver Station is connected to the robot
113     *        (if on a roboRIO).
114     * </ul>
115     *
116     * After auto logging has started the log once, logging will not be automatically
117     * stopped or restarted by auto logging.
118     *
119     * @param enable Whether to enable auto logging
120     * @return Status of auto logging enable/disable
121     */
122    public static StatusCode enableAutoLogging(boolean enable) {
123        return StatusCode.valueOf(SignalLoggerJNI.JNI_EnableAutoLogging(enable));
124    }
125
126    /**
127     * Adds the schema to the log file.
128     * <p>
129     * Users can call {@link #writeStruct}, {@link #writeStructArray}, and
130     * {@link #writeProtobuf} to directly write schema values instead.
131     * <p>
132     * The schema name should typically exactly match the name of the type
133     * (without any extra prefix or suffix).
134     * <p>
135     * For protobuf, first register all relevant file descriptors by file name
136     * (such as "geometry2d.proto"). Then, for each top-level type being used,
137     * add a separate empty schema with the full name of the type (such as
138     * "wpi.proto.ProtobufPose2d").
139     *
140     * @param name Name of the schema
141     * @param type Type of the schema, such as struct or protobuf
142     * @param schema Schema bytes to write
143     * @return Status of adding the schema
144     */
145    public static StatusCode addSchema(String name, HootSchemaType type, byte[] schema) {
146        return StatusCode.valueOf(SignalLoggerJNI.JNI_AddSchema(name, type.value, schema));
147    }
148    /**
149     * Adds the schema to the log file.
150     * <p>
151     * Users can call {@link #writeStruct}, {@link #writeStructArray}, and
152     * {@link #writeProtobuf} to directly write schema values instead.
153     * <p>
154     * The schema name should typically exactly match the name of the type
155     * (without any extra prefix or suffix).
156     * <p>
157     * For protobuf, first register all relevant file descriptors by file name
158     * (such as "geometry2d.proto"). Then, for each top-level type being used,
159     * add a separate empty schema with the full name of the type (such as
160     * "wpi.proto.ProtobufPose2d").
161     *
162     * @param name Name of the schema
163     * @param type Type of the schema, such as struct or protobuf
164     * @param schema Schema string to write
165     * @return Status of adding the schema
166     */
167    public static StatusCode addSchema(String name, HootSchemaType type, String schema) {
168        return StatusCode.valueOf(SignalLoggerJNI.JNI_AddSchemaString(name, type.value, schema));
169    }
170
171    /**
172     * Checks if the schema has already been added to the log files.
173     *
174     * @param name Name of the schema
175     * @param type Type of the schema, such as struct or protobuf
176     * @return Whether the schema has been added to the log files
177     */
178    public static boolean hasSchema(String name, HootSchemaType type) {
179        return SignalLoggerJNI.JNI_HasSchema(name, type.value);
180    }
181
182    /**
183     * Writes the schema-serialized bytes to the log file.
184     * <p>
185     * Users can call {@link #writeStruct}, {@link #writeStructArray}, and
186     * {@link #writeProtobuf} to directly write schema values instead.
187     * <p>
188     * The name of the associated schema must exactly match the type of the data
189     * (such as "Pose2d" or "wpi.proto.ProtobufPose2d"). Additionally, the schema
190     * name must be registered with {@link #addSchema} before calling this API.
191     *
192     * @param name Name of the signal
193     * @param schema Name of the associated schema
194     * @param type Type of the associated schema, such as struct or protobuf
195     * @param data Serialized data bytes
196     * @return Status of writing the data
197     */
198    public static StatusCode writeSchemaValue(String name, String schema, HootSchemaType type, byte[] data) {
199        return writeSchemaValue(name, schema, type, data, 0);
200    }
201    /**
202     * Writes the schema-serialized bytes to the log file.
203     * <p>
204     * Users can call {@link #writeStruct}, {@link #writeStructArray}, and
205     * {@link #writeProtobuf} to directly write schema values instead.
206     * <p>
207     * The name of the associated schema must exactly match the type of the data
208     * (such as "Pose2d" or "wpi.proto.ProtobufPose2d"). Additionally, the schema
209     * name must be registered with {@link #addSchema} before calling this API.
210     *
211     * @param name Name of the signal
212     * @param schema Name of the associated schema
213     * @param type Type of the associated schema, such as struct or protobuf
214     * @param data Serialized data bytes
215     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
216     *                       from the current time to get the timestamp written to the log
217     * @return Status of writing the data
218     */
219    public static StatusCode writeSchemaValue(String name, String schema, HootSchemaType type, byte[] data, double latencySeconds) {
220        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteSchemaValue(name, schema, type.value, data, 0, data.length, latencySeconds));
221    }
222
223    /**
224     * Writes the schema-serialized buffer to the log file.
225     * <p>
226     * Users can call {@link #writeStruct}, {@link #writeStructArray}, and
227     * {@link #writeProtobuf} to directly write schema values instead.
228     * <p>
229     * The name of the associated schema must exactly match the type of the data
230     * (such as "Pose2d" or "wpi.proto.ProtobufPose2d"). Additionally, the schema
231     * name must be registered with {@link #addSchema} before calling this API.
232     *
233     * @param name Name of the signal
234     * @param schema Name of the associated schema
235     * @param type Type of the associated schema, such as struct or protobuf
236     * @param data Serialized data buffer, where [0, data.position()) will be written
237     * @return Status of writing the data
238     */
239    public static StatusCode writeSchemaValue(String name, String schema, HootSchemaType type, ByteBuffer data) {
240        return writeSchemaValue(name, schema, type, data, 0);
241    }
242    /**
243     * Writes the schema-serialized buffer to the log file.
244     * <p>
245     * Users can call {@link #writeStruct}, {@link #writeStructArray}, and
246     * {@link #writeProtobuf} to directly write schema values instead.
247     * <p>
248     * The name of the associated schema must exactly match the type of the data
249     * (such as "Pose2d" or "wpi.proto.ProtobufPose2d"). Additionally, the schema
250     * name must be registered with {@link #addSchema} before calling this API.
251     *
252     * @param name Name of the signal
253     * @param schema Name of the associated schema
254     * @param type Type of the associated schema, such as struct or protobuf
255     * @param data Serialized data buffer, where [0, data.position()) will be written
256     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
257     *                       from the current time to get the timestamp written to the log
258     * @return Status of writing the data
259     */
260    public static StatusCode writeSchemaValue(String name, String schema, HootSchemaType type, ByteBuffer data, double latencySeconds) {
261        if (data.isDirect()) {
262            return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteSchemaBuffer(name, schema, type.value, data, 0, data.position(), latencySeconds));
263        } else if (data.hasArray()) {
264            return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteSchemaValue(name, schema, type.value, data.array(), data.arrayOffset(), data.position(), latencySeconds));
265        } else {
266            throw new UnsupportedOperationException("ByteBuffer must be direct or have a backing array");
267        }
268    }
269
270    /**
271     * Writes the WPILib Struct to the log file.
272     *
273     * @param name Name of the signal
274     * @param struct Struct serialization implementation
275     * @param value Value to write
276     * @return Status of writing the data
277     */
278    public static <T> StatusCode writeStruct(String name, Struct<T> struct, T value) {
279        return writeStruct(name, struct, value, 0);
280    }
281    /**
282     * Writes the WPILib Struct to the log file.
283     *
284     * @param name Name of the signal
285     * @param struct Struct serialization implementation
286     * @param value Value to write
287     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
288     *                       from the current time to get the timestamp written to the log
289     * @return Status of writing the data
290     */
291    public static <T> StatusCode writeStruct(String name, Struct<T> struct, T value, double latencySeconds) {
292        final var typeName = struct.getTypeName();
293        if (!hasSchema(typeName, HootSchemaType.Struct)) {
294            addStructSchema(struct, new HashSet<>());
295        }
296
297        try {
298            _structBufferLck.lock();
299
300            @SuppressWarnings("unchecked")
301            StructBuffer<T> buf = (StructBuffer<T>)_structBuffers.get(typeName);
302            if (buf == null) {
303                buf = StructBuffer.create(struct);
304                _structBuffers.put(typeName, buf);
305            }
306            return writeSchemaValue(name, typeName, HootSchemaType.Struct, buf.write(value), latencySeconds);
307        } finally {
308            _structBufferLck.unlock();
309        }
310    }
311
312    /**
313     * Writes the array of WPILib Structs to the log file.
314     *
315     * @param name Name of the signal
316     * @param struct Struct serialization implementation
317     * @param values Values to write
318     * @return Status of writing the data
319     */
320    public static <T> StatusCode writeStructArray(String name, Struct<T> struct, T[] values) {
321        return writeStructArray(name, struct, values, 0);
322    }
323    /**
324     * Writes the array of WPILib Structs to the log file.
325     *
326     * @param name Name of the signal
327     * @param struct Struct serialization implementation
328     * @param values Values to write
329     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
330     *                       from the current time to get the timestamp written to the log
331     * @return Status of writing the data
332     */
333    public static <T> StatusCode writeStructArray(String name, Struct<T> struct, T[] values, double latencySeconds) {
334        final var structTypeName = struct.getTypeName();
335        var typeName = _structArrayTypeName.get(structTypeName);
336        if (typeName == null) {
337            typeName = structTypeName + "[]";
338            _structArrayTypeName.putIfAbsent(structTypeName, typeName);
339        }
340
341        if (!hasSchema(typeName, HootSchemaType.Struct)) {
342            addStructSchema(struct, new HashSet<>());
343            addSchema(typeName, HootSchemaType.Struct, new byte[0]);
344        }
345
346        try {
347            _structBufferLck.lock();
348
349            @SuppressWarnings("unchecked")
350            StructBuffer<T> buf = (StructBuffer<T>)_structBuffers.get(typeName);
351            if (buf == null) {
352                buf = StructBuffer.create(struct);
353                _structBuffers.put(typeName, buf);
354            }
355            return writeSchemaValue(name, typeName, HootSchemaType.Struct, buf.writeArray(values), latencySeconds);
356        } finally {
357            _structBufferLck.unlock();
358        }
359    }
360
361    /**
362     * Writes the protobuf to the log file.
363     *
364     * @param name Name of the signal
365     * @param proto Protobuf serialization implementation
366     * @param value Value to write
367     * @return Status of writing the data
368     */
369    public static <T> StatusCode writeProtobuf(String name, Protobuf<T, ?> proto, T value) {
370        return writeProtobuf(name, proto, value, 0);
371    }
372    /**
373     * Writes the protobuf to the log file.
374     *
375     * @param name Name of the signal
376     * @param proto Protobuf serialization implementation
377     * @param value Value to write
378     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
379     *                       from the current time to get the timestamp written to the log
380     * @return Status of writing the data
381     */
382    public static <T> StatusCode writeProtobuf(String name, Protobuf<T, ?> proto, T value, double latencySeconds) {
383        final var typeName = proto.getDescriptor().getFullName();
384        if (!hasSchema(typeName, HootSchemaType.Protobuf)) {
385            addProtobufSchema(proto.getDescriptor().getFile());
386            addSchema(typeName, HootSchemaType.Protobuf, new byte[0]);
387        }
388
389        try {
390            _protoBufferLck.lock();
391
392            @SuppressWarnings("unchecked")
393            ProtobufBuffer<T, ?> buf = (ProtobufBuffer<T, ?>)_protoBuffers.get(typeName);
394            if (buf == null) {
395                buf = ProtobufBuffer.create(proto);
396                _protoBuffers.put(typeName, buf);
397            }
398            return writeSchemaValue(name, typeName, HootSchemaType.Protobuf, buf.write(value), latencySeconds);
399        } catch (IOException e) {
400            return StatusCode.InvalidParamValue;
401        } finally {
402            _protoBufferLck.unlock();
403        }
404    }
405
406    /**
407     * Writes the raw data bytes to the log file.
408     *
409     * @param name Name of the signal
410     * @param data Raw data bytes
411     * @return Status of writing the data
412     */
413    public static StatusCode writeRaw(String name, byte[] data) {
414        return writeRaw(name, data, 0);
415    }
416    /**
417     * Writes the raw data bytes to the log file.
418     *
419     * @param name Name of the signal
420     * @param data Raw data bytes
421     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
422     *                       from the current time to get the timestamp written to the log
423     * @return Status of writing the data
424     */
425    public static StatusCode writeRaw(String name, byte[] data, double latencySeconds) {
426        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteRaw(name, data, latencySeconds));
427    }
428
429    /**
430     * Writes the boolean to the log file.
431     *
432     * @param name Name of the signal
433     * @param value Value to write
434     * @return Status of writing the data
435     */
436    public static StatusCode writeBoolean(String name, boolean value) {
437        return writeBoolean(name, value, 0);
438    }
439    /**
440     * Writes the boolean to the log file.
441     *
442     * @param name Name of the signal
443     * @param value Value to write
444     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
445     *                       from the current time to get the timestamp written to the log
446     * @return Status of writing the data
447     */
448    public static StatusCode writeBoolean(String name, boolean value, double latencySeconds) {
449        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteBoolean(name, value, latencySeconds));
450    }
451
452    /**
453     * Writes the integer to the log file.
454     *
455     * @param name Name of the signal
456     * @param value Value to write
457     * @return Status of writing the data
458     */
459    public static StatusCode writeInteger(String name, long value) {
460        return writeInteger(name, value, "");
461    }
462    /**
463     * Writes the integer to the log file.
464     *
465     * @param name Name of the signal
466     * @param value Value to write
467     * @param units Units of the signal
468     * @return Status of writing the data
469     */
470    public static StatusCode writeInteger(String name, long value, String units) {
471        return writeInteger(name, value, units, 0);
472    }
473    /**
474     * Writes the integer to the log file.
475     *
476     * @param name Name of the signal
477     * @param value Value to write
478     * @param units Units of the signal
479     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
480     *                       from the current time to get the timestamp written to the log
481     * @return Status of writing the data
482     */
483    public static StatusCode writeInteger(String name, long value, String units, double latencySeconds) {
484        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteInteger(name, value, units, latencySeconds));
485    }
486
487    /**
488     * Writes the float to the log file.
489     *
490     * @param name Name of the signal
491     * @param value Value to write
492     * @return Status of writing the data
493     */
494    public static StatusCode writeFloat(String name, float value) {
495        return writeFloat(name, value, "");
496    }
497    /**
498     * Writes the float to the log file.
499     *
500     * @param name Name of the signal
501     * @param value Value to write
502     * @param units Units of the signal
503     * @return Status of writing the data
504     */
505    public static StatusCode writeFloat(String name, float value, String units) {
506        return writeFloat(name, value, units, 0);
507    }
508    /**
509     * Writes the float to the log file.
510     *
511     * @param name Name of the signal
512     * @param value Value to write
513     * @param units Units of the signal
514     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
515     *                       from the current time to get the timestamp written to the log
516     * @return Status of writing the data
517     */
518    public static StatusCode writeFloat(String name, float value, String units, double latencySeconds) {
519        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteFloat(name, value, units, latencySeconds));
520    }
521
522    /**
523     * Writes the double to the log file.
524     *
525     * @param name Name of the signal
526     * @param value Value to write
527     * @return Status of writing the data
528     */
529    public static StatusCode writeDouble(String name, double value) {
530        return writeDouble(name, value, "");
531    }
532    /**
533     * Writes the double to the log file.
534     *
535     * @param name Name of the signal
536     * @param value Value to write
537     * @param units Units of the signal
538     * @return Status of writing the data
539     */
540    public static StatusCode writeDouble(String name, double value, String units) {
541        return writeDouble(name, value, units, 0);
542    }
543    /**
544     * Writes the double to the log file.
545     *
546     * @param name Name of the signal
547     * @param value Value to write
548     * @param units Units of the signal
549     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
550     *                       from the current time to get the timestamp written to the log
551     * @return Status of writing the data
552     */
553    public static StatusCode writeDouble(String name, double value, String units, double latencySeconds) {
554        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteDouble(name, value, units, latencySeconds));
555    }
556
557    /**
558     * Writes the string to the log file.
559     *
560     * @param name Name of the signal
561     * @param value Value to write
562     * @return Status of writing the data
563     */
564    public static StatusCode writeString(String name, String value) {
565        return writeString(name, value, 0);
566    }
567    /**
568     * Writes the string to the log file.
569     *
570     * @param name Name of the signal
571     * @param value Value to write
572     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
573     *                       from the current time to get the timestamp written to the log
574     * @return Status of writing the data
575     */
576    public static StatusCode writeString(String name, String value, double latencySeconds) {
577        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteString(name, value, latencySeconds));
578    }
579
580    /**
581     * Writes the unit value to the log file.
582     *
583     * @param name Name of the signal
584     * @param value Value to write
585     * @return Status of writing the data
586     */
587    public static StatusCode writeValue(String name, Measure<?> value) {
588        return writeValue(name, value, 0);
589    }
590    /**
591     * Writes the unit value to the log file.
592     *
593     * @param name Name of the signal
594     * @param value Value to write
595     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
596     *                       from the current time to get the timestamp written to the log
597     * @return Status of writing the data
598     */
599    public static StatusCode writeValue(String name, Measure<?> value, double latencySeconds) {
600        return writeDouble(name, value.magnitude(), value.unit().symbol(), latencySeconds);
601    }
602
603    /**
604     * Writes the unit value to the log file in the given units.
605     *
606     * @param name Name of the signal
607     * @param value Value to write
608     * @param units Units to use for logging the measure
609     * @return Status of writing the data
610     */
611    public static <U extends Unit> StatusCode writeValue(String name, Measure<U> value, U units) {
612        return writeValue(name, value, units, 0);
613    }
614    /**
615     * Writes the unit value to the log file in the given units.
616     *
617     * @param name Name of the signal
618     * @param value Value to write
619     * @param units Units to use for logging the measure
620     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
621     *                       from the current time to get the timestamp written to the log
622     * @return Status of writing the data
623     */
624    public static <U extends Unit> StatusCode writeValue(String name, Measure<U> value, U units, double latencySeconds) {
625        return writeDouble(name, value.in(units), units.symbol(), latencySeconds);
626    }
627
628    /**
629     * Writes the array of booleans to the log file.
630     *
631     * @param name Name of the signal
632     * @param values Array of values to write
633     * @return Status of writing the data
634     */
635    public static StatusCode writeBooleanArray(String name, boolean[] values) {
636        return writeBooleanArray(name, values, 0);
637    }
638    /**
639     * Writes the array of booleans to the log file.
640     *
641     * @param name Name of the signal
642     * @param values Array of values to write
643     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
644     *                       from the current time to get the timestamp written to the log
645     * @return Status of writing the data
646     */
647    public static StatusCode writeBooleanArray(String name, boolean[] values, double latencySeconds) {
648        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteBooleanArray(name, values, latencySeconds));
649    }
650
651    /**
652     * Writes the array of integers to the log file.
653     *
654     * @param name Name of the signal
655     * @param values Array of values to write
656     * @return Status of writing the data
657     */
658    public static StatusCode writeIntegerArray(String name, long[] values) {
659        return writeIntegerArray(name, values, "");
660    }
661    /**
662     * Writes the array of integers to the log file.
663     *
664     * @param name Name of the signal
665     * @param values Array of values to write
666     * @param units Units of the signals
667     * @return Status of writing the data
668     */
669    public static StatusCode writeIntegerArray(String name, long[] values, String units) {
670        return writeIntegerArray(name, values, units, 0);
671    }
672    /**
673     * Writes the array of integers to the log file.
674     *
675     * @param name Name of the signal
676     * @param values Array of values to write
677     * @param units Units of the signals
678     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
679     *                       from the current time to get the timestamp written to the log
680     * @return Status of writing the data
681     */
682    public static StatusCode writeIntegerArray(String name, long[] values, String units, double latencySeconds) {
683        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteIntegerArray(name, values, units, latencySeconds));
684    }
685
686    /**
687     * Writes the array of floats to the log file.
688     *
689     * @param name Name of the signal
690     * @param values Array of values to write
691     * @return Status of writing the data
692     */
693    public static StatusCode writeFloatArray(String name, float[] values) {
694        return writeFloatArray(name, values, "");
695    }
696    /**
697     * Writes the array of floats to the log file.
698     *
699     * @param name Name of the signal
700     * @param values Array of values to write
701     * @param units Units of the signals
702     * @return Status of writing the data
703     */
704    public static StatusCode writeFloatArray(String name, float[] values, String units) {
705        return writeFloatArray(name, values, units, 0);
706    }
707    /**
708     * Writes the array of floats to the log file.
709     *
710     * @param name Name of the signal
711     * @param values Array of values to write
712     * @param units Units of the signals
713     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
714     *                       from the current time to get the timestamp written to the log
715     * @return Status of writing the data
716     */
717    public static StatusCode writeFloatArray(String name, float[] values, String units, double latencySeconds) {
718        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteFloatArray(name, values, units, latencySeconds));
719    }
720
721    /**
722     * Writes the array of doubles to the log file.
723     *
724     * @param name Name of the signal
725     * @param values Array of values to write
726     * @return Status of writing the data
727     */
728    public static StatusCode writeDoubleArray(String name, double[] values) {
729        return writeDoubleArray(name, values, "");
730    }
731    /**
732     * Writes the array of doubles to the log file.
733     *
734     * @param name Name of the signal
735     * @param values Array of values to write
736     * @param units Units of the signals
737     * @return Status of writing the data
738     */
739    public static StatusCode writeDoubleArray(String name, double[] values, String units) {
740        return writeDoubleArray(name, values, units, 0);
741    }
742    /**
743     * Writes the array of doubles to the log file.
744     *
745     * @param name Name of the signal
746     * @param values Array of values to write
747     * @param units Units of the signals
748     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
749     *                       from the current time to get the timestamp written to the log
750     * @return Status of writing the data
751     */
752    public static StatusCode writeDoubleArray(String name, double[] values, String units, double latencySeconds) {
753        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteDoubleArray(name, values, units, latencySeconds));
754    }
755
756    /**
757     * Writes the array of strings to the log file.
758     *
759     * @param name Name of the signal
760     * @param values Array of values to write
761     * @return Status of writing the data
762     */
763    public static StatusCode writeStringArray(String name, String[] values) {
764        return writeStringArray(name, values, 0);
765    }
766    /**
767     * Writes the array of strings to the log file.
768     *
769     * @param name Name of the signal
770     * @param values Array of values to write
771     * @param latencySeconds Latency of the signal in seconds; this value is subtracted
772     *                       from the current time to get the timestamp written to the log
773     * @return Status of writing the data
774     */
775    public static StatusCode writeStringArray(String name, String[] values, double latencySeconds) {
776        return StatusCode.valueOf(SignalLoggerJNI.JNI_WriteStringArray(name, values, latencySeconds));
777    }
778
779    private static final Lock _structBufferLck = new ReentrantLock();
780    private static final Map<String, StructBuffer<?>> _structBuffers = new HashMap<>();
781    private static final Map<String, String> _structArrayTypeName = new ConcurrentHashMap<>();
782
783    private static final Lock _protoBufferLck = new ReentrantLock();
784    private static final Map<String, ProtobufBuffer<?, ?>> _protoBuffers = new HashMap<>();
785
786    private static void addStructSchema(Struct<?> struct, Set<String> seen) {
787        final var typeName = struct.getTypeName();
788        if (hasSchema(typeName, HootSchemaType.Struct)) {
789            return;
790        }
791        if (!seen.add(typeName)) {
792            throw new UnsupportedOperationException(typeName + ": circular reference with " + seen);
793        }
794
795        for (var nested : struct.getNested()) {
796            addStructSchema(nested, seen);
797        }
798        addSchema(typeName, HootSchemaType.Struct, struct.getSchema());
799        seen.remove(typeName);
800    }
801
802    private static void addProtobufSchema(FileDescriptor desc) {
803        final var typeName = desc.getFullName();
804        if (hasSchema(typeName, HootSchemaType.Protobuf)) {
805            return;
806        }
807
808        for (var dep : desc.getDependencies()) {
809            addProtobufSchema(dep);
810        }
811        addSchema(typeName, HootSchemaType.Protobuf, desc.toProtoBytes());
812    }
813}