Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

log WPILogs to disk only #109

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/main/java/com/team766/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import com.team766.config.ConfigFileReader;
import com.team766.library.CircularBuffer;
import edu.wpi.first.util.datalog.StringLogEntry;
import edu.wpi.first.wpilibj.DataLogManager;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.concurrent.atomic.AtomicReference;

public final class Logger {

private static boolean alsoLogToDataLog = false;
private static AtomicReference<StringLogEntry> wpiLogEntryReference =
new AtomicReference<StringLogEntry>();

private static class LogUncaughtException implements Thread.UncaughtExceptionHandler {
public void uncaughtException(final Thread t, final Throwable e) {
Expand Down Expand Up @@ -73,7 +76,12 @@ public void uncaughtException(final Thread t, final Throwable e) {
}

public static void enableLoggingToDataLog(boolean enabled) {
alsoLogToDataLog = enabled;
if (enabled) {
wpiLogEntryReference.compareAndSet(
null, new StringLogEntry(DataLogManager.getLog(), "/maroon/logs"));
} else {
wpiLogEntryReference.set(null);
}
}

public static Logger get(final Category category) {
Expand All @@ -99,7 +107,7 @@ private Logger(final Category category) {
}

public static boolean isLoggingToDataLog() {
return alsoLogToDataLog;
return wpiLogEntryReference.get() != null;
}

public Collection<LogEntry> recentEntries() {
Expand All @@ -125,8 +133,9 @@ public void logData(final Severity severity, final String format, final Object..
if (m_logWriter != null) {
m_logWriter.logStoredFormat(entry);
}
if (alsoLogToDataLog && (severity.compareTo(Severity.INFO) >= 0)) {
DataLogManager.log(message);
StringLogEntry stringLogEntry = wpiLogEntryReference.get();
if (stringLogEntry != null && (severity.compareTo(Severity.INFO) >= 0)) {
stringLogEntry.append(message);
}
}

Expand All @@ -142,8 +151,9 @@ public void logRaw(final Severity severity, final String message) {
if (m_logWriter != null) {
m_logWriter.log(entry);
}
if (alsoLogToDataLog && (severity.compareTo(Severity.INFO) >= 0)) {
DataLogManager.log(message);
StringLogEntry stringLogEntry = wpiLogEntryReference.get();
if (stringLogEntry != null && (severity.compareTo(Severity.INFO) >= 0)) {
stringLogEntry.append(message);
}
}

Expand Down
Loading