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