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

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

Added Spell Checker
Added word count stats
Fixed some mail stuff

File size: 5.1 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 return (new SimpleDateFormat("KK:mm:ss")).format(time);
164 }
165
166 public static String EasyDateFormat(String format) {
167 return EasyDateFormat(format, new Date());
168 }
169
170 private static void WriteToFile(String toWrite) {
171 if (!org.expeditee.gui.UserSettings.Logging)
172 return;
173
174 // if Init has not been run yet
175 if (_filename == null)
176 Init();
177
178 try {
179 BufferedWriter writer = new BufferedWriter(new FileWriter(
180 org.expeditee.gui.FrameIO.LOGS_DIR + _filename, true));
181 writer.write(toWrite);
182 writer.newLine();
183
184 writer.flush();
185 writer.close();
186 } catch (IOException ioe) {
187 ioe.printStackTrace();
188 }
189
190 }
191
192}
Note: See TracBrowser for help on using the repository browser.