f@0: /* f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool f@0: f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) f@0: f@0: This program is free software: you can redistribute it and/or modify f@0: it under the terms of the GNU General Public License as published by f@0: the Free Software Foundation, either version 3 of the License, or f@0: (at your option) any later version. f@0: f@0: This program is distributed in the hope that it will be useful, f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@0: GNU General Public License for more details. f@0: f@0: You should have received a copy of the GNU General Public License f@0: along with this program. If not, see . f@0: */ f@0: f@0: package uk.ac.qmul.eecs.ccmi.utils; f@0: f@0: import java.io.IOException; f@0: import java.text.SimpleDateFormat; f@0: import java.util.Date; f@0: import java.util.logging.FileHandler; f@0: import java.util.logging.Formatter; f@0: import java.util.logging.Level; f@0: import java.util.logging.LogRecord; f@0: import java.util.logging.Logger; f@0: f@0: /** f@0: * A logger class using the {@code java.util.logging} package to log all the user's f@0: * relevant actions. f@0: */ f@0: public class InteractionLog { f@0: /** f@0: * Enable the logging f@0: * @param logFileDir the path of the directory where the log file will be saved f@0: * @throws IOException if a I/O problem occurs when writing log entry to the log file f@0: */ f@0: public static void enable(String logFileDir) throws IOException{ f@0: logger.setLevel(Level.FINE); f@0: logger.setUseParentHandlers(false); f@0: if(fileHandler == null){ f@0: fileHandler = new FileHandler(logFileDir+System.getProperty("file.separator")+"interaction%u.log",true); f@0: fileHandler.setFormatter(new InteractionLogFormatter()); f@0: logger.addHandler(fileHandler); f@0: f@0: /* also print the log on the console */ f@0: java.util.logging.ConsoleHandler ch = new java.util.logging.ConsoleHandler(); f@0: ch.setLevel(Level.ALL); f@0: ch.setFormatter(new InteractionLogFormatter()); f@0: logger.addHandler(ch); f@0: } f@0: } f@0: f@0: /** f@0: * Disable the logging f@0: */ f@0: public static void disable(){ f@0: logger.setLevel(Level.OFF); f@0: } f@0: f@0: /** f@0: * Logs a log entry in the file. Log entries are action that occurred during f@0: * the interaction by local or remote user. f@0: * f@0: * @param source the source of the interaction f@0: * @param action the occurred action f@0: * @param args further informations about the occurred action f@0: */ f@0: public static void log(String source, String action, String args){ f@0: StringBuilder builder = new StringBuilder(source); f@0: builder.append(SEPARATOR) f@0: .append(action) f@0: .append(SEPARATOR) f@0: .append(args); f@0: f@0: logger.fine(builder.toString()); f@0: } f@0: f@0: /** f@0: * Logs a general message in the log file. This log entries are not f@0: * linked to specific action of a local or remote user. f@0: * @param msg the message to log f@0: */ f@0: public static void log(String msg){ f@0: logger.config(msg); f@0: } f@0: f@0: private static class InteractionLogFormatter extends Formatter{ f@0: f@0: private InteractionLogFormatter(){ f@0: super(); f@0: } f@0: f@0: @Override f@0: public String format(LogRecord record) { f@0: StringBuilder builder = new StringBuilder(); f@0: if(record.getLevel() == Level.CONFIG){ f@0: SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss"); f@0: builder.append("--- ") f@0: .append(dateFormat.format(new Date(record.getMillis()))) f@0: .append(" - ") f@0: .append(record.getMessage()) f@0: .append(" ---") f@0: .append(NEW_LINE); f@0: }else if(record.getLevel() == Level.FINE){ f@0: SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); f@0: builder.append(dateFormat.format(new Date(record.getMillis()))) f@0: .append(SEPARATOR) f@0: .append(record.getMessage()) f@0: .append(NEW_LINE); f@0: } f@0: f@0: return builder.toString(); f@0: } f@0: } f@0: f@0: /** f@0: * Release allocated resources. to be called hwen the interaction log is f@0: * no longer needed. f@0: */ f@0: public static void dispose(){ f@0: if(fileHandler != null) f@0: fileHandler.close(); f@0: } f@0: f@0: private static Logger logger = Logger.getLogger("interaction"); f@0: private static FileHandler fileHandler; f@0: private static char SEPARATOR = ','; f@0: private final static String NEW_LINE = System.getProperty("line.separator"); f@0: }