f@0
|
1 /*
|
f@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
f@0
|
3
|
f@0
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
f@0
|
5
|
f@0
|
6 This program is free software: you can redistribute it and/or modify
|
f@0
|
7 it under the terms of the GNU General Public License as published by
|
f@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
f@0
|
9 (at your option) any later version.
|
f@0
|
10
|
f@0
|
11 This program is distributed in the hope that it will be useful,
|
f@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@0
|
14 GNU General Public License for more details.
|
f@0
|
15
|
f@0
|
16 You should have received a copy of the GNU General Public License
|
f@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@0
|
18 */
|
f@0
|
19
|
f@0
|
20 package uk.ac.qmul.eecs.ccmi.utils;
|
f@0
|
21
|
f@0
|
22 import java.io.IOException;
|
f@0
|
23 import java.text.SimpleDateFormat;
|
f@0
|
24 import java.util.Date;
|
f@0
|
25 import java.util.logging.FileHandler;
|
f@0
|
26 import java.util.logging.Formatter;
|
f@0
|
27 import java.util.logging.Level;
|
f@0
|
28 import java.util.logging.LogRecord;
|
f@0
|
29 import java.util.logging.Logger;
|
f@0
|
30
|
f@0
|
31 /**
|
f@0
|
32 * A logger class using the {@code java.util.logging} package to log all the user's
|
f@0
|
33 * relevant actions.
|
f@0
|
34 */
|
f@0
|
35 public class InteractionLog {
|
f@0
|
36 /**
|
f@0
|
37 * Enable the logging
|
f@0
|
38 * @param logFileDir the path of the directory where the log file will be saved
|
f@0
|
39 * @throws IOException if a I/O problem occurs when writing log entry to the log file
|
f@0
|
40 */
|
f@0
|
41 public static void enable(String logFileDir) throws IOException{
|
f@0
|
42 logger.setLevel(Level.FINE);
|
f@0
|
43 logger.setUseParentHandlers(false);
|
f@0
|
44 if(fileHandler == null){
|
f@0
|
45 fileHandler = new FileHandler(logFileDir+System.getProperty("file.separator")+"interaction%u.log",true);
|
f@0
|
46 fileHandler.setFormatter(new InteractionLogFormatter());
|
f@0
|
47 logger.addHandler(fileHandler);
|
f@0
|
48
|
f@0
|
49 /* also print the log on the console */
|
f@0
|
50 java.util.logging.ConsoleHandler ch = new java.util.logging.ConsoleHandler();
|
f@0
|
51 ch.setLevel(Level.ALL);
|
f@0
|
52 ch.setFormatter(new InteractionLogFormatter());
|
f@0
|
53 logger.addHandler(ch);
|
f@0
|
54 }
|
f@0
|
55 }
|
f@0
|
56
|
f@0
|
57 /**
|
f@0
|
58 * Disable the logging
|
f@0
|
59 */
|
f@0
|
60 public static void disable(){
|
f@0
|
61 logger.setLevel(Level.OFF);
|
f@0
|
62 }
|
f@0
|
63
|
f@0
|
64 /**
|
f@0
|
65 * Logs a log entry in the file. Log entries are action that occurred during
|
f@0
|
66 * the interaction by local or remote user.
|
f@0
|
67 *
|
f@0
|
68 * @param source the source of the interaction
|
f@0
|
69 * @param action the occurred action
|
f@0
|
70 * @param args further informations about the occurred action
|
f@0
|
71 */
|
f@0
|
72 public static void log(String source, String action, String args){
|
f@0
|
73 StringBuilder builder = new StringBuilder(source);
|
f@0
|
74 builder.append(SEPARATOR)
|
f@0
|
75 .append(action)
|
f@0
|
76 .append(SEPARATOR)
|
f@0
|
77 .append(args);
|
f@0
|
78
|
f@0
|
79 logger.fine(builder.toString());
|
f@0
|
80 }
|
f@0
|
81
|
f@0
|
82 /**
|
f@0
|
83 * Logs a general message in the log file. This log entries are not
|
f@0
|
84 * linked to specific action of a local or remote user.
|
f@0
|
85 * @param msg the message to log
|
f@0
|
86 */
|
f@0
|
87 public static void log(String msg){
|
f@0
|
88 logger.config(msg);
|
f@0
|
89 }
|
f@0
|
90
|
f@0
|
91 private static class InteractionLogFormatter extends Formatter{
|
f@0
|
92
|
f@0
|
93 private InteractionLogFormatter(){
|
f@0
|
94 super();
|
f@0
|
95 }
|
f@0
|
96
|
f@0
|
97 @Override
|
f@0
|
98 public String format(LogRecord record) {
|
f@0
|
99 StringBuilder builder = new StringBuilder();
|
f@0
|
100 if(record.getLevel() == Level.CONFIG){
|
f@0
|
101 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
|
f@0
|
102 builder.append("--- ")
|
f@0
|
103 .append(dateFormat.format(new Date(record.getMillis())))
|
f@0
|
104 .append(" - ")
|
f@0
|
105 .append(record.getMessage())
|
f@0
|
106 .append(" ---")
|
f@0
|
107 .append(NEW_LINE);
|
f@0
|
108 }else if(record.getLevel() == Level.FINE){
|
f@0
|
109 SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
|
f@0
|
110 builder.append(dateFormat.format(new Date(record.getMillis())))
|
f@0
|
111 .append(SEPARATOR)
|
f@0
|
112 .append(record.getMessage())
|
f@0
|
113 .append(NEW_LINE);
|
f@0
|
114 }
|
f@0
|
115
|
f@0
|
116 return builder.toString();
|
f@0
|
117 }
|
f@0
|
118 }
|
f@0
|
119
|
f@0
|
120 /**
|
f@0
|
121 * Release allocated resources. to be called hwen the interaction log is
|
f@0
|
122 * no longer needed.
|
f@0
|
123 */
|
f@0
|
124 public static void dispose(){
|
f@0
|
125 if(fileHandler != null)
|
f@0
|
126 fileHandler.close();
|
f@0
|
127 }
|
f@0
|
128
|
f@0
|
129 private static Logger logger = Logger.getLogger("interaction");
|
f@0
|
130 private static FileHandler fileHandler;
|
f@0
|
131 private static char SEPARATOR = ',';
|
f@0
|
132 private final static String NEW_LINE = System.getProperty("line.separator");
|
f@0
|
133 }
|