Add logs to PhoneFactory

Allow entities in the phone process to easily add logs
to TelephonyDebugService.

First user, VVM.

Bug: 22607570

Change-Id: I65cfaf723db647a3747f72f404b690a5ab110811
diff --git a/src/java/com/android/internal/telephony/PhoneFactory.java b/src/java/com/android/internal/telephony/PhoneFactory.java
index 6c49c4a..b4c1c94 100644
--- a/src/java/com/android/internal/telephony/PhoneFactory.java
+++ b/src/java/com/android/internal/telephony/PhoneFactory.java
@@ -29,6 +29,7 @@
 import android.telephony.Rlog;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.util.LocalLog;
 
 import com.android.internal.telephony.cdma.CDMALTEPhone;
 import com.android.internal.telephony.cdma.CDMAPhone;
@@ -42,9 +43,11 @@
 import com.android.internal.telephony.sip.SipPhoneFactory;
 import com.android.internal.telephony.uicc.IccCardProxy;
 import com.android.internal.telephony.uicc.UiccController;
+import com.android.internal.util.IndentingPrintWriter;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.util.HashMap;
 
 /**
  * {@hide}
@@ -74,6 +77,8 @@
     static private PhoneNotifier sPhoneNotifier;
     static private Context sContext;
 
+    static private final HashMap<String, LocalLog>sLocalLogs = new HashMap<String, LocalLog>();
+
     //***** Class Methods
 
     public static void makeDefaultPhones(Context context) {
@@ -412,6 +417,43 @@
         return ImsPhoneFactory.makePhone(sContext, phoneNotifier, defaultPhone);
     }
 
+    /**
+     * Adds a local log category.
+     *
+     * Only used within the telephony process.  Use localLog to add log entries.
+     *
+     * TODO - is there a better way to do this?  Think about design when we have a minute.
+     *
+     * @param key the name of the category - will be the header in the service dump.
+     * @param size the number of lines to maintain in this category
+     */
+    public static void addLocalLog(String key, int size) {
+        synchronized(sLocalLogs) {
+            if (sLocalLogs.containsKey(key)) {
+                throw new IllegalArgumentException("key " + key + " already present");
+            }
+            sLocalLogs.put(key, new LocalLog(size));
+        }
+    }
+
+    /**
+     * Add a line to the named Local Log.
+     *
+     * This will appear in the TelephonyDebugService dump.
+     *
+     * @param key the name of the log category to put this in.  Must be created
+     *            via addLocalLog.
+     * @param log the string to add to the log.
+     */
+    public static void localLog(String key, String log) {
+        synchronized(sLocalLogs) {
+            if (sLocalLogs.containsKey(key) == false) {
+                throw new IllegalArgumentException("key " + key + " not found");
+            }
+            sLocalLogs.get(key).log(log);
+        }
+    }
+
     public static void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("PhoneFactory:");
         PhoneProxy [] phones = (PhoneProxy[])PhoneFactory.getPhones();
@@ -468,5 +510,17 @@
             e.printStackTrace();
         }
         pw.flush();
+
+        pw.println("++++++++++++++++++++++++++++++++");
+        synchronized (sLocalLogs) {
+            final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, "  ");
+            for (String key : sLocalLogs.keySet()) {
+                ipw.println(key);
+                ipw.increaseIndent();
+                sLocalLogs.get(key).dump(fd, ipw, args);
+                ipw.decreaseIndent();
+            }
+            ipw.flush();
+        }
     }
 }