Mercurial > hg > ccmieditor
diff java/src/uk/ac/qmul/eecs/ccmi/utils/InteractionLog.java @ 0:9418ab7b7f3f
Initial import
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Fri, 16 Dec 2011 17:35:51 +0000 |
parents | |
children | 9e67171477bc |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/src/uk/ac/qmul/eecs/ccmi/utils/InteractionLog.java Fri Dec 16 17:35:51 2011 +0000 @@ -0,0 +1,112 @@ +/* + 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 { + 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 CCmILogFormatter()); + 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 CCmILogFormatter()); + logger.addHandler(ch); + } + + public static void disable(){ + logger.setLevel(Level.OFF); + } + + public static CCmILogFormatter newFormatter(){ + return new CCmILogFormatter(); + } + + 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()); + } + + public static void log(String msg){ + logger.config(msg); + } + + public static class CCmILogFormatter extends Formatter{ + + private CCmILogFormatter(){ + 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(); + } + } + + 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"); +}