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.epilogue.logging.errors;
006
007import edu.wpi.first.epilogue.logging.ClassSpecificLogger;
008
009/**
010 * An error handler is used by the Logged framework to catch and process any errors that occur
011 * during the logging process. Different handlers can be used in different operating modes, such as
012 * crashing in simulation to identify errors before they make it to a robot, or automatically
013 * disabling loggers if they encounter too many errors on the field to let the robot keep running
014 * while playing a match.
015 */
016@FunctionalInterface
017public interface ErrorHandler {
018  /**
019   * Handles an exception that arose while logging.
020   *
021   * @param exception the exception that occurred
022   * @param logger the logger that was being processed that caused the error to occur
023   */
024  void handle(Throwable exception, ClassSpecificLogger<?> logger);
025
026  /**
027   * Creates an error handler that will immediately re-throw an exception and cause robot code to
028   * exit. This is particularly useful when running in simulation or JUnit tests to identify errors
029   * quickly and safely.
030   *
031   * @return the error handler
032   */
033  static ErrorHandler crashOnError() {
034    return new CrashOnError();
035  }
036
037  /**
038   * Creates an error handler that will print error messages to the console output, but otherwise
039   * allow logging to continue in the future. This can be helpful when errors occur only rarely and
040   * you don't want your robot program to crash or disable future logging.
041   *
042   * @return the error handler
043   */
044  static ErrorHandler printErrorMessages() {
045    return new ErrorPrinter();
046  }
047
048  /**
049   * Creates an error handler that will automatically disable a logger if it encounters too many
050   * errors. Only the error-prone logger(s) will be disabled; loggers that have not encountered any
051   * errors, or encountered fewer than the limit, will continue to be used. Disabled loggers can be
052   * reset by calling {@link LoggerDisabler#reset()} on the handler.
053   *
054   * @param maximumPermissibleErrors the maximum number of errors that a logger is permitted to
055   *     encounter before being disabled.
056   * @return the error handler
057   */
058  static LoggerDisabler disabling(int maximumPermissibleErrors) {
059    return LoggerDisabler.forLimit(maximumPermissibleErrors);
060  }
061}