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
17package com.android.inputmethod.latin.utils;
18
19import android.util.Log;
20
21import com.android.inputmethod.latin.LatinImeLogger;
22
23/**
24 * A class for logging and debugging utility methods.
25 */
26public final class DebugLogUtils {
27    private final static String TAG = DebugLogUtils.class.getSimpleName();
28    private final static boolean sDBG = LatinImeLogger.sDBG;
29
30    /**
31     * Calls .toString() on its non-null argument or returns "null"
32     * @param o the object to convert to a string
33     * @return the result of .toString() or null
34     */
35    public static String s(final Object o) {
36        return null == o ? "null" : o.toString();
37    }
38
39    /**
40     * Get the string representation of the current stack trace, for debugging purposes.
41     * @return a readable, carriage-return-separated string for the current stack trace.
42     */
43    public static String getStackTrace() {
44        return getStackTrace(Integer.MAX_VALUE - 1);
45    }
46
47    /**
48     * Get the string representation of the current stack trace, for debugging purposes.
49     * @param limit the maximum number of stack frames to be returned.
50     * @return a readable, carriage-return-separated string for the current stack trace.
51     */
52    public static String getStackTrace(final int limit) {
53        final StringBuilder sb = new StringBuilder();
54        try {
55            throw new RuntimeException();
56        } catch (final RuntimeException e) {
57            final StackTraceElement[] frames = e.getStackTrace();
58            // Start at 1 because the first frame is here and we don't care about it
59            for (int j = 1; j < frames.length && j < limit + 1; ++j) {
60                sb.append(frames[j].toString() + "\n");
61            }
62        }
63        return sb.toString();
64    }
65
66    /**
67     * Get the stack trace contained in an exception as a human-readable string.
68     * @param t the throwable
69     * @return the human-readable stack trace
70     */
71    public static String getStackTrace(final Throwable t) {
72        final StringBuilder sb = new StringBuilder();
73        final StackTraceElement[] frames = t.getStackTrace();
74        for (int j = 0; j < frames.length; ++j) {
75            sb.append(frames[j].toString() + "\n");
76        }
77        return sb.toString();
78    }
79
80    /**
81     * Helper log method to ease null-checks and adding spaces.
82     *
83     * This sends all arguments to the log, separated by spaces. Any null argument is converted
84     * to the "null" string. It uses a very visible tag and log level for debugging purposes.
85     *
86     * @param args the stuff to send to the log
87     */
88    public static void l(final Object... args) {
89        if (!sDBG) return;
90        final StringBuilder sb = new StringBuilder();
91        for (final Object o : args) {
92            sb.append(s(o).toString());
93            sb.append(" ");
94        }
95        Log.e(TAG, sb.toString());
96    }
97
98    /**
99     * Helper log method to put stuff in red.
100     *
101     * This does the same as #l but prints in red
102     *
103     * @param args the stuff to send to the log
104     */
105    public static void r(final Object... args) {
106        if (!sDBG) return;
107        final StringBuilder sb = new StringBuilder("\u001B[31m");
108        for (final Object o : args) {
109            sb.append(s(o).toString());
110            sb.append(" ");
111        }
112        sb.append("\u001B[0m");
113        Log.e(TAG, sb.toString());
114    }
115}
116