Go to Post Remember: "It's not the strongest that survive, but the most adaptable to change" - Daniel_LaFleur [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 06-30-2012, 09:57 PM
joelg236 joelg236 is offline
4334 Retired Mentor & Alumni
AKA: Joel Gallant
no team
Team Role: Mentor
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 733
joelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond repute
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;
        }
    }
}
__________________
All opinions are my own.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 07:26 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi