LogView.java (4536B)
1 package de.nordgedanken.rpicms.common.logger; 2 3 /** 4 * Created by frank on 14.10.14. 5 */ 6 import android.app.Activity; 7 import android.content.Context; 8 import android.util.*; 9 import android.widget.TextView; 10 11 /** Simple TextView which is used to output log data received through the LogNode interface. 12 */ 13 public class LogView extends TextView implements LogNode { 14 15 public LogView(Context context) { 16 super(context); 17 } 18 19 public LogView(Context context, AttributeSet attrs) { 20 super(context, attrs); 21 } 22 23 public LogView(Context context, AttributeSet attrs, int defStyle) { 24 super(context, attrs, defStyle); 25 } 26 27 /** 28 * Formats the log data and prints it out to the LogView. 29 * @param priority Log level of the data being logged. Verbose, Error, etc. 30 * @param tag Tag for for the log data. Can be used to organize log statements. 31 * @param msg The actual message to be logged. The actual message to be logged. 32 * @param tr If an exception was thrown, this can be sent along for the logging facilities 33 * to extract and print useful information. 34 */ 35 @Override 36 public void println(int priority, String tag, String msg, Throwable tr) { 37 38 39 String priorityStr = null; 40 41 // For the purposes of this View, we want to print the priority as readable text. 42 switch(priority) { 43 case android.util.Log.VERBOSE: 44 priorityStr = "VERBOSE"; 45 break; 46 case android.util.Log.DEBUG: 47 priorityStr = "DEBUG"; 48 break; 49 case android.util.Log.INFO: 50 priorityStr = "INFO"; 51 break; 52 case android.util.Log.WARN: 53 priorityStr = "WARN"; 54 break; 55 case android.util.Log.ERROR: 56 priorityStr = "ERROR"; 57 break; 58 case android.util.Log.ASSERT: 59 priorityStr = "ASSERT"; 60 break; 61 default: 62 break; 63 } 64 65 // Handily, the Log class has a facility for converting a stack trace into a usable string. 66 String exceptionStr = null; 67 if (tr != null) { 68 exceptionStr = android.util.Log.getStackTraceString(tr); 69 } 70 71 // Take the priority, tag, message, and exception, and concatenate as necessary 72 // into one usable line of text. 73 final StringBuilder outputBuilder = new StringBuilder(); 74 75 String delimiter = "\t"; 76 appendIfNotNull(outputBuilder, priorityStr, delimiter); 77 appendIfNotNull(outputBuilder, tag, delimiter); 78 appendIfNotNull(outputBuilder, msg, delimiter); 79 appendIfNotNull(outputBuilder, exceptionStr, delimiter); 80 81 // In case this was originally called from an AsyncTask or some other off-UI thread, 82 // make sure the update occurs within the UI thread. 83 ((Activity) getContext()).runOnUiThread( (new Thread(new Runnable() { 84 @Override 85 public void run() { 86 // Display the text we just generated within the LogView. 87 appendToLog(outputBuilder.toString()); 88 } 89 }))); 90 91 if (mNext != null) { 92 mNext.println(priority, tag, msg, tr); 93 } 94 } 95 96 public LogNode getNext() { 97 return mNext; 98 } 99 100 public void setNext(LogNode node) { 101 mNext = node; 102 } 103 104 /** Takes a string and adds to it, with a separator, if the bit to be added isn't null. Since 105 * the logger takes so many arguments that might be null, this method helps cut out some of the 106 * agonizing tedium of writing the same 3 lines over and over. 107 * @param source StringBuilder containing the text to append to. 108 * @param addStr The String to append 109 * @param delimiter The String to separate the source and appended strings. A tab or comma, 110 * for instance. 111 * @return The fully concatenated String as a StringBuilder 112 */ 113 private StringBuilder appendIfNotNull(StringBuilder source, String addStr, String delimiter) { 114 if (addStr != null) { 115 if (addStr.length() == 0) { 116 delimiter = ""; 117 } 118 119 return source.append(addStr).append(delimiter); 120 } 121 return source; 122 } 123 124 // The next LogNode in the chain. 125 LogNode mNext; 126 127 /** Outputs the string as a new line of log data in the LogView. */ 128 public void appendToLog(String s) { 129 append("\n" + s); 130 } 131 132 133 }