近期在一个近10万行代码的android工程中修复BUG。为查找到BUG的位置需要将工程中的日志全部输出到文件中。

工程运行过程中有3个进程。

  1. com.citrix.Receiver
  2. com.citrix.Receiver:pnagent
  3. com.citrix.Receiver:wfica

在本文中给第1个进程命名进程A;第2个进程命名进程B;第3个进程命名进程C。

第2个进程对应AndroidManifest.xml文件

<!-- PNAgent multi-tab activity -->
<activity
android:name="com.citrix.client.pnagent.PNAgent"
android:configChanges
="keyboard|keyboardHidden|orientation"
android:process
=":pnagent"
android:label
="@string/app_name"
android:windowSoftInputMode
="stateHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="citrixreceiver" android:host="launchapp"/>
</intent-filter>
</activity>

第3个进程对应AndroidManifest.xml文件

<!-- Main receiver activity that displays the remote session -->
<activity
android:name="com.citrix.client.gui.ReceiverViewActivity"
android:windowSoftInputMode
="stateHidden|adjustResize"
android:configChanges
="keyboard|keyboardHidden|orientation"
android:process
=":wfica">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

在工程中Debug类定义static变量logFileName。在进程A中创建日志文件,给变量logFileName赋值。在进程B中获取logFileName的值,结果null。通过观察,进程A和进程B无法通过定义static变量来传递和分享数据。

进程A,进程B,进程C的关系。进程A通过定义Intent,调用startActivity(Intent)方法来启动进程B。进程B启动的同时并没有结束进程A。进程B启动进程C过程同进程A启动进程B。进程C启动后,在DDMS中可以观察到程序中同时有3个进程存在。

通过Intent.putExtra传递数据

进程A中添加语句

intent.putExtra(Debug.LOG_FILE_NAME, Debug.logFileName);

进程B中添加语句

Debug.logFileName = this.getIntent().getStringExtra(Debug.LOG_FILE_NAME);

通过扩展Application传递数据

定义类

public class CReceiver extends Application

定义变量

public static String[] messages = new String[1024];
public static int messageCount = 0;

当messageCount小于1024时,将日志存到数组messages中。当messageCount达到1024时,将存在数组中的所有日志写到文件中。

message = message.endsWith("\n") ? message : message + "\n";
messages[messageCount] = message;
messageCount++;
if(messageCount == 1024)
{
messageCount = 0;
for(int i = 0; i < 1024; i++)
{
fos.write(messages[i].getBytes());
messages[i] = null;
}
}

附:com.citrix.client.global.CReceiver

  1 package com.citrix.client.global;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8
9 import android.app.Application;
10 import android.os.Environment;
11 import android.util.Log;
12
13 public class CReceiver extends Application
14 {
15 public static String logFileName;
16 public static FileOutputStream fos;
17
18 public static String[] messages = new String[1024];
19 public static int messageCount = 0;
20
21 private static String getLogFileName()
22 {
23 if(logFileName == null)
24 {
25 String root = Environment.getExternalStorageDirectory().getPath();
26 String format = "yyyyMMdd-HHmmss";
27 String fileName = "CReceiverLog-" + new SimpleDateFormat(format).format(new Date()) + ".txt";
28 logFileName = root + "//emass//" + fileName;
29 }
30 return logFileName;
31 }
32
33 private static String getLogPathName()
34 {
35 String root = Environment.getExternalStorageDirectory().getPath();
36 return root + "//emass";
37 }
38
39 public synchronized static void writeLogFile(String message)
40 {
41 try
42 {
43 if (fos == null)
44 {
45 System.gc();
46
47 // 创建日志目录
48 File fileLogPath = new File(getLogPathName());
49 if (!fileLogPath.exists())
50 {
51 fileLogPath.mkdirs();
52 }
53
54 // 创建日志文件
55 File fileLog = new File(getLogFileName());
56 if (!fileLog.exists())
57 {
58 fileLog.createNewFile();
59 }
60
61 // 构建FileOutputStream
62 fos = new FileOutputStream(fileLog, true);
63 }
64 message = message.endsWith("\n") ? message : message + "\n";
65 messages[messageCount] = message;
66 messageCount++;
67 if(messageCount == 1024)
68 {
69 messageCount = 0;
70 for(int i = 0; i < 1024; i++)
71 {
72 fos.write(messages[i].getBytes());
73 messages[i] = null;
74 }
75 }
76 } catch (IOException e)
77 {
78 Log.i("CReceiver", getStackTrace(e));
79 }
80 }
81
82 public static void closeLogStream()
83 {
84 try
85 {
86 if(fos != null)
87 {
88 fos.close();
89 }
90 } catch (IOException e)
91 {
92 Log.i("CReceiver", getStackTrace(e));
93 }
94 }
95
96 public static String getStackTrace(Throwable t)
97 {
98 StackTraceElement[] items = t.getStackTrace();
99 String stackTrace = t.getMessage();
100 for (StackTraceElement item : items)
101 {
102 stackTrace += "\n " + item.getClassName();
103 stackTrace += "." + item.getMethodName();
104 stackTrace += ":" + item.getLineNumber();
105 }
106 return stackTrace;
107 }
108 }


 

posted on 2012-03-13 12:40  习以常  阅读(1362)  评论(0编辑  收藏  举报