fiore@1
|
1 package uk.ac.qmul.eecs.ccmi.utilities;
|
fiore@1
|
2
|
fiore@1
|
3 import java.io.File;
|
fiore@1
|
4 import java.io.IOException;
|
fiore@1
|
5 import java.text.SimpleDateFormat;
|
fiore@1
|
6 import java.util.Date;
|
fiore@1
|
7 import java.util.logging.FileHandler;
|
fiore@1
|
8 import java.util.logging.Formatter;
|
fiore@1
|
9 import java.util.logging.LogRecord;
|
fiore@1
|
10 import java.util.logging.Logger;
|
fiore@1
|
11
|
fiore@1
|
12 import android.os.Environment;
|
fiore@1
|
13
|
fiore@1
|
14
|
fiore@1
|
15 public class ILogger {
|
fiore@1
|
16 private static final Logger LOGGER = Logger.getLogger(ILogger.class.getName());
|
fiore@1
|
17 private static final String LOG_PATH = "/Android/data/uk.ac.qmul.eecs.ccmi/files" ;
|
fiore@1
|
18 private static final String LOG_FILE = "/interaction%u.txt" ;
|
fiore@1
|
19 private static FileHandler fileHandler;
|
fiore@1
|
20 public final static int CLICK_BACK = -1;
|
fiore@1
|
21
|
fiore@1
|
22
|
fiore@1
|
23 private static void initHandler() throws IOException{
|
fiore@1
|
24 String state = Environment.getExternalStorageState();
|
fiore@1
|
25 if(!Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
|
fiore@1
|
26 throw new IOException("Could not find or write on SD Card");
|
fiore@1
|
27 }
|
fiore@1
|
28
|
fiore@1
|
29 File logDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+LOG_PATH);
|
fiore@1
|
30 if(!logDir.exists()){
|
fiore@1
|
31 if(!logDir.mkdirs())
|
fiore@1
|
32 throw new IOException("Could not create log file directory");
|
fiore@1
|
33 }
|
fiore@1
|
34
|
fiore@1
|
35 fileHandler = new FileHandler(logDir.getAbsolutePath()+LOG_FILE,true);
|
fiore@1
|
36 fileHandler.setFormatter(new InteractionLogFormatter());
|
fiore@1
|
37 LOGGER.addHandler(fileHandler);
|
fiore@1
|
38 }
|
fiore@1
|
39
|
fiore@1
|
40
|
fiore@1
|
41 public static void log(String message) {
|
fiore@1
|
42 if(fileHandler == null){
|
fiore@1
|
43 try {
|
fiore@1
|
44 initHandler();
|
fiore@1
|
45 } catch (IOException e) {
|
fiore@1
|
46 e.printStackTrace();
|
fiore@1
|
47 throw new RuntimeException(e);
|
fiore@1
|
48 }
|
fiore@1
|
49 }
|
fiore@1
|
50 LOGGER.info(message+"\n");
|
fiore@1
|
51 }
|
fiore@1
|
52
|
fiore@1
|
53 public static void logTap(CharSequence item){
|
fiore@1
|
54 log("user tap on: "+ item);
|
fiore@1
|
55 }
|
fiore@1
|
56
|
fiore@1
|
57 public static void logLongTap(CharSequence item){
|
fiore@1
|
58 log("user long tap on: "+item);
|
fiore@1
|
59 }
|
fiore@1
|
60
|
fiore@1
|
61 public static void logDialog(CharSequence dialog){
|
fiore@1
|
62 log("display dialog: "+dialog);
|
fiore@1
|
63 }
|
fiore@1
|
64
|
fiore@1
|
65 public static void logButton(CharSequence button){
|
fiore@1
|
66 log("user button press: "+button);
|
fiore@1
|
67 }
|
fiore@1
|
68
|
fiore@1
|
69 public static void logActivity(CharSequence activity){
|
fiore@1
|
70 log("display: "+activity);
|
fiore@1
|
71 }
|
fiore@1
|
72
|
fiore@1
|
73 public static void logError(String cause ){
|
fiore@1
|
74 log("error: "+cause);
|
fiore@1
|
75 }
|
fiore@1
|
76
|
fiore@1
|
77 public static void logHover(String text){
|
fiore@1
|
78 log("user hover: "+text);
|
fiore@1
|
79 }
|
fiore@1
|
80
|
fiore@1
|
81 public static void dispose(){
|
fiore@1
|
82 if(fileHandler != null){
|
fiore@1
|
83 fileHandler.close();
|
fiore@1
|
84 fileHandler = null;
|
fiore@1
|
85 }
|
fiore@1
|
86 }
|
fiore@1
|
87
|
fiore@1
|
88 }
|
fiore@1
|
89
|
fiore@1
|
90 class InteractionLogFormatter extends Formatter {
|
fiore@1
|
91 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS");
|
fiore@1
|
92
|
fiore@1
|
93 @Override
|
fiore@1
|
94 public String format(LogRecord record) {
|
fiore@1
|
95 return DATE_FORMAT.format(new Date(record.getMillis()))+','+record.getMessage();
|
fiore@1
|
96 }
|
fiore@1
|
97
|
fiore@1
|
98 } |