view java/src/uk/ac/qmul/eecs/ccmi/utils/InteractionLog.java @ 5:d66dd5880081

Added support for Falcon Haptic device and Tablet/Mouse as haptic device
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Tue, 10 Jul 2012 22:39:37 +0100
parents 9e67171477bc
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/  

package uk.ac.qmul.eecs.ccmi.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * A logger class using the {@code java.util.logging} package to log all the user's 
 * relevant actions. 
 */
public class InteractionLog {
	/**
	 * Enable the logging 
	 * @param logFileDir the path of the directory where the log file will be saved  
	 * @throws IOException if a I/O problem occurs when writing log entry to the log file 
	 */
	public static void enable(String logFileDir) throws IOException{
		logger.setLevel(Level.FINE);
		logger.setUseParentHandlers(false);
		if(fileHandler == null){
			fileHandler = new FileHandler(logFileDir+System.getProperty("file.separator")+"interaction%u.log",true);
			fileHandler.setFormatter(new InteractionLogFormatter());
			logger.addHandler(fileHandler);

			/* also print the log on the console */
			java.util.logging.ConsoleHandler ch = new java.util.logging.ConsoleHandler();
			ch.setLevel(Level.ALL);
			ch.setFormatter(new InteractionLogFormatter());
			logger.addHandler(ch);
		}
	}
	
	/**
	 * Disable the logging 
	 */
	public static void disable(){
		logger.setLevel(Level.OFF);
	}
	
	/**
	 * Logs a log entry in the file. Log entries are action that occurred during 
	 * the interaction by local or remote user.   
	 * 
	 * @param source the source of the interaction 
	 * @param action the occurred action 
	 * @param args further informations about the occurred action 
	 */
	public static void log(String source, String action, String args){
		StringBuilder builder = new StringBuilder(source);
		builder.append(SEPARATOR)
			.append(action)
			.append(SEPARATOR)
			.append(args);

		logger.fine(builder.toString());
	}

	/**
	 * Logs a general message in the log file. This log entries are not 
	 * linked to specific action of a local or remote user. 
	 * @param msg the message to log 
	 */
	public static void log(String msg){
		logger.config(msg);
	}
	
	private static class InteractionLogFormatter extends Formatter{

		private InteractionLogFormatter(){
			super();
		}
		
		@Override
		public String format(LogRecord record) {
			StringBuilder builder = new StringBuilder();
			if(record.getLevel() == Level.CONFIG){
				SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
				builder.append("--- ")
					.append(dateFormat.format(new Date(record.getMillis())))
					.append(" - ")
					.append(record.getMessage())
					.append(" ---")
					.append(NEW_LINE);
			}else if(record.getLevel() == Level.FINE){
				SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
				builder.append(dateFormat.format(new Date(record.getMillis())))
					.append(SEPARATOR)
					.append(record.getMessage())
					.append(NEW_LINE);
			}
			
			return builder.toString();
		}
	}

	/**
	 * Release allocated resources. to be called hwen the interaction log is 
	 * no longer needed.  
	 */
	public static void dispose(){
		if(fileHandler != null)
			fileHandler.close();
	}
	
	private static Logger logger = Logger.getLogger("interaction");
	private static FileHandler fileHandler;
	private static char SEPARATOR = ',';
	private final static String NEW_LINE = System.getProperty("line.separator");
}