Home | History | Annotate | Download | only in dictionarypack
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.dictionarypack;
     18 
     19 import android.util.Log;
     20 
     21 /**
     22  * A class for various utility methods, especially debugging.
     23  */
     24 public final class Utils {
     25     private final static String TAG = Utils.class.getSimpleName() + ":DEBUG --";
     26     private final static boolean DEBUG = DictionaryProvider.DEBUG;
     27 
     28     /**
     29      * Calls .toString() on its non-null argument or returns "null"
     30      * @param o the object to convert to a string
     31      * @return the result of .toString() or null
     32      */
     33     public static String s(final Object o) {
     34         return null == o ? "null" : o.toString();
     35     }
     36 
     37     /**
     38      * Get the string representation of the current stack trace, for debugging purposes.
     39      * @return a readable, carriage-return-separated string for the current stack trace.
     40      */
     41     public static String getStackTrace() {
     42         final StringBuilder sb = new StringBuilder();
     43         try {
     44             throw new RuntimeException();
     45         } catch (RuntimeException e) {
     46             StackTraceElement[] frames = e.getStackTrace();
     47             // Start at 1 because the first frame is here and we don't care about it
     48             for (int j = 1; j < frames.length; ++j) {
     49                 sb.append(frames[j].toString() + "\n");
     50             }
     51         }
     52         return sb.toString();
     53     }
     54 
     55     /**
     56      * Get the stack trace contained in an exception as a human-readable string.
     57      * @param e the exception
     58      * @return the human-readable stack trace
     59      */
     60     public static String getStackTrace(final Exception e) {
     61         final StringBuilder sb = new StringBuilder();
     62         final StackTraceElement[] frames = e.getStackTrace();
     63         for (int j = 0; j < frames.length; ++j) {
     64             sb.append(frames[j].toString() + "\n");
     65         }
     66         return sb.toString();
     67     }
     68 
     69     /**
     70      * Helper log method to ease null-checks and adding spaces.
     71      *
     72      * This sends all arguments to the log, separated by spaces. Any null argument is converted
     73      * to the "null" string. It uses a very visible tag and log level for debugging purposes.
     74      *
     75      * @param args the stuff to send to the log
     76      */
     77     public static void l(final Object... args) {
     78         if (!DEBUG) return;
     79         final StringBuilder sb = new StringBuilder();
     80         for (final Object o : args) {
     81             sb.append(s(o).toString());
     82             sb.append(" ");
     83         }
     84         Log.e(TAG, sb.toString());
     85     }
     86 
     87     /**
     88      * Helper log method to put stuff in red.
     89      *
     90      * This does the same as #l but prints in red
     91      *
     92      * @param args the stuff to send to the log
     93      */
     94     public static void r(final Object... args) {
     95         if (!DEBUG) return;
     96         final StringBuilder sb = new StringBuilder("\u001B[31m");
     97         for (final Object o : args) {
     98             sb.append(s(o).toString());
     99             sb.append(" ");
    100         }
    101         sb.append("\u001B[0m");
    102         Log.e(TAG, sb.toString());
    103     }
    104 }
    105