Alexander Lucas | 9b2d682 | 2013-04-19 10:10:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.os.Bundle; |
| 19 | import android.support.v4.app.Fragment; |
| 20 | import android.util.Log; |
| 21 | import android.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
| 24 | import android.widget.ScrollView; |
| 25 | import android.widget.TextView; |
| 26 | |
| 27 | /** |
| 28 | * Simple fragment which contains a TextView and uses is to output log data it receives |
| 29 | * through the LogNode interface. |
| 30 | */ |
| 31 | public class LogFragment extends Fragment implements LogNode { |
| 32 | |
| 33 | // The next LogNode in the chain. |
| 34 | LogNode mNext; |
| 35 | |
| 36 | public LogFragment() {} |
| 37 | |
| 38 | @Override |
| 39 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 40 | Bundle savedInstanceState) { |
| 41 | return inflater.inflate(R.layout.log_fragment, container, false); |
| 42 | } |
| 43 | |
| 44 | public TextView getTextView() { |
| 45 | return (TextView) getActivity().findViewById(R.id.log_output); |
| 46 | } |
| 47 | |
| 48 | /** Outputs the string as a new line of log data in the TextView. */ |
| 49 | public void appendToLog(String s) { |
| 50 | getTextView().append("\n" + s); |
| 51 | ((ScrollView) getActivity().findViewById(R.id.log_scroll)).fullScroll(View.FOCUS_DOWN); |
| 52 | } |
| 53 | |
| 54 | public LogNode getNext() { |
| 55 | return mNext; |
| 56 | } |
| 57 | |
| 58 | public void setNext(LogNode node) { |
| 59 | mNext = node; |
| 60 | } |
| 61 | |
| 62 | /** Takes a string and adds to it, with a seperator, if the bit to be added isn't null. Since |
| 63 | * the logger takes so many arguments that might be null, this method helps cut out some of the |
| 64 | * agonizing tedium of writing the same 3 lines over and over. |
| 65 | * @param sourceStr The String to append to. |
| 66 | * @param appendStr The String to append |
| 67 | * @param delimiter The String to seperate the source and appendee strings. A tab or comma, |
| 68 | * for instance. |
| 69 | * @return The fully concatonated String |
| 70 | */ |
| 71 | private String appendIfNotNull(String sourceStr, String appendStr, String delimiter) { |
| 72 | if (appendStr != null) { |
| 73 | if (appendStr.length() == 0) { |
| 74 | delimiter = ""; |
| 75 | } |
| 76 | sourceStr = TextUtils.concat(sourceStr, delimiter, appendStr); |
| 77 | } |
| 78 | return sourceStr; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Formats the log data and prints it out to the TextView. |
| 83 | * @param priority Log level of the data being logged. Verbose, Error, etc. |
| 84 | * @param tag Tag for for the log data. Can be used to organize log statements. |
| 85 | * @param msg The actual message to be logged. The actual message to be logged. |
| 86 | * @param tr If an exception was thrown, this can be sent along for the logging facilities |
| 87 | * to extract and print useful information. |
| 88 | */ |
| 89 | @Override |
| 90 | public void println(int priority, String tag, String msg, Throwable tr) { |
| 91 | String priorityStr = null; |
| 92 | |
| 93 | // For the purposes of this Fragment, we want to print the priority as readable text. |
| 94 | switch(priority) { |
| 95 | case Log.VERBOSE: |
| 96 | priorityStr = "V"; |
| 97 | break; |
| 98 | case Log.DEBUG: |
| 99 | priorityStr = "D"; |
| 100 | break; |
| 101 | case Log.INFO: |
| 102 | priorityStr = "I"; |
| 103 | break; |
| 104 | case Log.WARN: |
| 105 | priorityStr = "W"; |
| 106 | break; |
| 107 | case Log.ERROR: |
| 108 | priorityStr = "E"; |
| 109 | break; |
| 110 | case Log.ASSERT: |
| 111 | priorityStr = "A"; |
| 112 | break; |
| 113 | default: |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | // Handily, the Log class has a facility for converting a stack trace into a useable string. |
| 118 | String exceptionStr = null; |
| 119 | if (tr != null) { |
| 120 | exceptionStr = Log.getStackTraceString(tr); |
| 121 | } |
| 122 | |
| 123 | // Take the priority, tag, message, and exception, and concatonate as necessary |
| 124 | // into one usable line of text. |
| 125 | String outputStr = ""; |
| 126 | outputStr = appendIfNotNull(outputStr, priorityStr, "\t"); |
| 127 | outputStr = appendIfNotNull(outputStr, tag, "\t"); |
| 128 | outputStr = appendIfNotNull(outputStr, msg, "\t"); |
| 129 | outputStr = appendIfNotNull(outputStr, exceptionStr, "\t"); |
| 130 | |
| 131 | // Actually display the text we just generated within the TextView. |
| 132 | appendToLog(outputStr); |
| 133 | |
| 134 | if (mNext != null) { |
| 135 | mNext.println(priority, tag, msg, tr); |
| 136 | } |
| 137 | } |
| 138 | } |