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