androidプログラムログ記録
7550 ワード
<uses-permission android:name="android.permission.READ_LOGS" />
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Demo8Activity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
String filenameTemp = Environment.getExternalStorageDirectory().toString()+ "/app.txt";//
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) this.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//
try
{
int i=10/0;
}catch(Exception e)
{
Log.e("Android", e.getMessage());
}
Process mLogcatProc = null;
BufferedReader reader = null;
try
{
mLogcatProc = Runtime.getRuntime().exec(new String[]
{"logcat", "-d", "AndroidRuntime:E Android:V *:S" });//Android Tag
reader = new BufferedReader(new InputStreamReader
(mLogcatProc.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
log.append(line);
log.append(separator);
}
CreateText();
print(log.toString());
// do whatever you want with the log. I'd recommend using Intents to create an email
}
catch (IOException e)
{
}
finally
{
if (reader != null)
try
{
reader.close();
}
catch (IOException e)
{
}
}
}
});
}
//
public void CreateText() throws IOException {
File dir = new File(filenameTemp);
if (!dir.exists()) {
try {
//
System.out.println(dir.createNewFile()+"---------------------");;
} catch (Exception e) {
}
}
}
//
public void print(String str) {
FileWriter fw = null;
BufferedWriter bw = null;
String datetime = "";
try {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " "
+ "hh:mm:ss");
datetime = tempDate.format(new java.util.Date()).toString();
fw = new FileWriter(filenameTemp, true);//
// FileWriter ,
bw = new BufferedWriter(fw); //
String myreadline = datetime + "[]" + str;
bw.write(myreadline + "
"); //
bw.newLine();
bw.flush(); //
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
bw.close();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
}
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
/**
*
*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//
final AlertDialog isExit = new AlertDialog.Builder(this).create();
//
isExit.setTitle(" ");
//
isExit.setMessage(" ");
//
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case AlertDialog.BUTTON1:// " "
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
break;
case AlertDialog.BUTTON2:// " "
isExit.cancel();
break;
default:
break;
}
}
};
//
isExit.setButton(" ", listener);
isExit.setButton2(" ", listener);
//
isExit.show();
return false;
}
return false;
}
}
ログをファイルに出力する
:2012-02-28 05:06:00[]----beginning of/dev/log/system----beginning of/dev/log/maineE/Android Runtime(22045):divide by zero logcatコマンドの使い方について
http://developer.android.com/guide/developing/tools/adb.html#usinglogcat
Here's an example of a filter expression that suppresses all log messages except those with the tag "ActivityManager", at priority "Info"or above, and all log messages with tag "MyApp", with priority "Debug"or above:
adb logcat ActivityManager:I MyApp:D *:S