DebugLog.java 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2013 Zhang Rui <bbcallen@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Z
Zhang Rui 已提交
17 18 19 20 21 22
package tv.danmaku.ijk.media.player;

import java.util.Locale;
import android.util.Log;

public class DebugLog {
Z
Zhang Rui 已提交
23 24 25 26 27
    public static final boolean ENABLE_ERROR = Pragma.ENABLE_VERBOSE;
    public static final boolean ENABLE_INFO = Pragma.ENABLE_VERBOSE;
    public static final boolean ENABLE_WARN = Pragma.ENABLE_VERBOSE;
    public static final boolean ENABLE_DEBUG = Pragma.ENABLE_VERBOSE;
    public static final boolean ENABLE_VERBOSE = Pragma.ENABLE_VERBOSE;
28

Z
Zhang Rui 已提交
29
    public static int e(String tag, String msg) {
30
        if (ENABLE_ERROR) {
Z
Zhang Rui 已提交
31 32 33 34 35 36 37
            return Log.e(tag, msg);
        }

        return 0;
    }

    public static int e(String tag, String msg, Throwable tr) {
38
        if (ENABLE_ERROR) {
Z
Zhang Rui 已提交
39 40 41 42 43 44 45
            return Log.e(tag, msg, tr);
        }

        return 0;
    }

    public static int efmt(String tag, String fmt, Object... args) {
46
        if (ENABLE_ERROR) {
Z
Zhang Rui 已提交
47 48 49 50 51 52 53 54
            String msg = String.format(Locale.US, fmt, args);
            return Log.e(tag, msg);
        }

        return 0;
    }

    public static int i(String tag, String msg) {
55
        if (ENABLE_INFO) {
Z
Zhang Rui 已提交
56 57 58 59 60 61 62
            return Log.i(tag, msg);
        }

        return 0;
    }

    public static int i(String tag, String msg, Throwable tr) {
63
        if (ENABLE_INFO) {
Z
Zhang Rui 已提交
64 65 66 67 68 69 70
            return Log.i(tag, msg, tr);
        }

        return 0;
    }

    public static int ifmt(String tag, String fmt, Object... args) {
71
        if (ENABLE_INFO) {
Z
Zhang Rui 已提交
72 73 74 75 76 77 78 79
            String msg = String.format(Locale.US, fmt, args);
            return Log.i(tag, msg);
        }

        return 0;
    }

    public static int w(String tag, String msg) {
80
        if (ENABLE_WARN) {
Z
Zhang Rui 已提交
81 82 83 84 85 86 87
            return Log.w(tag, msg);
        }

        return 0;
    }

    public static int w(String tag, String msg, Throwable tr) {
88
        if (ENABLE_WARN) {
Z
Zhang Rui 已提交
89 90 91 92 93 94 95
            return Log.w(tag, msg, tr);
        }

        return 0;
    }

    public static int wfmt(String tag, String fmt, Object... args) {
96
        if (ENABLE_WARN) {
Z
Zhang Rui 已提交
97 98 99 100 101 102 103 104
            String msg = String.format(Locale.US, fmt, args);
            return Log.w(tag, msg);
        }

        return 0;
    }

    public static int d(String tag, String msg) {
105
        if (ENABLE_DEBUG) {
Z
Zhang Rui 已提交
106 107 108 109 110 111 112
            return Log.d(tag, msg);
        }

        return 0;
    }

    public static int d(String tag, String msg, Throwable tr) {
113
        if (ENABLE_DEBUG) {
Z
Zhang Rui 已提交
114 115 116 117 118 119 120
            return Log.d(tag, msg, tr);
        }

        return 0;
    }

    public static int dfmt(String tag, String fmt, Object... args) {
121
        if (ENABLE_DEBUG) {
Z
Zhang Rui 已提交
122 123 124 125 126 127 128 129
            String msg = String.format(Locale.US, fmt, args);
            return Log.d(tag, msg);
        }

        return 0;
    }

    public static int v(String tag, String msg) {
130
        if (ENABLE_VERBOSE) {
Z
Zhang Rui 已提交
131 132 133 134 135 136 137
            return Log.v(tag, msg);
        }

        return 0;
    }

    public static int v(String tag, String msg, Throwable tr) {
138
        if (ENABLE_VERBOSE) {
Z
Zhang Rui 已提交
139 140 141 142 143 144 145
            return Log.v(tag, msg, tr);
        }

        return 0;
    }

    public static int vfmt(String tag, String fmt, Object... args) {
146
        if (ENABLE_VERBOSE) {
Z
Zhang Rui 已提交
147 148 149 150 151 152 153 154
            String msg = String.format(Locale.US, fmt, args);
            return Log.v(tag, msg);
        }

        return 0;
    }

    public static void printStackTrace(Throwable e) {
155
        if (ENABLE_WARN) {
Z
Zhang Rui 已提交
156 157 158 159 160
            e.printStackTrace();
        }
    }

    public static void printCause(Throwable e) {
161
        if (ENABLE_WARN) {
Z
Zhang Rui 已提交
162 163 164 165 166 167 168 169
            Throwable cause = e.getCause();
            if (cause != null)
                e = cause;

            printStackTrace(e);
        }
    }
}