FileDocCategorySizeDatePackage
RecognizerLogger.javaAPI DocAndroid 1.5 API8413Wed May 06 22:42:48 BST 2009com.android.voicedialer

RecognizerLogger

public class RecognizerLogger extends Object
This class logs the inputs and results of a recognition session to the files listed below, which reside in /data/data/com.android.voicedialer/app_logdir. The files have the date encoded in the name so that they will sort in time order. The newest RecognizerLogger.MAX_FILES are kept, and the rest deleted to limit space used in the file system.
  • datename.wav - what the microphone heard.
  • datename.log - contact list, results, errors, etc.

Fields Summary
private static final String
TAG
private static final String
LOGDIR
private static final String
ENABLED
private static final int
MAX_FILES
private final String
mDatedPath
private final BufferedWriter
mWriter
Constructors Summary
public RecognizerLogger(android.content.Context context)
Constructor

param
dataDir directory to contain the log files.

        if (Config.LOGD) Log.d(TAG, "RecognizerLogger");
        
        // generate new root filename
        File dir = context.getDir(LOGDIR, 0);
        mDatedPath = dir.toString() + File.separator + "log_" +
                DateFormat.format("yyyy_MM_dd_kk_mm_ss",
                        System.currentTimeMillis());
        
        // delete oldest files
        deleteOldest(".wav");
        deleteOldest(".log");
        
        // generate new text output log file
        mWriter = new BufferedWriter(new FileWriter(mDatedPath + ".log"), 8192);
        mWriter.write(Build.FINGERPRINT);
        mWriter.newLine();
    
Methods Summary
public voidclose()
Close the text log file.

throws
IOException

        mWriter.close();
    
private voiddeleteOldest(java.lang.String suffix)
Delete oldest files with a given suffix, if more than MAX_FILES.

param
suffix delete oldest files with this suffix.

        FileFilter ff = new FileFilter() {
            public boolean accept(File f) {
                String name = f.getName();
                return name.startsWith("log_") && name.endsWith(suffix);
            }
        };
        File[] files = (new File(mDatedPath)).getParentFile().listFiles(ff);
        Arrays.sort(files);

        for (int i = 0; i < files.length - MAX_FILES; i++) {
            files[i].delete();            
        }
    
public static voiddisable(android.content.Context context)
Disable logging.

param
context needed to reference the logging directory.

        try {
            File dir = context.getDir(LOGDIR, 0);
            File enabled = new File(dir, ENABLED);
            enabled.delete();
        }
        catch (SecurityException e) {
            Log.e(TAG, "disableLogging " + e);
        }
    
public static voidenable(android.content.Context context)
Enable logging.

param
context needed to reference the logging directory.

        try {
            File dir = context.getDir(LOGDIR, 0);
            File enabled = new File(dir, ENABLED);
            enabled.createNewFile();
        }
        catch (IOException e) {
            Log.e(TAG, "enableLogging " + e);
        }
    
public static booleanisEnabled(android.content.Context context)
Determine if logging is enabled. If the

param
context needed to reference the logging directory.
return
true if logging is enabled, determined by the 'enabled' file.

    
                                    
         
        File dir = context.getDir(LOGDIR, 0);
        File enabled = new File(dir, ENABLED);
        return enabled.exists();
    
public voidlogContacts(java.util.List contacts)
Write the list of contacts into the text log file.

param
contacts

        logLine("Contacts *****************");
        for (VoiceContact vc : contacts) logLine(vc.toString());
        try {
            mWriter.flush();
        }
        catch (IOException e) {
            Log.e(TAG, "logContacts exception: " + e);
        }
    
public java.io.InputStreamlogInputStream(java.io.InputStream inputStream, int sampleRate)
InputStream wrapper which will log the contents to a WAV file.

param
inputStream
param
sampleRate
return

        final ByteArrayOutputStream baos = new ByteArrayOutputStream(sampleRate * 2 * 20);
        
        return new InputStream() {

            public int available() throws IOException {
                return inputStream.available();
            }

            public int read(byte[] b, int offset, int length) throws IOException {
                int rtn = inputStream.read(b, offset, length);
                if (rtn > 0) baos.write(b, offset, rtn);
                return rtn;
            }

            public int read(byte[] b) throws IOException {
                int rtn = inputStream.read(b);
                if (rtn > 0) baos.write(b, 0, rtn);
                return rtn;
            }

            public int read() throws IOException {
                int rtn = inputStream.read();
                if (rtn > 0) baos.write(rtn);
                return rtn;
            }

            public long skip(long n) throws IOException {
                throw new UnsupportedOperationException();
            }

            public void close() throws IOException {
                try {
                    OutputStream out = new FileOutputStream(mDatedPath + ".wav");
                    try {
                        byte[] pcm = baos.toByteArray();
                        WaveHeader hdr = new WaveHeader(WaveHeader.FORMAT_PCM,
                                (short)1, sampleRate, (short)16, pcm.length);
                        hdr.write(out);
                        out.write(pcm);
                    }
                    finally {
                        out.close();
                    }
                }
                finally {
                    inputStream.close();
                    baos.close();
                }
            }
        };
    
public voidlogIntents(java.util.ArrayList intents)
Write a list of Intents into the text log file.

param
intents

        logLine("Intents *********************");
        StringBuffer sb = new StringBuffer();
        for (Intent intent : intents) {
            logLine(intent.toString() + " " + RecognizerEngine.SENTENCE_EXTRA + "=" +
                    intent.getStringExtra(RecognizerEngine.SENTENCE_EXTRA));
        }
        try {
            mWriter.flush();
        }
        catch (IOException e) {
            Log.e(TAG, "logIntents exception: " + e);
        }
    
public voidlogLine(java.lang.String msg)
Write a line into the text log file.

        try {
            mWriter.write(msg);
            mWriter.newLine();
        }
        catch (IOException e) {
            Log.e(TAG, "logLine exception: " + e);
        }
    
public voidlogNbestHeader()
Write a header for the NBest lines into the text log file.

        logLine("Nbest *****************");