diff src/uk/ac/qmul/eecs/ccmi/utilities/ILogger.java @ 1:66b3a838feca logging tip

Added logging of user interaction
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Tue, 12 Feb 2013 15:31:48 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/uk/ac/qmul/eecs/ccmi/utilities/ILogger.java	Tue Feb 12 15:31:48 2013 +0000
@@ -0,0 +1,98 @@
+package uk.ac.qmul.eecs.ccmi.utilities;
+
+import java.io.File;
+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.LogRecord;
+import java.util.logging.Logger;
+
+import android.os.Environment;
+
+
+public class ILogger {
+	private static final Logger LOGGER = Logger.getLogger(ILogger.class.getName());
+	private static final String LOG_PATH = "/Android/data/uk.ac.qmul.eecs.ccmi/files" ;
+	private static final String LOG_FILE = "/interaction%u.txt" ;
+	private static FileHandler fileHandler;
+	public final static int CLICK_BACK = -1;
+	
+	
+	private static void initHandler() throws IOException{
+		String state = Environment.getExternalStorageState();
+		if(!Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
+			throw new IOException("Could not find or write on SD Card");
+		}
+		
+		File logDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+LOG_PATH);
+		if(!logDir.exists()){
+			if(!logDir.mkdirs())
+				throw new IOException("Could not create log file directory");
+		}
+		
+		fileHandler = new FileHandler(logDir.getAbsolutePath()+LOG_FILE,true);
+		fileHandler.setFormatter(new InteractionLogFormatter());
+		LOGGER.addHandler(fileHandler);		
+	}
+
+	
+	public static void log(String message) {
+		if(fileHandler == null){
+			try {
+				initHandler();
+			} catch (IOException e) {
+				e.printStackTrace();
+				throw new RuntimeException(e);	
+			}
+		}
+		LOGGER.info(message+"\n");
+	}
+	
+	public static void logTap(CharSequence item){
+		log("user tap on: "+ item);
+	}
+	
+	public static void logLongTap(CharSequence item){
+		log("user long tap on: "+item);
+	}
+	
+	public static void logDialog(CharSequence dialog){
+		log("display dialog: "+dialog);
+	}
+	
+	public static void logButton(CharSequence button){
+		log("user button press: "+button);
+	}
+	
+	public static void logActivity(CharSequence activity){
+		log("display: "+activity);
+	}
+	
+	public static void logError(String cause ){
+		log("error: "+cause);
+	}
+	
+	public static void logHover(String text){
+		log("user hover: "+text);
+	}
+	
+	public static  void dispose(){
+		if(fileHandler != null){
+			fileHandler.close();
+			fileHandler = null;
+		}
+	}
+
+}
+
+class InteractionLogFormatter extends Formatter {
+	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS");
+	
+	@Override
+	public String format(LogRecord record) {
+		return DATE_FORMAT.format(new Date(record.getMillis()))+','+record.getMessage();
+	}
+	
+}
\ No newline at end of file