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