Alexander Lucas | 919cb71 | 2013-06-06 14:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.example.android.common.logger; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.text.method.ScrollingMovementMethod; |
| 20 | import android.util.*; |
| 21 | import android.widget.TextView; |
| 22 | |
| 23 | /** |
| 24 | * Created by alexlucas on 6/4/13. |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | public class LogView extends TextView implements LogNode { |
| 29 | |
| 30 | public LogView(Context context) { |
| 31 | super(context); |
| 32 | } |
| 33 | |
| 34 | public LogView(Context context, AttributeSet attrs) { |
| 35 | super(context, attrs); |
| 36 | } |
| 37 | |
| 38 | public LogView(Context context, AttributeSet attrs, int defStyle) { |
| 39 | super(context, attrs, defStyle); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Formats the log data and prints it out to the LogView. |
| 44 | * @param priority Log level of the data being logged. Verbose, Error, etc. |
| 45 | * @param tag Tag for for the log data. Can be used to organize log statements. |
| 46 | * @param msg The actual message to be logged. The actual message to be logged. |
| 47 | * @param tr If an exception was thrown, this can be sent along for the logging facilities |
| 48 | * to extract and print useful information. |
| 49 | */ |
| 50 | @Override |
| 51 | public void println(int priority, String tag, String msg, Throwable tr) { |
| 52 | String priorityStr = null; |
| 53 | |
| 54 | // For the purposes of this View, we want to print the priority as readable text. |
| 55 | switch(priority) { |
| 56 | case android.util.Log.VERBOSE: |
| 57 | priorityStr = "VERBOSE"; |
| 58 | break; |
| 59 | case android.util.Log.DEBUG: |
| 60 | priorityStr = "DEBUG"; |
| 61 | break; |
| 62 | case android.util.Log.INFO: |
| 63 | priorityStr = "INFO"; |
| 64 | break; |
| 65 | case android.util.Log.WARN: |
| 66 | priorityStr = "WARN"; |
| 67 | break; |
| 68 | case android.util.Log.ERROR: |
| 69 | priorityStr = "ERROR"; |
| 70 | break; |
| 71 | case android.util.Log.ASSERT: |
| 72 | priorityStr = "ASSERT"; |
| 73 | break; |
| 74 | default: |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | // Handily, the Log class has a facility for converting a stack trace into a useable string. |
| 79 | String exceptionStr = null; |
| 80 | if (tr != null) { |
| 81 | exceptionStr = android.util.Log.getStackTraceString(tr); |
| 82 | } |
| 83 | |
| 84 | // Take the priority, tag, message, and exception, and concatenate as necessary |
| 85 | // into one usable line of text. |
| 86 | StringBuilder outputBuilder = new StringBuilder(); |
| 87 | |
| 88 | appendIfNotNull(outputBuilder, priorityStr, delimiter); |
| 89 | appendIfNotNull(outputBuilder, tag, delimiter); |
| 90 | appendIfNotNull(outputBuilder, msg, delimiter); |
| 91 | appendIfNotNull(outputBuilder, exceptionStr, delimiter); |
| 92 | |
| 93 | // Actually display the text we just generated within the LogView. |
| 94 | appendToLog(outputBuilder.toString()); |
| 95 | |
| 96 | if (mNext != null) { |
| 97 | mNext.println(priority, tag, msg, tr); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public LogNode getNext() { |
| 102 | return mNext; |
| 103 | } |
| 104 | |
| 105 | public void setNext(LogNode node) { |
| 106 | mNext = node; |
| 107 | } |
| 108 | |
| 109 | /** Takes a string and adds to it, with a seperator, if the bit to be added isn't null. Since |
| 110 | * the logger takes so many arguments that might be null, this method helps cut out some of the |
| 111 | * agonizing tedium of writing the same 3 lines over and over. |
| 112 | * @param source Stringbuilder containing the text to append to. |
| 113 | * @param addStrStr The String to append |
| 114 | * @param delimiter The String to seperate the source and appendee strings. A tab or comma, |
| 115 | * for instance. |
| 116 | * @return The fully concatenated String as a StringBuilder |
| 117 | */ |
| 118 | private StringBuilder appendIfNotNull(StringBuilder source, String addStr, String delimiter) { |
| 119 | if (addStr != null) { |
| 120 | if (addStr.length() == 0) { |
| 121 | delimiter = ""; |
| 122 | } |
| 123 | |
| 124 | return source.append(addStr).append(delimiter); |
| 125 | } |
| 126 | return source; |
| 127 | } |
| 128 | |
| 129 | // The next LogNode in the chain. |
| 130 | LogNode mNext; |
| 131 | |
| 132 | /** Outputs the string as a new line of log data in the LogView. */ |
| 133 | public void appendToLog(String s) { |
| 134 | append("\n" + s); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | } |