-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingCommand.java
More file actions
141 lines (117 loc) · 4.17 KB
/
LoggingCommand.java
File metadata and controls
141 lines (117 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package frc.robot.utils.logging;
import edu.wpi.first.util.datalog.BooleanLogEntry;
import edu.wpi.first.wpilibj.DataLogManager;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Subsystem;
import frc.robot.constants.Constants;
import java.util.Set;
public abstract class LoggingCommand extends Command {
// Prefix for the command name (appended ahead of the command name) - this can be appended to when the command gets
// embedded at another command through appendNamePrefix
private String namePrefix;
// Private name for the command (last in the hierarchy for this command) - this can be changed through setName
private String commandName;
// The delegate command where we refer all calls
private Command underlying;
// The name as it would be used in the logs
private String fullyQualifiedName;
// Lazy initialized log entry to make sure we don't create it until it is needed (and command is scheduled)
// otherwise we get an empty line in the log visualization tool
private BooleanLogEntry logEntry;
public LoggingCommand(String namePrefix, String commandName, Command underlying) {
this.namePrefix = namePrefix;
this.commandName = commandName;
this.underlying = underlying;
resetLoggingName(namePrefix, commandName);
}
/**
* A special constructor to allow for the creation of the command when we can't create the underlying up front.
* It is assumed that the {@link #setUnderlying(Command)} method is called immediately following the construction.
*/
protected LoggingCommand(String namePrefix, String commandName) {
this.namePrefix = namePrefix;
this.commandName = commandName;
resetLoggingName(namePrefix, commandName);
}
/**
* Second phase of initialization - set the underlying command
*/
protected void setUnderlying(Command underlying) {
this.underlying = underlying;
}
@Override
public void initialize() {
if (Constants.ENABLE_LOGGING) getLoggingEntry().append(true);
underlying.initialize();
}
@Override
public void execute() {
underlying.execute();
}
@Override
public void end(boolean interrupted) {
if (Constants.ENABLE_LOGGING) underlying.end(interrupted);
getLoggingEntry().append(false);
}
@Override
public boolean isFinished() {
return underlying.isFinished();
}
@Override
public Set<Subsystem> getRequirements() {
return underlying.getRequirements();
}
@Override
public String getName() {
return underlying.getName();
}
@Override
public void setName(String name) {
underlying.setName(name);
this.commandName = name;
resetLoggingName(namePrefix, name);
}
@Override
public String getSubsystem() {
return underlying.getSubsystem();
}
@Override
public void setSubsystem(String subsystem) {
underlying.setSubsystem(subsystem);
}
@Override
public boolean runsWhenDisabled() {
return underlying.runsWhenDisabled();
}
public String getNamePrefix() {
return namePrefix;
}
/**
* Add a new prefix in front of the current one
* @param prefix the new prefix
*/
public void appendNamePrefix(String prefix) {
this.namePrefix = prefix + CommandUtil.NAME_SEPARATOR + namePrefix;
resetLoggingName(namePrefix, commandName);
}
public Command getUnderlying() {
return underlying;
}
public String getFullyQualifiedName() {
return fullyQualifiedName;
}
private void resetLoggingName(String namePrefix, String name) {
fullyQualifiedName = CommandUtil.COMMAND_PREFIX + CommandUtil.NAME_SEPARATOR +
((namePrefix == null) ? name : namePrefix + CommandUtil.NAME_SEPARATOR + name);
if (logEntry != null) {
logEntry.finish();
logEntry = null;
}
}
private BooleanLogEntry getLoggingEntry() {
if (logEntry == null) {
logEntry = new BooleanLogEntry(DataLogManager.getLog(), fullyQualifiedName);
}
return logEntry;
}
}