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