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

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

Added DebugFrame action
Added GetCometStats
Lots of bug fixes

File size: 5.0 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 public static final String SYSTEM = "S";
34
35 public static final String USER = "U";
36
37 public static final String LOAD = "Load";
38
39 public static final String SAVE = "Save";
40
41 public static final String PICKUP = "Pickup";
42
43 public static final String ANCHOR = "Anchor";
44
45 public static final String DELETE = "Delete";
46
47 public static final String TDFC = "TDFC";
48
49 public static final String NEW_FRAMESET = "New Frameset";
50
51 public static final String PICKUP_COPY = "Pickup Copies";
52
53 public static final String ANCHOR_COPY = "Anchor Copies";
54
55 public static final String CONTROL_CHAR = "Control Character";
56
57 public static final String CREATE_TEXT = "Creating Text";
58
59 public static final String INSERTING_TEXT = "Inserting Text";
60
61 public static final String BACK = "Back";
62
63 public static final String FOLLOW_LINK = "Following Link";
64
65 public static final String MERGING = "Merging";
66
67 // These should be the same as used in KMSReader
68 private static String _filename = null;
69
70 // how many spaces the output time should be padded to
71 private static int _padTime = 8;
72
73 private static TimeKeeper _timer = new TimeKeeper();
74
75 /**
76 * Sets the path on disk that the log files will be created in
77 *
78 * @param location
79 * The path on disk to create the log files in.
80 */
81 public static void Init() {
82 if (!org.expeditee.gui.UserSettings.Logging)
83 return;
84
85 File test = new File(org.expeditee.gui.FrameIO.LOGS_DIR);
86 if (!test.exists())
87 test.mkdir();
88
89 _filename = "UserData-" + EasyDateFormat("ddMMMyy-HHmm") + ".txt";
90 }
91
92 /**
93 * Appends the given event to the log file.
94 *
95 * @param origin
96 * The origin of this event, typically SYSTEM
97 * @param type
98 * The Type of this event (LOAD, SAVE, CLICK, etc)
99 * @param comments
100 * Any further comments that should be recorded about the event
101 */
102 public static void Log(String origin, String type, String comments) {
103 String toWrite = "TIME:\t" + _timer.getElapsedStringMillis(_padTime)
104 + " \t\t\t\t" + " - " + origin + " - " + type + " - "
105 + comments;
106 WriteToFile(toWrite);
107 }
108
109 /**
110 * Appends the given event to the log file.
111 *
112 * @param x
113 * The mouse X coordinate when the event occured
114 * @param y
115 * The mouse Y coordinate when the event occured
116 * @param origin
117 * The origin of the event, typically USER
118 * @param type
119 * The type of this event (LOAD, SAVE, CLICK, etc).
120 * @param comments
121 * Any further comments that should be recorded about the event
122 */
123 public static void Log(int x, int y, String origin, String type,
124 String comments) {
125 String toWrite = "TIME:\t" + _timer.getElapsedStringMillis(_padTime)
126 + " \t X:" + x + " Y:" + y + " - " + origin + " - " + type
127 + " - " + comments;
128 WriteToFile(toWrite);
129 }
130
131 /**
132 * Appends a record of the Exception to the log file. The text output is
133 * equal to that output by e.printStackTrace()
134 *
135 * @param e
136 * The Exception to log.
137 */
138 public static void Log(Exception e) {
139 String toWrite = "TIME:" + _timer.getElapsedStringMillis(_padTime)
140 + "\tException Thrown: " + e.getClass().getSimpleName();
141
142 WriteToFile(toWrite);
143
144 StackTraceElement[] st = e.getStackTrace();
145
146 for (StackTraceElement element : st)
147 WriteToFile(element.toString());
148
149 }
150
151 public static String EasyDateFormat(String format, Date date) {
152 return (new SimpleDateFormat(format)).format(date);
153 }
154
155 public static String EasyTimeFormat(Time time) {
156 return (new SimpleDateFormat("KK:mm:ss")).format(time);
157 }
158
159 public static String EasyDateFormat(String format) {
160 return EasyDateFormat(format, new Date());
161 }
162
163 private static void WriteToFile(String toWrite) {
164 if (!org.expeditee.gui.UserSettings.Logging)
165 return;
166
167 // if Init has not been run yet
168 if (_filename == null)
169 Init();
170
171 try {
172 BufferedWriter writer = new BufferedWriter(new FileWriter(
173 org.expeditee.gui.FrameIO.LOGS_DIR + _filename, true));
174 writer.write(toWrite);
175 writer.newLine();
176
177 writer.flush();
178 writer.close();
179 } catch (IOException ioe) {
180 ioe.printStackTrace();
181 }
182
183 }
184
185}
Note: See TracBrowser for help on using the repository browser.