-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotLogger.java
More file actions
167 lines (152 loc) · 5.27 KB
/
Copy pathRobotLogger.java
File metadata and controls
167 lines (152 loc) · 5.27 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.util.logging.*;
import java.util.HashMap;
public class RobotLogger {
private Logger mainLogger = Logger.getLogger(RobotLogger.class.getName());
private HashMap<String, Field> itemMap = new HashMap<>();
/**
* Accesses the raw logger and makes a statement with a timestamp.
* @param message The message to log
* @param messageLevel The level of the message to log.
*/
/**
* Gets a string timestamp
* @return the timestamp as a string
*/
private String getTimestampString() {
return Long.toString(System.currentTimeMillis());
}
/**
* Logs a message to the log
* @param message The message to log
* @param messageLevel The level of the message to log.
*/
public void logMessage(String message, Level messageLevel) {
mainLogger.log(messageLevel,message);
}
/**
* Adds a handler to the logger.
* @param h The handler to be added.
*/
public void addHandler(Handler h) {
mainLogger.addHandler(h);
}
/**
* Sets the level of messages to be logged.
* @param newLevel The level in question.
*/
public void setLevel(Level newLevel) {
mainLogger.setLevel(newLevel);
}
/**
* Adds a field of any type to the logger.
* @param f The field to log.
*/
public void createField(Field f) {
itemMap.put(f.getName(), f);
}
/**
* Creates or changes a new double field
* @param name The name of the field
* @param value The initial value of the field
* @param level The priority of the field
*/
public void createDoubleField(String name, Level level) {
itemMap.put(name, new DoubleField(name, 0.0, level));
}
/**
* Creates or changes a new boolean field
* @param name The name of the field
* @param value The initial value of the field
* @param level The priority of the field
* @param message
*/
public void createBoolField(String name, Level level) {
itemMap.put(name, new BooleanField(name, false, level));
}
/**
* Creates or changes a new integer field
* @param name The name of the field
* @param value The initial value of the field
* @param level The priority of the field
*/
public void createIntegerField(String name, Level level) {
itemMap.put(name, new IntegerField(name, 0, level));
}
/**
* Creates or changes a new String field
* @param name The name of the field
* @param value The initial value of the field
* @param level The priority of the field
*/
public void createStringField(String name, Level level) {
itemMap.put(name, new StringField(name, "", level));
}
/**
* Sets a field to a value with an optional log message.
* @param name The name of the field you are setting.
* @param value The value to set the field to
*/
public void setDoubleField(String name, double value) {
Field f = itemMap.get(name);
if (f instanceof DoubleField) {
DoubleField df = (DoubleField)f;
df.setValue(value);
logMessage("[VAR]" + "[" + name + "]" + "[" + getTimestampString() + "] " + df.toString() + "\n", df.getLevel());
}
}
/**
* Sets a field to a value with an optional log message.
* @param name The name of the field you are setting.
* @param value The value to set the field to
*/
public void setBoolField(String name, Boolean value) {
Field f = itemMap.get(name);
if (f instanceof BooleanField) {
BooleanField df = (BooleanField)f;
df.setValue(value);
logMessage("[VAR]" + "[" + name + "]" + "[" + getTimestampString() + "] " + df.toString() + "\n", df.getLevel());
}
}
/**
* Sets a field to a value with an optional log message.
* @param name The name of the field you are setting.
* @param value The value to set the field to
*/
public void setStringField(String name, String value) {
Field f = itemMap.get(name);
if (f instanceof StringField) {
StringField df = (StringField)f;
df.setValue(value);
logMessage("[VAR]" + "[" + name + "]" + "[" + getTimestampString() + "] " + df.toString() + "\n", df.getLevel());
}
}
/**
* Sets a field to a value with an optional log message.
* @param name The name of the field you are setting.
* @param value The value to set the field to
*/
public void setIntegerField(String name, int value) {
Field f = itemMap.get(name);
if (f instanceof IntegerField) {
IntegerField df = (IntegerField)f;
df.setValue(value);
logMessage("[VAR]" + "[" + name + "]" + "[" + getTimestampString() + "] " + df.toString() + "\n", df.getLevel());
}
}
/**
* Changes the level of a field
* @param name The name of the field you are changing
* @param level The level to change it to
*/
public void setFieldLevel(String name, Level level) {
itemMap.get(name).setLevel(level);
}
public Field getField(String name) {
return itemMap.get(name);
}
/**
* Flushes all logged updates to a file
*/
public void flush() {
}
}