Add LogUtils to try to unify logging messages
authorDa Risk <da_risk@beem-project.com>
Tue, 09 Oct 2012 01:08:40 +0200
changeset 1001 ec0c60b581ed
parent 1000 3651228bf160
child 1002 85cce4bb7f5e
Add LogUtils to try to unify logging messages
doc/Logging.txt
src/com/beem/project/beem/ui/AddContact.java
src/com/beem/project/beem/ui/ContactList.java
src/com/google/android/apps/iosched/util/LogUtils.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/Logging.txt	Tue Oct 09 01:08:40 2012 +0200
@@ -0,0 +1,58 @@
+BEEM : Boost Your Eyes, Ears, and Mouth
+---------------------------------------
+
+This little document attempt to explain how the Log should be made in Beem.
+
+Logging Usage
+-------------
+
+Before
+  Many use of android.utils.Log.* with different LOGTAG. Often it
+  uses the class name as a LOGTAG. This cause confusion in logcat because it
+  is hard to say for which application the tag is from.
+
+Hopefully now
+  The class com.google.android.apps.iosched.util.LogUtils allow to unify the
+  Log.
+  The method makeLogTag() add a prefix to the tag made by the
+  application (current prefix is "Beem-").
+  This class also implement the best practices for log on the Android
+  platform. Each Log call is preceed by an isLoggable() call to check if the
+  application should do Logging at this LogLevel.
+  Also on release build, verbose logs will not be printed.
+  The drawback of this is that all logs are not visible by default and require
+  little work to make them appears.
+
+Usage of Log levels
+-------------------
+
+VERBOSE: for log messages in low level layers, that should be necessary only
+for the developers.
+
+DEBUG: for log messages which allow to debug the application by retrieving
+info not easilly retrieved and not necessary to the user.
+
+INFO: for log informational message potentially usefull to the the user.
+
+WARNING: to log bad events but handled by the application. The LOGW(TAG,
+"message", exception) is particularly usefull
+
+ERROR: to log fatal error. Typically crash or other unexpected errors.
+
+Showing all logs
+----------------
+
+As specified in the javadoc for android.util.Log :
+
+The default level of any tag is set to INFO. This means that any level above and including
+INFO will be logged. Before you make any calls to a logging method you should check to see
+if your tag should be logged. You can change the default level by setting a system property:
+'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>'
+Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
+turn off all logging for your tag. You can also create a local.prop file that with the
+following in it:
+'log.tag.<YOUR_LOG_TAG>=<LEVEL>'
+and place that in /data/local.prop.
+
+Don't forget that your LOG_TAG is prefixed by "Beem-"
+
--- a/src/com/beem/project/beem/ui/AddContact.java	Mon Oct 08 23:36:20 2012 +0200
+++ b/src/com/beem/project/beem/ui/AddContact.java	Tue Oct 09 01:08:40 2012 +0200
@@ -45,7 +45,6 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.regex.Pattern;
 
 import android.app.Activity;
 import android.content.ComponentName;
--- a/src/com/beem/project/beem/ui/ContactList.java	Mon Oct 08 23:36:20 2012 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Tue Oct 09 01:08:40 2012 +0200
@@ -65,7 +65,6 @@
 import android.support.v4.app.FragmentPagerAdapter;
 import android.support.v4.view.PagerTabStrip;
 import android.support.v4.view.ViewPager;
-import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -87,6 +86,8 @@
 
 import org.jivesoftware.smack.util.StringUtils;
 
+import static com.google.android.apps.iosched.util.LogUtils.*;
+
 /**
  * The contact list activity displays the roster of the user.
  */
@@ -97,7 +98,7 @@
 	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
     }
 
-    private static final String TAG = "ContactList";
+    private static final String TAG = makeLogTag(ContactList.class);
     private static final float PAGER_TAB_SECONDARY_ALPHA = 0.5f;
     private final List<String> mListGroup = new ArrayList<String>();
 
@@ -154,7 +155,7 @@
 		List<Contact> openedChats;
 		try {
 		    openedChats = mChatManager.getOpenedChatList();
-		    Log.d(TAG, "opened chats = " + openedChats);
+		    LOGD(TAG, "opened chats = " + openedChats);
 		    Dialog chatList = new ChatList(ContactList.this, openedChats).create();
 		    chatList.show();
 		} catch (RemoteException e) {
@@ -214,7 +215,7 @@
 		mRoster = null;
 	    }
 	} catch (RemoteException e) {
-	    Log.d("ContactList", "Remote exception", e);
+	    LOGD("ContactList", "Remote exception", e);
 	}
 	if (mBinded) {
 	    unbindService(mServConn);
@@ -227,7 +228,7 @@
     protected void onDestroy() {
 	super.onDestroy();
 	this.unregisterReceiver(mReceiver);
-	Log.e(TAG, "onDestroy activity");
+	LOGV(TAG, "onDestroy activity");
     }
 
     /**
@@ -294,7 +295,7 @@
 		delete.show();
 		break;
 	    default:
-		Log.w(TAG, "Context menu action not supported" + itemId);
+		LOGW(TAG, "Context menu action not supported" + itemId);
 		break;
 	}
 
@@ -383,7 +384,7 @@
 	 */
 	@Override
 	public void onEntriesDeleted(final List<String> addresses) throws RemoteException {
-	    Log.d(TAG, "onEntries deleted " + addresses);
+	    LOGD(TAG, "onEntries deleted " + addresses);
 	    for (String cToDelete : addresses) {
 		final Contact contact = new Contact(cToDelete);
 		for (final ContactListAdapter adapter : contactListAdapters.values()) {
@@ -411,7 +412,7 @@
 	 */
 	@Override
 	public void onEntriesUpdated(final List<String> addresses) throws RemoteException {
-	    Log.d(TAG, "onEntries updated " + addresses);
+	    LOGD(TAG, "onEntries updated " + addresses);
 	    for (String cToDelete : addresses) {
 		Contact contact = new Contact(cToDelete);
 		for (ContactListAdapter adapter : contactListAdapters.values()) {
@@ -495,7 +496,7 @@
 		    assignContactToGroups(mRoster.getContactList(), tmpGroupList);
 
 		    mRoster.addRosterListener(mBeemRosterListener);
-		    Log.d(TAG, "add roster listener");
+		    LOGD(TAG, "add roster listener");
 		    mChatManager = mXmppFacade.getChatManager();
 		}
 	    } catch (RemoteException e) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/google/android/apps/iosched/util/LogUtils.java	Tue Oct 09 01:08:40 2012 +0200
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.android.apps.iosched.util;
+
+import com.beem.project.beem.BuildConfig;
+
+import android.util.Log;
+
+/**
+ * Helper methods that make logging more consistent throughout the app.
+ */
+public class LogUtils {
+    private static final String LOG_PREFIX = "Beem";
+    private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
+    private static final int MAX_LOG_TAG_LENGTH = 23;
+
+    public static String makeLogTag(String str) {
+        if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
+            return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
+        }
+
+        return LOG_PREFIX + str;
+    }
+
+    /**
+     * WARNING: Don't use this when obfuscating class names with Proguard!
+     */
+    public static String makeLogTag(Class cls) {
+        return makeLogTag(cls.getSimpleName());
+    }
+
+    public static void LOGD(final String tag, String message) {
+        if (Log.isLoggable(tag, Log.DEBUG)) {
+            Log.d(tag, message);
+        }
+    }
+
+    public static void LOGD(final String tag, String message, Throwable cause) {
+        if (Log.isLoggable(tag, Log.DEBUG)) {
+            Log.d(tag, message, cause);
+        }
+    }
+
+    public static void LOGV(final String tag, String message) {
+        //noinspection PointlessBooleanExpression,ConstantConditions
+        if (BuildConfig.DEBUG && Log.isLoggable(tag, Log.VERBOSE)) {
+            Log.v(tag, message);
+        }
+    }
+
+    public static void LOGV(final String tag, String message, Throwable cause) {
+        //noinspection PointlessBooleanExpression,ConstantConditions
+        if (BuildConfig.DEBUG && Log.isLoggable(tag, Log.VERBOSE)) {
+            Log.v(tag, message, cause);
+        }
+    }
+
+    public static void LOGI(final String tag, String message) {
+        Log.i(tag, message);
+    }
+
+    public static void LOGI(final String tag, String message, Throwable cause) {
+        Log.i(tag, message, cause);
+    }
+
+    public static void LOGW(final String tag, String message) {
+        Log.w(tag, message);
+    }
+
+    public static void LOGW(final String tag, String message, Throwable cause) {
+        Log.w(tag, message, cause);
+    }
+
+    public static void LOGE(final String tag, String message) {
+        Log.e(tag, message);
+    }
+
+    public static void LOGE(final String tag, String message, Throwable cause) {
+        Log.e(tag, message, cause);
+    }
+
+    private LogUtils() {
+    }
+}