Java tutorial
//package com.java2s; import android.util.Log; import java.io.PrintWriter; import java.io.StringWriter; public class Main { private static final int MAX_STACK_TRACE_SIZE = 131071; private static boolean isDebug = true; private static String debugTag = "View"; /** * Error. * * @param e the e */ public static void error(Throwable e) { error(toStackTraceString(e)); } /** * Error. * * @param message the message */ public static void error(String message) { error(debugTag, message); } /** * Error. * * @param object the object * @param message the message */ public static void error(Object object, String message) { error(object.getClass().getSimpleName(), message); } /** * Error. * * @param object the object * @param e the e */ public static void error(Object object, Throwable e) { error(object.getClass().getSimpleName(), toStackTraceString(e)); } public static void error(String tag, String message) { if (isDebug) { try { Log.e(debugTag + tag, message); } catch (Exception e) { System.out.println(debugTag + ">>>" + message); } } } /** * To stack trace string string. * * @param throwable the throwable * @return the string */ public static String toStackTraceString(Throwable throwable) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); throwable.printStackTrace(pw); String stackTraceString = sw.toString(); //Reduce data to 128KB so we don't get a TransactionTooLargeException when sending the intent. //The limit is 1MB on Android but some devices seem to have it lower. //See: http://developer.android.com/reference/android/os/TransactionTooLargeException.html //And: http://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception#comment46697371_12809171 if (stackTraceString.length() > MAX_STACK_TRACE_SIZE) { String disclaimer = " [stack trace too large]"; stackTraceString = stackTraceString.substring(0, MAX_STACK_TRACE_SIZE - disclaimer.length()) + disclaimer; } return stackTraceString; } }