annotate java/src/uk/ac/qmul/eecs/ccmi/utils/InteractionLog.java @ 4:2c67ac862920

bug fix (correct haptic painting after renaming nodes and after nodes deletion with an edge with more than 2 nodes)
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Tue, 29 May 2012 15:32:19 +0100
parents 9e67171477bc
children d66dd5880081
rev   line source
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@0 36 public static void enable(String logFileDir) throws IOException{
fiore@0 37 logger.setLevel(Level.FINE);
fiore@0 38 logger.setUseParentHandlers(false);
fiore@0 39 if(fileHandler == null){
fiore@0 40 fileHandler = new FileHandler(logFileDir+System.getProperty("file.separator")+"interaction%u.log",true);
fiore@0 41 fileHandler.setFormatter(new CCmILogFormatter());
fiore@0 42 logger.addHandler(fileHandler);
fiore@0 43 }
fiore@0 44
fiore@0 45 /* also print the log on the console */
fiore@3 46 java.util.logging.ConsoleHandler ch = new java.util.logging.ConsoleHandler();
fiore@0 47 ch.setLevel(Level.ALL);
fiore@0 48 ch.setFormatter(new CCmILogFormatter());
fiore@0 49 logger.addHandler(ch);
fiore@0 50 }
fiore@0 51
fiore@0 52 public static void disable(){
fiore@0 53 logger.setLevel(Level.OFF);
fiore@0 54 }
fiore@0 55
fiore@0 56 public static CCmILogFormatter newFormatter(){
fiore@0 57 return new CCmILogFormatter();
fiore@0 58 }
fiore@0 59
fiore@0 60 public static void log(String source, String action, String args){
fiore@0 61 StringBuilder builder = new StringBuilder(source);
fiore@0 62 builder.append(SEPARATOR)
fiore@0 63 .append(action)
fiore@0 64 .append(SEPARATOR)
fiore@0 65 .append(args);
fiore@0 66
fiore@0 67 logger.fine(builder.toString());
fiore@0 68 }
fiore@0 69
fiore@0 70 public static void log(String msg){
fiore@0 71 logger.config(msg);
fiore@0 72 }
fiore@0 73
fiore@0 74 public static class CCmILogFormatter extends Formatter{
fiore@0 75
fiore@0 76 private CCmILogFormatter(){
fiore@0 77 super();
fiore@0 78 }
fiore@0 79
fiore@0 80 @Override
fiore@0 81 public String format(LogRecord record) {
fiore@0 82 StringBuilder builder = new StringBuilder();
fiore@0 83 if(record.getLevel() == Level.CONFIG){
fiore@0 84 SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
fiore@0 85 builder.append("--- ")
fiore@0 86 .append(dateFormat.format(new Date(record.getMillis())))
fiore@0 87 .append(" - ")
fiore@0 88 .append(record.getMessage())
fiore@0 89 .append(" ---")
fiore@0 90 .append(NEW_LINE);
fiore@0 91 }else if(record.getLevel() == Level.FINE){
fiore@0 92 SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
fiore@0 93 builder.append(dateFormat.format(new Date(record.getMillis())))
fiore@0 94 .append(SEPARATOR)
fiore@0 95 .append(record.getMessage())
fiore@0 96 .append(NEW_LINE);
fiore@0 97 }
fiore@0 98
fiore@0 99 return builder.toString();
fiore@0 100 }
fiore@0 101 }
fiore@0 102
fiore@0 103 public static void dispose(){
fiore@0 104 if(fileHandler != null)
fiore@0 105 fileHandler.close();
fiore@0 106 }
fiore@0 107
fiore@0 108 private static Logger logger = Logger.getLogger("interaction");
fiore@0 109 private static FileHandler fileHandler;
fiore@0 110 private static char SEPARATOR = ',';
fiore@0 111 private final static String NEW_LINE = System.getProperty("line.separator");
fiore@0 112 }