| joelg236 |
30-06-2012 21:57 |
File logging
I have written some code for logging (Driverstation and files), but without a CRIO I am not sure whether it works or not. I am still trying to grasp logging and input/output streams in java, as I have next to no experience with it. Would this code work? And what would work better?
Code:
package frc.team4334.debug;
import com.sun.squawk.microedition.io.FileConnection;
import edu.wpi.first.wpilibj.DriverStationLCD;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
/**
* Logging class. Used to give messages to user in different ways depending on
* urgency.
*
* @author Joel Gallant
*/
public class Log {
/**
* Class representing the urgency of the log.
*/
public static class urgency {}
/**
* Urgency values.
*/
public final static urgency URGENT = new urgency(),
WARNING = new urgency(),
USERMESSAGE = new urgency(),
STATUSREPORT = new urgency(),
LOG = new urgency();
/**
* Logs a message, and depending on the urgency given, reacts appropriately.
*
* @param urgency Urgency of the log. Urgencies are available as static
* variables in the {@link Log Log} class.
* @param msg Message to send to the user.
*/
public static void log(urgency urgency, String msg) {
String usrMsg = msg;
if(urgency == URGENT) {
usrMsg.toUpperCase();
dsLCD("!!! URGENT !!! : "+usrMsg);
dsLCD("IT IS ADVISED TO TURN THE ROBOT OFF");
usrMsg = "!!! URGENT !!! : "+usrMsg+"\n"+"IT IS ADVISED TO TURN THE ROBOT OFF";
}else if(urgency == WARNING) {
usrMsg.toUpperCase();
usrMsg = "WARNING : "+usrMsg;
dsLCD(usrMsg);
}else if(urgency == USERMESSAGE) {
dsLCD(usrMsg);
}else if(urgency == STATUSREPORT) {
usrMsg = "STATUS : "+msg;
dsLCD("STATUS : "+msg);
}
// All logs are logged
resume();
makeLog();
write(usrMsg);
pause();
}
/**
* Writes the specified string (plus a newline) into the log file.
*
* @param output The string to be written.
*/
private static synchronized void write(String output){
String kOutput = output + "\n";
try {
if (kDos != null)
kDos.write(kOutput.getBytes(), 0, kOutput.getBytes().length);
} catch (IOException e){
System.out.println("Unable to write to log.");
}
}
private static final String PATH = "file:///log.txt";
private static DataOutputStream kDos;
private static FileConnection kConnection;
private static void makeLog() {
try {
if (kDos != null) {
kDos.close();
kDos = null;
}
if (kConnection != null) {
kConnection.close();
kConnection = null;
}
kConnection = (FileConnection) Connector.open(PATH, Connector.READ_WRITE);
if (kConnection.exists()) {
kConnection.delete();
kConnection.close();
kConnection = (FileConnection)Connector.open(PATH, Connector.READ_WRITE);
}
kConnection.create();
kDos = kConnection.openDataOutputStream();
} catch (IOException e) {
System.out.println("Unable to create file log.");
}
}
/**
* Closes the file temporarily for other file writing operations
*/
private static synchronized void pause(){
try {
kConnection.close();
kDos.close();
} catch (IOException e){
Log.write(e.getMessage());
}
}
/**
* Reopens the last file for resumed logging functionality
*/
private static synchronized void resume(){
try {
kConnection.create();
kDos = kConnection.openDataOutputStream();
} catch (IOException e){
Log.write(e.getMessage());
}
}
private static int lineNum = 1;
private static void dsLCD(String msg) {
DriverStationLCD.Line line;
switch(lineNum) {
case(1):
line = DriverStationLCD.Line.kMain6;
break;
case(2):
line = DriverStationLCD.Line.kUser2;
break;
case(3):
line = DriverStationLCD.Line.kUser3;
break;
case(4):
line = DriverStationLCD.Line.kUser4;
break;
case(5):
line = DriverStationLCD.Line.kUser5;
break;
case(6):
line = DriverStationLCD.Line.kUser6;
break;
default:
line = DriverStationLCD.Line.kMain6;
}
DriverStationLCD.getInstance().println(line, lineNum, msg+" ");
DriverStationLCD.getInstance().updateLCD();
lineNum++;
if(lineNum > 6) {
lineNum = 1;
}
}
}
|