package com.beerme.android.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

/**
 * Provides consistent error logging via {@link Log} and error message display
 * via {@link Toast}.
 * 
 */
public class ErrLog {

	private ErrLog() {
	}

	/**
	 * 
	 * @param context
	 *            Application context
	 * @param methodName
	 *            Name of the method that suffered the error (may be null)
	 * @param exception
	 *            Exception (may be null)
	 * @param userMessage
	 *            Integer pointer to a text resource; error message for the user
	 * 
	 */
	public static void log(final Context context, final String methodName,
			final Exception exception, final int userMessage) {
		final String umsg = (userMessage == 0) ? "" : context.getText(userMessage).toString();
		log(context, methodName, exception, umsg);
	}

	/**
	 * 
	 * @param context
	 *            Application context
	 * @param methodName
	 *            Name of the method that suffered the error (may be null)
	 * @param exception
	 *            Exception (may be null)
	 * @param userMessage
	 *            Error message for the user (may be null)
	 * 
	 */
	public static void log(final Context context, final String methodName,
			final Exception exception, final String userMessage) {
		final Activity activity = (Activity) context;

		Log.e(Utils.APPTAG, activity.getClass().getName() + "." + methodName  + ": " + userMessage);

		if (exception != null) {
			String exMsg = exception.getLocalizedMessage();
			Log.e(Utils.APPTAG, (exMsg == null) ? exception.toString() : exMsg);
		}

		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (userMessage != null) {
					Toast.makeText(activity, userMessage, Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}
@@ -752,32 +752,58 @@
  userMessage) %7B%0A
+%09%09if (context != null) %7B%0A%09
 %09%09final String u
@@ -877,16 +877,17 @@
 ng();%0A%09%09
+%09
 log(cont
@@ -921,16 +921,121 @@
  umsg);%0A
+%09%09%7D else %7B%0A%09%09%09Log.e(%22beerme%22,%22ErrLog.log: NULL CONTEXT%22);%0A%09%09%09log(null, methodName, exception, null);%0A%09%09%7D%0A
 %09%7D%0A%0A%09/**
@@ -1467,16 +1467,42 @@
 sage) %7B%0A
+%09%09if (context != null) %7B%0A%09
 %09%09final 
@@ -1542,16 +1542,17 @@
 ntext;%0A%0A
+%09
 %09%09Log.e(
@@ -1614,17 +1614,16 @@
 hodName 
- 
 + %22: %22 +
@@ -1640,16 +1640,17 @@
 ge);%0A%0A%09%09
+%09
 if (exce
@@ -1666,16 +1666,17 @@
 null) %7B%0A
+%09
 %09%09%09Strin
@@ -1718,16 +1718,17 @@
 sage();%0A
+%09
 %09%09%09Log.e
@@ -1793,19 +1793,21 @@
 Msg);%0A%09%09
+%09
 %7D%0A%0A
+%09
 %09%09activi
@@ -1843,16 +1843,17 @@
 () %7B%0A%09%09%09
+%09
 @Overrid
@@ -1854,16 +1854,17 @@
 verride%0A
+%09
 %09%09%09publi
@@ -1882,16 +1882,17 @@
 ) %7B%0A%09%09%09%09
+%09
 if (user
@@ -1906,24 +1906,25 @@
  != null) %7B%0A
+%09
 %09%09%09%09%09Toast.m
@@ -1989,21 +1989,28 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%7D%0A%09%09
+%09
 %7D);%0A
+%09%09%7D%0A
 %09%7D%0A%7D

Fix NPE in ErrLog