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"; /** * Warn. * * @param e the e */ public static void warn(Throwable e) { warn(toStackTraceString(e)); } /** * Warn. * * @param message the message */ public static void warn(String message) { warn(debugTag, message); } /** * Warn. * * @param object the object * @param message the message */ public static void warn(Object object, String message) { warn(object.getClass().getSimpleName(), message); } /** * Warn. * * @param object the object * @param e the e */ public static void warn(Object object, Throwable e) { warn(object.getClass().getSimpleName(), toStackTraceString(e)); } public static void warn(String tag, String message) { if (isDebug) { try { Log.w(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; } }