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
007/**
008 * A data log background writer that periodically flushes the data log on a background thread. The
009 * data log file is created immediately upon construction with a temporary filename. The file may be
010 * renamed at any time using the setFilename() function.
011 *
012 * <p>The data log is periodically flushed to disk. It can also be explicitly flushed to disk by
013 * using the flush() function. This operation is, however, non-blocking.
014 */
015public final class DataLogBackgroundWriter extends DataLog {
016  /**
017   * Construct a new Data Log. The log will be initially created with a temporary filename.
018   *
019   * @param dir directory to store the log
020   * @param filename filename to use; if none provided, a random filename is generated of the form
021   *     "wpilog_{}.wpilog"
022   * @param period time between automatic flushes to disk, in seconds; this is a time/storage
023   *     tradeoff
024   * @param extraHeader extra header data
025   */
026  public DataLogBackgroundWriter(String dir, String filename, double period, String extraHeader) {
027    super(DataLogJNI.bgCreate(dir, filename, period, extraHeader));
028  }
029
030  /**
031   * Construct a new Data Log. The log will be initially created with a temporary filename.
032   *
033   * @param dir directory to store the log
034   * @param filename filename to use; if none provided, a random filename is generated of the form
035   *     "wpilog_{}.wpilog"
036   * @param period time between automatic flushes to disk, in seconds; this is a time/storage
037   *     tradeoff
038   */
039  public DataLogBackgroundWriter(String dir, String filename, double period) {
040    this(dir, filename, period, "");
041  }
042
043  /**
044   * Construct a new Data Log. The log will be initially created with a temporary filename.
045   *
046   * @param dir directory to store the log
047   * @param filename filename to use; if none provided, a random filename is generated of the form
048   *     "wpilog_{}.wpilog"
049   */
050  public DataLogBackgroundWriter(String dir, String filename) {
051    this(dir, filename, 0.25);
052  }
053
054  /**
055   * Construct a new Data Log. The log will be initially created with a temporary filename.
056   *
057   * @param dir directory to store the log
058   */
059  public DataLogBackgroundWriter(String dir) {
060    this(dir, "", 0.25);
061  }
062
063  /** Construct a new Data Log. The log will be initially created with a temporary filename. */
064  public DataLogBackgroundWriter() {
065    this("");
066  }
067
068  /**
069   * Change log filename.
070   *
071   * @param filename filename
072   */
073  public void setFilename(String filename) {
074    DataLogJNI.bgSetFilename(m_impl, filename);
075  }
076
077  /**
078   * Resumes appending of data records to the log. If called after stop(), opens a new file (with
079   * random name if SetFilename was not called after stop()) and appends Start records and schema
080   * data values for all previously started entries and schemas.
081   */
082  @Override
083  public void resume() {
084    DataLogJNI.resume(m_impl);
085  }
086}