source: trunk/src/org/expeditee/io/Logger.java@ 417

Last change on this file since 417 was 417, checked in by ra33, 16 years ago

Fixed formatting problem for times.. which did not work for times greater than 12 hours

File size: 5.4 KB
Line 
1package org.expeditee.io;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.sql.Time;
8import java.text.SimpleDateFormat;
9import java.util.Date;
10
11import org.expeditee.gui.TimeKeeper;
12
13/**
14 * This class is used to create all log files. The log file is opened, appended,
15 * and closed for each new entry to ensure robustness against program crashes.
16 * Files are created with filename: UserData.Date.Time<br>
17 * Where Date is in the format DDMMMYYYY and Time is in the format HHMM. <br>
18 * <br>
19 * Log files are stored in the following format: <br>
20 * <code>
21 * TIME: (Time since program start) <br>
22 * X,Y (For user events only) <br>
23 * Origin (System/User) <br>
24 * Description <br>
25 * </code> <br>
26 * Exceptions can also be logged, in which case the result from
27 * Exception.printStackTrace() is output to the file.
28 *
29 * @author jdm18
30 *
31 */
32public class Logger {
33
34 public static final String SHORT_DATE_FORMAT = "ddMMMyyyy";
35
36 public static final String TIME_DATE_FORMAT = "[HH:mm]";
37
38 public static final String LONG_DATE_FORMAT = "ddMMMyyyy[HH:mm]";
39
40 public static final String SYSTEM = "S";
41
42 public static final String USER = "U";
43
44 public static final String LOAD = "Load";
45
46 public static final String SAVE = "Save";
47
48 public static final String PICKUP = "Pickup";
49
50 public static final String ANCHOR = "Anchor";
51
52 public static final String DELETE = "Delete";
53
54 public static final String TDFC = "TDFC";
55
56 public static final String NEW_FRAMESET = "New Frameset";
57
58 public static final String PICKUP_COPY = "Pickup Copies";
59
60 public static final String ANCHOR_COPY = "Anchor Copies";
61
62 public static final String CONTROL_CHAR = "Control Character";
63
64 public static final String CREATE_TEXT = "Creating Text";
65
66 public static final String INSERTING_TEXT = "Inserting Text";
67
68 public static final String BACK = "Back";
69
70 public static final String FOLLOW_LINK = "Following Link";
71
72 public static final String MERGING = "Merging";
73
74 // These should be the same as used in KMSReader
75 private static String _filename = null;
76
77 // how many spaces the output time should be padded to
78 private static int _padTime = 8;
79
80 private static TimeKeeper _timer = new TimeKeeper();
81
82 /**
83 * Sets the path on disk that the log files will be created in
84 *
85 * @param location
86 * The path on disk to create the log files in.
87 */
88 public static void Init() {
89 if (!org.expeditee.gui.UserSettings.Logging)
90 return;
91
92 File test = new File(org.expeditee.gui.FrameIO.LOGS_DIR);
93 if (!test.exists())
94 test.mkdir();
95
96 _filename = "UserData-" + EasyDateFormat("ddMMMyy-HHmm") + ".txt";
97 }
98
99 /**
100 * Appends the given event to the log file.
101 *
102 * @param origin
103 * The origin of this event, typically SYSTEM
104 * @param type
105 * The Type of this event (LOAD, SAVE, CLICK, etc)
106 * @param comments
107 * Any further comments that should be recorded about the event
108 */
109 public static void Log(String origin, String type, String comments) {
110 String toWrite = "TIME:\t" + _timer.getElapsedStringMillis(_padTime)
111 + " \t\t\t\t" + " - " + origin + " - " + type + " - "
112 + comments;
113 WriteToFile(toWrite);
114 }
115
116 /**
117 * Appends the given event to the log file.
118 *
119 * @param x
120 * The mouse X coordinate when the event occured
121 * @param y
122 * The mouse Y coordinate when the event occured
123 * @param origin
124 * The origin of the event, typically USER
125 * @param type
126 * The type of this event (LOAD, SAVE, CLICK, etc).
127 * @param comments
128 * Any further comments that should be recorded about the event
129 */
130 public static void Log(int x, int y, String origin, String type,
131 String comments) {
132 String toWrite = "TIME:\t" + _timer.getElapsedStringMillis(_padTime)
133 + " \t X:" + x + " Y:" + y + " - " + origin + " - " + type
134 + " - " + comments;
135 WriteToFile(toWrite);
136 }
137
138 /**
139 * Appends a record of the Exception to the log file. The text output is
140 * equal to that output by e.printStackTrace()
141 *
142 * @param e
143 * The Exception to log.
144 */
145 public static void Log(Exception e) {
146 String toWrite = "TIME:" + _timer.getElapsedStringMillis(_padTime)
147 + "\tException Thrown: " + e.getClass().getSimpleName();
148
149 WriteToFile(toWrite);
150
151 StackTraceElement[] st = e.getStackTrace();
152
153 for (StackTraceElement element : st)
154 WriteToFile(element.toString());
155
156 }
157
158 public static String EasyDateFormat(String format, Date date) {
159 return (new SimpleDateFormat(format)).format(date);
160 }
161
162 public static String EasyTimeFormat(Time time) {
163 /*
164 * Truncate the millis
165 */
166 long total = time.getTime() / 1000;
167 long seconds = total % 60;
168 // Truncate the secs
169 total /= 60;
170 long minutes = total % 60;
171 // Truncate the minutes
172 long hours = total / 60;
173
174 return String.format("%1$02d:%2$02d:%3$02d", hours, minutes, seconds);
175 }
176
177 public static String EasyDateFormat(String format) {
178 return EasyDateFormat(format, new Date());
179 }
180
181 private static void WriteToFile(String toWrite) {
182 if (!org.expeditee.gui.UserSettings.Logging)
183 return;
184
185 // if Init has not been run yet
186 if (_filename == null)
187 Init();
188
189 try {
190 BufferedWriter writer = new BufferedWriter(new FileWriter(
191 org.expeditee.gui.FrameIO.LOGS_DIR + _filename, true));
192 writer.write(toWrite);
193 writer.newLine();
194
195 writer.flush();
196 writer.close();
197 } catch (IOException ioe) {
198 ioe.printStackTrace();
199 }
200
201 }
202
203}
Note: See TracBrowser for help on using the repository browser.