PageRenderTime 43ms CodeModel.GetById 29ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/TalkBack/src/com/google/android/marvin/talkback/LogUtils.java

http://eyes-free.googlecode.com/
Java | 90 lines | 24 code | 10 blank | 56 comment | 2 complexity | e4d3d1a7e56e67dfabcdf65faa65db29 MD5 | raw file
 1/*
 2 * Copyright (C) 2011 Google Inc.
 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
17package com.google.android.marvin.talkback;
18
19import android.util.Log;
20
21import java.util.IllegalFormatException;
22
23/**
24 * Handles logging formatted strings.
25 */
26public class LogUtils {
27    public static String TAG = "TalkBack";
28
29    /**
30     * The minimum log level that will be printed to the console. Set this to
31     * {@link Log#ERROR} for release or {@link Log#VERBOSE} for debugging.
32     */
33    private static int sLogLevel = Log.ERROR;
34
35    /**
36     * Set the minimum log level that will be printed to the console. Set this
37     * to {@link Log#ERROR} for release or {@link Log#VERBOSE} for debugging.
38     * 
39     * @param level
40     */
41    public static void setLogLevel(int level) {
42        sLogLevel = level;
43    }
44
45    /**
46     * Logs a formatted string to the console using the source object's name as
47     * the log tag. If the source object is null, the default tag (see
48     * {@link LogUtils#TAG} is used.
49     * <p>
50     * Example usage:
51     * 
52     * <pre class="prettyprint">
53     * LogUtils.log(this.getClass(), Log.ERROR, &quot;Invalid value: %d&quot;, value);
54     * </pre>
55     * 
56     * @param source The object that generated the log event.
57     * @param priority The log entry priority, see
58     *            {@link Log#println(int, String, String)}.
59     * @param format A format string, see
60     *            {@link String#format(String, Object...)}.
61     * @param args String formatter arguments.
62     */
63    public static void log(Class<?> source, int priority, String format, Object... args) {
64        if (priority < sLogLevel) {
65            return;
66        }
67
68        final String sourceClass = source == null ? TAG : source.getSimpleName();
69
70        try {
71            Log.println(priority, sourceClass, String.format(format, args));
72        } catch (IllegalFormatException e) {
73            Log.e(TAG, "Bad formatting string: \"format\"", e);
74        }
75    }
76
77    /**
78     * Logs a formatted string to the console using the default tag (see
79     * {@link LogUtils#TAG}.
80     * 
81     * @param priority The log entry priority, see
82     *            {@link Log#println(int, String, String)}.
83     * @param format A format string, see
84     *            {@link String#format(String, Object...)}.
85     * @param args String formatter arguments.
86     */
87    public static void log(int priority, String format, Object... args) {
88        log(null, priority, format, args);
89    }
90}