Changeset 47 in tmcsimulator for trunk/src/tmcsim/paramicslog
- Timestamp:
- 06/28/2016 02:25:42 PM (10 years ago)
- Location:
- trunk/src/tmcsim/paramicslog
- Files:
-
- 1 added
- 2 edited
-
ParamicsLog.java (modified) (10 diffs)
-
ParamicsLogFileHandler.java (added)
-
gui/ParamicsLogGUI.java (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/paramicslog/ParamicsLog.java
r33 r47 10 10 import java.util.logging.Level; 11 11 import java.util.logging.Logger; 12 13 12 import javax.swing.JOptionPane; 14 15 import org.w3c.dom.Element;16 17 13 import tmcsim.common.SimulationException; 18 14 import tmcsim.interfaces.CoordinatorInterface; … … 21 17 /** 22 18 * Logs communication from ParamicsCommunicator to ParamicsSimulator. 23 * 24 * The system property "PARAMICS_LOG_CONFIG" should be set to the path 25 * where theproperties file for this class is located. <br><br>19 * 20 * The system property "PARAMICS_LOG_CONFIG" should be set to the path where the 21 * properties file for this class is located. <br><br> 26 22 * The data for the properties file follows. <br> 27 23 * <code> 28 24 * -----------------------------------------------------------------<br> 29 * Log File The file to write the communication to. 30 * CAD Simulator Host The host that runs the CAD Simulator. 31 * CAD Simulator RMI Port The port on the host that runs the CAD 32 * Simulator where the RMI Coordinator 33 * object is registered to. 25 * Log File The file to write the communication to. CAD Simulator Host The host 26 * that runs the CAD Simulator. CAD Simulator RMI Port The port on the host that 27 * runs the CAD Simulator where the RMI Coordinator object is registered to. 34 28 * -----------------------------------------------------------------<br> 35 29 * Example File: <br> 36 * LogFile=c:\\log.txt 37 * CADSimulatorHost=localhost 38 * CADSimulatorRMIPort=4445 30 * LogFile=c:\\log.txt CADSimulatorHost=localhost CADSimulatorRMIPort=4445 39 31 * -----------------------------------------------------------------<br> 40 32 * </code> 41 * 33 * 42 34 * @author Nathaniel Lehrer 43 * @version 35 * @version 44 36 */ 45 public class ParamicsLog extends Observable 46 { 37 public class ParamicsLog extends Observable 38 { 39 47 40 /** 48 41 * Enmeration containing property names. 42 * 49 43 * @author Nathaniel Lehrer 50 44 */ 51 private enum PROPERTIES 52 { 53 LOG_FILE ("LogFile"), 54 CAD_SIM_HOST ("CADSimulatorHost"),55 CAD_SIM_ PORT ("CADSimulatorRMIPort");56 45 private enum PROPERTIES 46 { 47 48 LOG_FILE("LogFile"), 49 CAD_SIM_HOST("CADSimulatorHost"), 50 CAD_SIM_PORT("CADSimulatorRMIPort"); 57 51 public String name; 58 52 59 53 private PROPERTIES(String n) 60 54 { … … 62 56 } 63 57 } 64 65 private static final String CONFIG_FILE_NAME = "paramics_communicator_logging.properties"; 66 67 /** Error logger.*/58 private static final String CONFIG_FILE_NAME = "paramics_communicator_logging.properties"; 59 /** 60 * Error logger. 61 */ 68 62 private static Logger paramLogger = Logger.getLogger("tmcsim.paramicslog"); 69 70 /** Static instance. */ 63 /** 64 * Static instance. 65 */ 71 66 private static ParamicsLog instance; 72 73 /** Properties object. */ 67 /** 68 * Properties object. 69 */ 74 70 private Properties paramicsLogProp; 75 76 /** File log entries are written to */ 71 /** 72 * File log entries are written to 73 */ 77 74 private File logFile; 78 79 /** Stores the log entries. This contains the same information logFile. */ 75 /** 76 * Stores the log entries. This contains the same information logFile. 77 */ 80 78 private StringBuilder log; 81 82 /** Remote reference to the simulation */ 79 /** 80 * Remote reference to the simulation 81 */ 83 82 private CoordinatorInterface theCoorInt; 84 85 /** Object for synchronizing IO */ 83 /** 84 * Object for synchronizing IO 85 */ 86 86 Object lock; 87 87 88 88 /** 89 89 * Creates the singleton instance of this class. 90 90 */ 91 static { 92 try { 93 if(System.getProperty("CONFIG_DIR") == null){ 94 System.setProperty("CONFIG_DIR", "config"); 95 } 96 97 instance = new ParamicsLog(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); 98 } 99 catch (Exception e) 91 static 92 { 93 try 94 { 95 if (System.getProperty("CONFIG_DIR") == null) 96 { 97 System.setProperty("CONFIG_DIR", "config"); 98 } 99 100 instance = new ParamicsLog(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME); 101 } catch (Exception e) 100 102 { 101 103 instance = new ParamicsLog(); 102 103 paramLogger.logp(Level.WARNING, "ParamicsLog", "static initializer", 104 105 paramLogger.logp(Level.WARNING, "ParamicsLog", "static initializer", 104 106 "Error occured initializing application", e); 105 107 106 JOptionPane.showMessageDialog(null, e.getMessage(), 107 "Error - ParamicsLog will not save log to file.", 108 JOptionPane.ERROR_MESSAGE); 109 } 110 108 JOptionPane.showMessageDialog(null, e.getMessage(), 109 "Error - ParamicsLog will not save log to file.", 110 JOptionPane.ERROR_MESSAGE); 111 } 112 111 113 instance.addObserver(ParamicsLogGUI.getInstance()); 112 114 } 113 114 /** 115 * Creates an instance of ParamicsLog that does not write to a file when 115 116 /** 117 * Creates an instance of ParamicsLog that does not write to a file when 116 118 * writeToLog is called. 117 119 */ 118 private ParamicsLog() { 119 120 private ParamicsLog() 121 { 122 120 123 lock = new Object(); 121 124 log = new StringBuilder(""); 122 125 } 123 124 /** 125 * Creates an instance of ParamicsLog that writes to a file when writeToLog is called. 126 127 /** 128 * Creates an instance of ParamicsLog that writes to a file when writeToLog 129 * is called. 130 * 126 131 * @param propertiesFile 127 132 */ 128 private ParamicsLog(String propertiesFile) { 133 private ParamicsLog(String propertiesFile) 134 { 129 135 this(); 130 136 131 137 String logFile = null; 132 138 String CADSIMHost = null; 133 139 String CADSIMPort = null; 134 135 try { 140 141 try 142 { 136 143 paramicsLogProp = new Properties(); 137 144 paramicsLogProp.load(new FileInputStream(propertiesFile)); 138 145 139 146 if ((logFile = paramicsLogProp.getProperty(PROPERTIES.LOG_FILE.name)) == null) 140 147 { … … 142 149 } 143 150 else if ((CADSIMHost = paramicsLogProp.getProperty(PROPERTIES.CAD_SIM_HOST.name)) == null) 144 { 151 { 145 152 throw new Exception("Properties file missing CAD Simulator host."); 146 153 } … … 149 156 throw new Exception("Properties file missing CAD Simulator RMI port."); 150 157 } 151 158 152 159 try 153 160 { 154 161 connect(CADSIMHost, CADSIMPort); 155 } 156 catch (Exception e) 157 { 158 JOptionPane.showMessageDialog(null, 159 "ParamicsLog: Could not connect to remote Coordinator object.", 160 "Network Error", JOptionPane.ERROR_MESSAGE); 161 } 162 163 try 162 } catch (Exception e) 163 { 164 JOptionPane.showMessageDialog(null, 165 "ParamicsLog: Could not connect to remote Coordinator object.", 166 "Network Error", JOptionPane.ERROR_MESSAGE); 167 } 168 169 try 164 170 { 165 171 createLogFile(logFile); 166 } 167 catch (Exception e) 168 { 169 JOptionPane.showMessageDialog(null, 170 "ParamicsLog: Could not create new log file.", 172 } catch (Exception e) 173 { 174 JOptionPane.showMessageDialog(null, 175 "ParamicsLog: Could not create new log file.", 171 176 "File Error", JOptionPane.ERROR_MESSAGE); 172 177 } 173 174 } catch (Exception e) { 175 176 paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor", 178 179 } catch (Exception e) 180 { 181 182 paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor", 177 183 "Properties file incorrect or missing.", e); 178 179 JOptionPane.showMessageDialog(null, 180 "ParamicsLog: Properties file invalid.", 184 185 JOptionPane.showMessageDialog(null, 186 "ParamicsLog: Properties file invalid.", 181 187 "Invalid Configuration", JOptionPane.ERROR_MESSAGE); 182 188 } 183 189 } 184 190 185 191 /** 186 192 * Creates the log file. 193 * 187 194 * @param filePath The path to the file including the file name. 188 195 * @throws IOException If the log file could not be created. … … 190 197 private void createLogFile(String filePath) throws IOException 191 198 { 192 try { 199 try 200 { 193 201 logFile = new File(filePath); 194 195 if (logFile.exists()) { 202 203 if (logFile.exists()) 204 { 196 205 logFile.delete(); 197 206 } 198 207 199 208 logFile.createNewFile(); 200 201 } catch (Exception e) { 202 209 210 } catch (Exception e) 211 { 212 203 213 logFile = null; 204 205 paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor", 214 215 paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor", 206 216 "Could not create new log file.", e); 207 217 208 218 throw new IOException("Could not create log file."); 209 } 210 } 211 219 } 220 } 221 212 222 /** 213 223 * Connect to the Coordinator's RMI object. 214 * @param hostname Host name of the CAD Simulator. 215 * @param portNumber Port number of the CAD Simulator RMI communication. 216 * @throws SimulationException if there is an error creating the RMI connection. 217 */ 218 private void connect(String hostname, String portNumber) 219 throws SimulationException { 220 224 * 225 * @param hostname Host name of the CAD Simulator. 226 * @param portNumber Port number of the CAD Simulator RMI communication. 227 * @throws SimulationException if there is an error creating the RMI 228 * connection. 229 */ 230 private void connect(String hostname, String portNumber) 231 throws SimulationException 232 { 233 221 234 String coorIntURL = ""; 222 223 try {224 coorIntURL = "rmi://" + hostname + ":" + portNumber + "/coordinator";225 226 theCoorInt = (CoordinatorInterface)Naming.lookup(coorIntURL); 227 }228 catch (Exception e)229 { 230 paramLogger.logp(Level.WARNING, "ParamicsLog", 231 "establishRMIConnection", "Unable to establish RMI " +232 "communication with the CAD Simulator. URL <" + coorIntURL + ">", e);233 } 234 } 235 235 236 try 237 { 238 coorIntURL = "rmi://" + hostname + ":" + portNumber + "/coordinator"; 239 240 theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL); 241 } catch (Exception e) 242 { 243 paramLogger.logp(Level.WARNING, "ParamicsLog", 244 "establishRMIConnection", "Unable to establish RMI " 245 + "communication with the CAD Simulator. URL <" + coorIntURL + ">", e); 246 } 247 } 248 236 249 /** 237 250 * Accessor to the entries in the log. No file IO is used. 251 * 238 252 * @return The entries in the log. 239 253 */ 240 public String getLog() { 241 254 public String getLog() 255 { 256 242 257 return log.toString(); 243 258 } 244 245 /** 246 * Writes an entry to the log. 247 * The simulator time when the message was sent is prepended to the entry.248 * Entries are padded by a blank line before and after them.249 * TODO: Ensure output is written in order of request.259 260 /** 261 * Writes an entry to the log. The simulator time when the message was sent 262 * is prepended to the entry. Entries are padded by a blank line before and 263 * after them. TODO: Ensure output is written in order of request. 264 * 250 265 * @param entry 251 266 */ 252 public void writeToLog(String entry) { 253 267 public void writeToLog(String entry) 268 { 269 254 270 String time = "?"; 255 271 String formattedEntry; 256 272 257 273 if (theCoorInt != null) 258 274 { … … 260 276 { 261 277 time = formatTime(theCoorInt.getCurrentSimulationTime()); 262 } 263 catch (Exception e) 264 { 265 paramLogger.logp(Level.WARNING, "ParamicsLog", 278 } catch (Exception e) 279 { 280 paramLogger.logp(Level.WARNING, "ParamicsLog", 266 281 "RMICommunication", "Unable to communicate with RMI object", e); 267 282 } 268 283 } 269 284 270 285 formattedEntry = "\n" + "<!-- Time written to file: " + time + " -->\n" + entry + "\n"; 271 286 log.append(formattedEntry); 272 287 273 288 if (logFile != null) 274 289 { 275 try 276 { 277 synchronized (lock)290 try 291 { 292 synchronized (lock) 278 293 { 279 294 FileWriter writer = new FileWriter(logFile, true); … … 282 297 writer.close(); 283 298 } 284 } 285 catch (IOException e) 286 { 287 paramLogger.logp(Level.WARNING, "ParamicsLog", "writeToLog", 299 } catch (IOException e) 300 { 301 paramLogger.logp(Level.WARNING, "ParamicsLog", "writeToLog", 288 302 "Could not write to log file.", e); 289 303 } 290 304 } 291 305 292 306 setChanged(); 293 307 notifyObservers(entry); 294 308 } 295 309 296 310 /** 297 311 * Formats the time given in seconds to hh:mm:ss format. 312 * 298 313 * @param time The time in seconds. 299 314 */ 300 p rivateString formatTime(long time)315 public String formatTime(long time) 301 316 { 302 317 long seconds = time % 60; … … 306 321 return padr(hours) + ":" + padr(minutes) + ":" + padr(seconds); 307 322 } 308 323 309 324 private String padr(long n) 310 325 { … … 317 332 } 318 333 } 319 334 320 335 /** 321 336 * Accessor for static instance of this class. 337 * 322 338 * @return The instance of this class. 323 339 */ 324 public static ParamicsLog getInstance() { 325 340 public static ParamicsLog getInstance() 341 { 342 326 343 return instance; 327 344 } -
trunk/src/tmcsim/paramicslog/gui/ParamicsLogGUI.java
r2 r47 1 1 package tmcsim.paramicslog.gui; 2 2 3 import javax.swing.*;4 3 import java.awt.event.WindowEvent; 5 4 import java.util.*; 5 import javax.swing.*; 6 6 7 7 /** 8 * The UI for ParamicsLog. 8 * The UI for ParamicsLog. 9 * 9 10 * @author Nathaniel Lehrer 10 11 * @version 11 12 */ 12 public class ParamicsLogGUI extends JFrame implements Observer { 13 public class ParamicsLogGUI extends JFrame implements Observer 14 { 13 15 14 /** The static instance */ 16 /** 17 * The static instance 18 */ 15 19 private static ParamicsLogGUI instance = new ParamicsLogGUI(); 20 /** 21 * The text area to display the log in 22 */ 23 private JTextArea textArea; 16 24 17 /** The text area to display the log in */18 private JTextArea textArea;19 20 /** Creates an instance of this class */21 public ParamicsLogGUI(){22 25 /** 26 * Creates an instance of this class 27 */ 28 public ParamicsLogGUI() 29 { 30 23 31 setupGUI(); 24 32 } 25 26 /** Creates the UI */ 33 34 /** 35 * Creates the UI 36 */ 27 37 private void setupGUI() 28 38 { 29 try { 39 try 40 { 30 41 UIManager.setLookAndFeel( 31 UIManager.getSystemLookAndFeelClassName()); 32 } catch (Exception e) { 42 UIManager.getSystemLookAndFeelClassName()); 43 } catch (Exception ex) 44 { 45 System.out.println(ex.getMessage()); 33 46 System.err.println("Couldn't use system look and feel."); 34 47 } 35 36 this.addWindowListener(new java.awt.event.WindowAdapter() {37 48 49 this.addWindowListener(new java.awt.event.WindowAdapter() 50 { 38 51 public void windowClosing(WindowEvent e) 39 52 { … … 41 54 } 42 55 }); 43 44 56 57 45 58 setTitle("Paramics Log"); 46 59 47 60 textArea = new JTextArea(); 48 61 textArea.setColumns(60); 49 62 textArea.setRows(30); 50 63 JScrollPane scrollPane = new JScrollPane(textArea); 51 64 52 65 getContentPane().add(scrollPane); 53 66 54 67 } 55 56 /** Shows the UI window */ 68 69 /** 70 * Shows the UI window 71 */ 57 72 public void display() 58 73 { … … 62 77 63 78 /** 64 * Updates the text area. If the observable class given is of type ParamicsLog then 65 * the log entries are displayed. 79 * Updates the text area. If the observable class given is of type 80 * ParamicsLog then the log entries are displayed. 81 * 66 82 * @param o The model for this viewer. 67 83 * @param arg An argument that is not used. … … 72 88 { 73 89 textArea.setText(((tmcsim.paramicslog.ParamicsLog) o).getLog()); 74 } 90 } 75 91 76 92 repaint(); 77 93 } 78 94 79 95 /** 80 96 * Accessor for the instance of ParamicsLogGUI. 97 * 81 98 * @return The instance of ParamicsLogGUI. 82 99 */ 83 public static ParamicsLogGUI getInstance() { 100 public static ParamicsLogGUI getInstance() 101 { 84 102 return instance; 85 103 } 86 87 104 }
Note: See TracChangeset
for help on using the changeset viewer.
