RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Logger.java
1/*
2 * Copyright (C) 2016 John H. Gaby
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Contact: robotics@gabysoft.com
17 */
18
19package robotCore;
20
21import java.io.FileNotFoundException;
22import java.io.PrintWriter;
23import java.text.DateFormat;
24import java.text.SimpleDateFormat;
25import java.util.ArrayList;
26import java.util.Date;
27
28import org.wpilib.hardware.hal.HALUtil;
29
30import robotCore.Network.NetworkReceiver;
31
52
53public class Logger
54{
55 private static Object m_lock = new Object();
56 private final static String m_traceName = "trace";
57 private final static String m_logDir = "/home/pi/logs/";
58 private static PrintWriter m_file = null;
59 private static int m_level = -999; // By default, use logging level based on the type
60 private static long m_startTime = HALUtil.getMonotonicMilliseconds();
61 private static Network m_network = null;
62
63 static private PrintWriter createLog(String name)
64 {
65 DateFormat dateFormat = new SimpleDateFormat("MM-dd-HH-mm-ss");
66 Date date = new Date();
67 String path = m_logDir + name + dateFormat.format(date) + ".txt";
68 PrintWriter file = null;
69
70 try
71 {
72 file = new PrintWriter(path);
73 }
74 catch (FileNotFoundException e)
75 {
76 e.printStackTrace();
77 }
78
79 return(file);
80 }
81
82 static private class LogType
83 {
84 private String m_name = null;
85 private int m_level = 0;
86 private PrintWriter m_file = null;
87 private boolean m_logToConsole = true;
88 private boolean m_logTime = false;
89
90 public LogType(String name)
91 {
92 m_name = name;
93
94 if (m_name.equals(m_traceName))
95 {
96 m_logTime = true;
97 }
98 }
99
100 public void closeLog()
101 {
102 if (m_file != null);
103 {
104 m_file.close();
105 m_file = null;
106 }
107 }
108
109 public void createLog(String name, boolean logTime, boolean logToConsole)
110 {
111 m_logToConsole = logToConsole;
112 m_logTime = logTime;
113
114 if (m_file != null)
115 {
116 m_file.close();
117 }
118
119 m_file = Logger.createLog(name);
120 }
121
122 public void setLogLevel(int level)
123 {
124 m_level = level;
125 }
126 }
127
128 static private ArrayList<LogType> m_types = new ArrayList<LogType>();
129
130 static private LogType findType(String tag)
131 {
132 if (tag == null)
133 {
134 tag = m_traceName;
135 }
136
137 for (LogType type : m_types)
138 {
139 if (type.m_name.equals(tag))
140 {
141 return(type);
142 }
143 }
144
145 /*
146 * Tag not found, add it
147 */
148 Logger.LogType newType = new Logger.LogType(tag);
149 m_types.add(newType);
150
151 return(newType);
152 }
153
161 static public void log(String tag, int level, String message, boolean noLogToConsole)
162 {
163 synchronized(m_lock)
164 {
165 if (level >= m_level)
166 {
167 LogType type = findType(tag);
168
169 if (level >= type.m_level)
170 {
171 PrintWriter file;
172 boolean logTime;
173
174 if (type.m_file == null)
175 {
176 file = m_file;
177 logTime = true; // Always log the time to the master file
178 }
179 else
180 {
181 file = type.m_file;
182 logTime = type.m_logTime;
183 }
184
185 if (file != null)
186 {
187// System.out.println("Log: logTime = " + logTime);
188 if (logTime)
189 {
190 // file.print(getElapsedTime() + ":" + type.m_name + "(" + level + "):");
191 file.print(String.format("%d:%d:%s(%d)", getElapsedTime(), Thread.currentThread().getId(), type.m_name, level));
192 }
193
194 file.println(message + "\r");
195 file.flush();
196 }
197
198 if ((file == null) || (type.m_logToConsole && !noLogToConsole))
199 {
200 String log = "" + getElapsedTime() + ":" + type.m_name + "(" + level + "):" + message;
201 System.out.println(log);
202
203 if (m_network != null)
204 {
205// String msg = log + "\r\n" + "xxx";
206// System.out.print("network: " + msg);
207 m_network.sendMessage(log);
208 }
209
210// System.out.print(GetElapsedTime() + ":"); // Always log time to console
211// System.out.println(type.m_name + "(" + level + "):" + message);
212 }
213 }
214 }
215 }
216 }
217
218 static public void log(String tag, int level, String message)
219 {
220 log(tag, level, message, false);
221 }
226 static public void setLogLevel(int level)
227 {
228 synchronized(m_lock)
229 {
230 m_level = level;
231 }
232 }
233
239 static public void setLogLevel(String tag, int level)
240 {
241 synchronized(m_lock)
242 {
243 findType(tag).setLogLevel(level);
244 }
245 }
246
251 static public void setLogFile(String name)
252 {
253 synchronized(m_lock)
254 {
255 if (m_file != null)
256 {
257 m_file.close();
258 }
259
260 m_file = createLog(name);
261 }
262 }
263
272 static public void setLogFile(String tag, String name)
273 {
274 setLogFile(tag, name, false, false);
275 }
276
286 static public void setLogFile(String tag, String name, boolean logTime)
287 {
288 setLogFile(tag, name, logTime, false);
289 }
290
298 static public void setLogFile(String tag, String name, boolean logTime, boolean logToConsole)
299 {
300 synchronized(m_lock)
301 {
302 findType(tag).createLog(name, logTime, logToConsole);
303 }
304 }
305
309 static public void closeLogFile()
310 {
311 synchronized(m_lock)
312 {
313 if (m_file != null)
314 {
315 m_file.close();
316 }
317 }
318 }
319
325 static public void closeLogFile(String tag)
326 {
327 synchronized(m_lock)
328 {
329 findType(tag).closeLog();
330 }
331 }
332
337 static public void closeAllLogFiles()
338 {
339 closeLogFile();
340
341 synchronized(m_lock)
342 {
343 for (LogType type : m_types)
344 {
345 type.closeLog();
346 }
347 }
348 }
349
354 static public long getElapsedTime()
355 {
356 synchronized(m_lock)
357 {
358 return(HALUtil.getMonotonicMilliseconds() - m_startTime);
359 }
360 }
361
366 static public void resetElapsedTime()
367 {
368 synchronized(m_lock)
369 {
370 m_startTime = HALUtil.getMonotonicMilliseconds();
371 }
372 }
373
374 static private class Receiver implements NetworkReceiver
375 {
376 @Override
377 public void processData(String command)
378 {
379 }
380
381 @Override
382 public void connected() {
383 }
384
385 @Override
386 public void disconnected() {
387 }
388 }
389
395 static public void startLoggingServer()
396 {
397 if (m_network == null)
398 {
399 m_network = new Network();
400
401 m_network.listen(new Receiver(), 5810);
402 }
403 }
404}
The Logger class provides a mechanism to log messages.
Definition Logger.java:54
static void log(String tag, int level, String message, boolean noLogToConsole)
Definition Logger.java:161
static void resetElapsedTime()
Definition Logger.java:366
static void setLogFile(String name)
Definition Logger.java:251
static void setLogLevel(String tag, int level)
Definition Logger.java:239
static void closeLogFile()
Definition Logger.java:309
static void setLogLevel(int level)
Definition Logger.java:226
static void setLogFile(String tag, String name, boolean logTime)
Definition Logger.java:286
static void startLoggingServer()
Definition Logger.java:395
static long getElapsedTime()
Definition Logger.java:354
static void closeAllLogFiles()
Definition Logger.java:337
static void setLogFile(String tag, String name)
Definition Logger.java:272
static void closeLogFile(String tag)
Definition Logger.java:325
static void setLogFile(String tag, String name, boolean logTime, boolean logToConsole)
Definition Logger.java:298
The Network class handles network connections.
Definition Network.java:45
The NetworkReceiver interface provides a callback for received data on a network connection.
Definition Network.java:68