Bug 778784: Add logging to sutAgent to track its lifetime; r=jmaher

This commit is contained in:
Geoff Brown 2012-07-30 14:45:07 -06:00
parent 2c48de6a45
commit b120c03be3
2 changed files with 55 additions and 28 deletions

View File

@ -8,13 +8,11 @@ import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.mozilla.SUTAgentAndroid.SUTAgentAndroid;
import android.util.Log;
@ -100,6 +98,8 @@ public class CmdWorkerThread extends Thread
String inputLine, outputLine;
DoCommand dc = new DoCommand(theParent.svc);
SUTAgentAndroid.logToFile(dc.GetTestRoot(), dc.GetSystemTime(), "CmdWorkerThread starts: "+getId());
int nAvail = cmdIn.available();
cmdIn.skip(nAvail);
@ -134,30 +134,9 @@ public class CmdWorkerThread extends Thread
if ((inputLine += readLine(in)) != null)
{
String datestamp = dc.GetSystemTime();
String message = String.format("%s : %s : %s",
datestamp, socket.getInetAddress().getHostAddress(), inputLine);
Log.i("SUTAgentAndroid", message);
String fileDateStr = "00";
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
Date dateStr = sdf.parse(datestamp);
SimpleDateFormat sdf_file = new SimpleDateFormat("yyyy-MM-dd");
fileDateStr = sdf_file.format(dateStr);
} catch (ParseException pe) {}
String logFile = dc.GetTestRoot() + "/" + fileDateStr + "-sutcommands.txt";
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(logFile, true));
if (message != null) {
pw.println(message);
}
} catch (IOException ioe) {
Log.e("SUTAgentAndroid", "exception with file writer on: " + logFile);
} finally {
pw.close();
}
String message = String.format("%s : %s",
socket.getInetAddress().getHostAddress(), inputLine);
SUTAgentAndroid.logToFile(dc.GetTestRoot(), dc.GetSystemTime(), message);
outputLine = dc.processCommand(inputLine, out, in, cmdOut);
if (outputLine.length() > 0)
@ -187,6 +166,7 @@ public class CmdWorkerThread extends Thread
in.close();
in = null;
socket.close();
SUTAgentAndroid.logToFile(dc.GetTestRoot(), dc.GetSystemTime(), "CmdWorkerThread ends: "+getId());
}
catch (IOException e)
{

View File

@ -5,6 +5,8 @@
package com.mozilla.SUTAgentAndroid;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import org.apache.http.conn.util.InetAddressUtils;
@ -150,6 +152,8 @@ public class SUTAgentAndroid extends Activity
DoCommand dc = new DoCommand(getApplication());
logToFile(dc.GetTestRoot(), dc.GetSystemTime(), "onCreate");
// Get configuration settings from "ini" file
File dir = getFilesDir();
File iniFile = new File(dir, "SUTAgent.ini");
@ -351,8 +355,10 @@ public class SUTAgentAndroid extends Activity
public void onDestroy()
{
super.onDestroy();
DoCommand dc = new DoCommand(getApplication());
if (isFinishing())
{
logToFile(dc.GetTestRoot(), dc.GetSystemTime(), "onDestroy - finishing");
Intent listenerSvc = new Intent(this, ASMozStub.class);
listenerSvc.setAction("com.mozilla.SUTAgentAndroid.service.LISTENER_SERVICE");
stopService(listenerSvc);
@ -365,6 +371,10 @@ public class SUTAgentAndroid extends Activity
System.exit(0);
}
else
{
logToFile(dc.GetTestRoot(), dc.GetSystemTime(), "onDestroy - not finishing");
}
}
private void monitorBatteryState()
@ -714,4 +724,41 @@ public class SUTAgentAndroid extends Activity
}
return null;
}
public static void logToFile(String testRoot, String datestamp, String message)
{
if (testRoot == null ||
datestamp == null ||
message == null)
{
Log.e("SUTAgentAndroid", "bad arguments in logToFile()!");
return;
}
Log.i("SUTAgentAndroid", message);
String fileDateStr = "00";
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
Date dateStr = sdf.parse(datestamp);
SimpleDateFormat sdf_file = new SimpleDateFormat("yyyy-MM-dd");
fileDateStr = sdf_file.format(dateStr);
}
catch (ParseException pe) {}
String logFile = testRoot + "/" + fileDateStr + "-sutcommands.txt";
PrintWriter pw = null;
try
{
pw = new PrintWriter(new FileWriter(logFile, true));
pw.println(datestamp + " : " + message);
}
catch (IOException ioe)
{
Log.e("SUTAgentAndroid", "exception with file writer on: " + logFile);
}
finally
{
pw.close();
}
}
}