From a6ef8d28bcc365aff31de385cfd105d776c5a912 Mon Sep 17 00:00:00 2001 From: Sriman Achanta <68172138+srimanachanta@users.noreply.github.com> Date: Thu, 14 Dec 2023 23:02:40 -0500 Subject: [PATCH] Move getLogPath to LoggerUtils uses optional as well for readability --- src/main/java/frc/robot/Robot.java | 8 ++------ src/main/java/frc/robot/util/LoggerUtil.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/frc/robot/Robot.java b/src/main/java/frc/robot/Robot.java index f7916d0..012d50d 100644 --- a/src/main/java/frc/robot/Robot.java +++ b/src/main/java/frc/robot/Robot.java @@ -4,7 +4,6 @@ import edu.wpi.first.wpilibj2.command.CommandScheduler; import frc.robot.constants.Constants; import frc.robot.util.LoggerUtil; -import java.nio.file.Path; import org.littletonrobotics.junction.LogFileUtil; import org.littletonrobotics.junction.LoggedRobot; import org.littletonrobotics.junction.Logger; @@ -24,11 +23,8 @@ public void robotInit() { switch (Constants.getRobotMode()) { case REAL: // Running on a real robot, log to a USB stick - var usbPath = Path.of("/U"); - // Check if the USB is plugged in, log to it if so - if (usbPath.toFile().exists()) { - Logger.addDataReceiver(new WPILOGWriter(usbPath.toString())); - } + LoggerUtil.getLogPath() + .ifPresent(p -> Logger.addDataReceiver(new WPILOGWriter(p.toString()))); Logger.addDataReceiver(new NT4Publisher()); break; case SIM: diff --git a/src/main/java/frc/robot/util/LoggerUtil.java b/src/main/java/frc/robot/util/LoggerUtil.java index fbdae2d..5a3b785 100644 --- a/src/main/java/frc/robot/util/LoggerUtil.java +++ b/src/main/java/frc/robot/util/LoggerUtil.java @@ -3,6 +3,8 @@ import edu.wpi.first.wpilibj.RobotBase; import frc.generated.BuildConstants; import frc.robot.constants.Constants; +import java.nio.file.Path; +import java.util.Optional; import org.littletonrobotics.junction.Logger; public class LoggerUtil { @@ -24,4 +26,20 @@ public static void initializeLoggerMetadata() { default -> Logger.recordMetadata("GIT_STATUS", "Unknown"); } } + + /** + * Get the path to the logging directory. Returns Empty if the USB drive is not plugged into the + * robot. + * + * @return logging path. Empty if the drive is not plugged in. + */ + public static Optional getLogPath() { + var usbPath = Path.of("/U"); + // Return USB path if it is plugged in + if (usbPath.toFile().exists()) { + return Optional.of(usbPath); + } + + return Optional.empty(); + } }