source: trunk/org/expeditee/io/Logger.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

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