source: trunk/src/org/expeditee/stats/Logger.java@ 1510

Last change on this file since 1510 was 1274, checked in by bln4, 5 years ago

Consistency of FrameIO path names

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