001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.util.datalog;
006
007import edu.wpi.first.util.struct.Struct;
008import edu.wpi.first.util.struct.StructBuffer;
009import java.nio.ByteBuffer;
010
011/**
012 * Log struct-encoded values.
013 *
014 * @param <T> value class
015 */
016public final class StructLogEntry<T> extends DataLogEntry {
017  private StructLogEntry(
018      DataLog log, String name, Struct<T> struct, String metadata, long timestamp) {
019    super(log, name, struct.getTypeString(), metadata, timestamp);
020    m_buf = StructBuffer.create(struct);
021    log.addSchema(struct, timestamp);
022  }
023
024  /**
025   * Creates a struct-encoded log entry.
026   *
027   * @param <T> value class (inferred from struct)
028   * @param log datalog
029   * @param name name of the entry
030   * @param struct struct serialization implementation
031   * @param metadata metadata
032   * @param timestamp entry creation timestamp (0=now)
033   * @return StructLogEntry
034   */
035  public static <T> StructLogEntry<T> create(
036      DataLog log, String name, Struct<T> struct, String metadata, long timestamp) {
037    return new StructLogEntry<>(log, name, struct, metadata, timestamp);
038  }
039
040  /**
041   * Creates a struct-encoded log entry.
042   *
043   * @param <T> value class (inferred from struct)
044   * @param log datalog
045   * @param name name of the entry
046   * @param struct struct serialization implementation
047   * @param metadata metadata
048   * @return StructLogEntry
049   */
050  public static <T> StructLogEntry<T> create(
051      DataLog log, String name, Struct<T> struct, String metadata) {
052    return create(log, name, struct, metadata, 0);
053  }
054
055  /**
056   * Creates a struct-encoded log entry.
057   *
058   * @param <T> value class (inferred from struct)
059   * @param log datalog
060   * @param name name of the entry
061   * @param struct struct serialization implementation
062   * @param timestamp entry creation timestamp (0=now)
063   * @return StructLogEntry
064   */
065  public static <T> StructLogEntry<T> create(
066      DataLog log, String name, Struct<T> struct, long timestamp) {
067    return create(log, name, struct, "", timestamp);
068  }
069
070  /**
071   * Creates a struct-encoded log entry.
072   *
073   * @param <T> value class (inferred from struct)
074   * @param log datalog
075   * @param name name of the entry
076   * @param struct struct serialization implementation
077   * @return StructLogEntry
078   */
079  public static <T> StructLogEntry<T> create(DataLog log, String name, Struct<T> struct) {
080    return create(log, name, struct, 0);
081  }
082
083  /**
084   * Appends a record to the log.
085   *
086   * @param value Value to record
087   * @param timestamp Time stamp (0 to indicate now)
088   */
089  public void append(T value, long timestamp) {
090    synchronized (m_buf) {
091      ByteBuffer bb = m_buf.write(value);
092      m_log.appendRaw(m_entry, bb, 0, bb.position(), timestamp);
093    }
094  }
095
096  /**
097   * Appends a record to the log.
098   *
099   * @param value Value to record
100   */
101  public void append(T value) {
102    append(value, 0);
103  }
104
105  private final StructBuffer<T> m_buf;
106}