Home | History | Annotate | Download | only in swipeablelistview
      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.android.deskclock.widget.swipeablelistview;
     17 
     18 import android.text.TextUtils;
     19 import android.util.Log;
     20 
     21 import java.util.regex.Pattern;
     22 
     23 public class LogUtils {
     24 
     25     public static final String TAG = "AlarmClock";
     26 
     27     // "GMT" + "+" or "-" + 4 digits
     28     private static final Pattern DATE_CLEANUP_PATTERN_WRONG_TIMEZONE =
     29             Pattern.compile("GMT([-+]\\d{4})$");
     30 
     31     /**
     32      * Priority constant for the println method; use LogUtils.v.
     33      */
     34     public static final int VERBOSE = Log.VERBOSE;
     35 
     36     /**
     37      * Priority constant for the println method; use LogUtils.d.
     38      */
     39     public static final int DEBUG = Log.DEBUG;
     40 
     41     /**
     42      * Priority constant for the println method; use LogUtils.i.
     43      */
     44     public static final int INFO = Log.INFO;
     45 
     46     /**
     47      * Priority constant for the println method; use LogUtils.w.
     48      */
     49     public static final int WARN = Log.WARN;
     50 
     51     /**
     52      * Priority constant for the println method; use LogUtils.e.
     53      */
     54     public static final int ERROR = Log.ERROR;
     55 
     56     /**
     57      * Used to enable/disable logging that we don't want included in
     58      * production releases.
     59      */
     60     private static final int MAX_ENABLED_LOG_LEVEL = ERROR;
     61 
     62     private static Boolean sDebugLoggingEnabledForTests = null;
     63 
     64     /**
     65      * Enable debug logging for unit tests.
     66      */
     67     //@VisibleForTesting
     68     static void setDebugLoggingEnabledForTests(boolean enabled) {
     69         sDebugLoggingEnabledForTests = Boolean.valueOf(enabled);
     70     }
     71 
     72     /**
     73      * Returns true if the build configuration prevents debug logging.
     74      */
     75     //@VisibleForTesting
     76     public static boolean buildPreventsDebugLogging() {
     77         return MAX_ENABLED_LOG_LEVEL > VERBOSE;
     78     }
     79 
     80     /**
     81      * Returns a boolean indicating whether debug logging is enabled.
     82      */
     83     protected static boolean isDebugLoggingEnabled(String tag) {
     84         if (buildPreventsDebugLogging()) {
     85             return false;
     86         }
     87         if (sDebugLoggingEnabledForTests != null) {
     88             return sDebugLoggingEnabledForTests.booleanValue();
     89         }
     90         return Log.isLoggable(tag, Log.DEBUG);
     91     }
     92 
     93     /**
     94      * Checks to see whether or not a log for the specified tag is loggable at the specified level.
     95      */
     96     public static boolean isLoggable(String tag, int level) {
     97         if (MAX_ENABLED_LOG_LEVEL > level) {
     98             return false;
     99         }
    100         return Log.isLoggable(tag, level);
    101     }
    102 
    103     /**
    104      * Send a {@link #VERBOSE} log message.
    105      * @param tag Used to identify the source of a log message.  It usually identifies
    106      *        the class or activity where the log call occurs.
    107      * @param format the format string (see {@link java.util.Formatter#format})
    108      * @param args
    109      *            the list of arguments passed to the formatter. If there are
    110      *            more arguments than required by {@code format},
    111      *            additional arguments are ignored.
    112      */
    113     public static int v(String tag, String format, Object... args) {
    114         if (isLoggable(tag, VERBOSE)) {
    115             return Log.v(tag, String.format(format, args));
    116         }
    117         return 0;
    118     }
    119 
    120     /**
    121      * Send a {@link #VERBOSE} log message.
    122      * @param tag Used to identify the source of a log message.  It usually identifies
    123      *        the class or activity where the log call occurs.
    124      * @param tr An exception to log
    125      * @param format the format string (see {@link java.util.Formatter#format})
    126      * @param args
    127      *            the list of arguments passed to the formatter. If there are
    128      *            more arguments than required by {@code format},
    129      *            additional arguments are ignored.
    130      */
    131     public static int v(String tag, Throwable tr, String format, Object... args) {
    132         if (isLoggable(tag, VERBOSE)) {
    133             return Log.v(tag, String.format(format, args), tr);
    134         }
    135         return 0;
    136     }
    137 
    138     /**
    139      * Send a {@link #DEBUG} log message.
    140      * @param tag Used to identify the source of a log message.  It usually identifies
    141      *        the class or activity where the log call occurs.
    142      * @param format the format string (see {@link java.util.Formatter#format})
    143      * @param args
    144      *            the list of arguments passed to the formatter. If there are
    145      *            more arguments than required by {@code format},
    146      *            additional arguments are ignored.
    147      */
    148     public static int d(String tag, String format, Object... args) {
    149         if (isLoggable(tag, DEBUG)) {
    150             return Log.d(tag, String.format(format, args));
    151         }
    152         return 0;
    153     }
    154 
    155     /**
    156      * Send a {@link #DEBUG} log message.
    157      * @param tag Used to identify the source of a log message.  It usually identifies
    158      *        the class or activity where the log call occurs.
    159      * @param tr An exception to log
    160      * @param format the format string (see {@link java.util.Formatter#format})
    161      * @param args
    162      *            the list of arguments passed to the formatter. If there are
    163      *            more arguments than required by {@code format},
    164      *            additional arguments are ignored.
    165      */
    166     public static int d(String tag, Throwable tr, String format, Object... args) {
    167         if (isLoggable(tag, DEBUG)) {
    168             return Log.d(tag, String.format(format, args), tr);
    169         }
    170         return 0;
    171     }
    172 
    173     /**
    174      * Send a {@link #INFO} log message.
    175      * @param tag Used to identify the source of a log message.  It usually identifies
    176      *        the class or activity where the log call occurs.
    177      * @param format the format string (see {@link java.util.Formatter#format})
    178      * @param args
    179      *            the list of arguments passed to the formatter. If there are
    180      *            more arguments than required by {@code format},
    181      *            additional arguments are ignored.
    182      */
    183     public static int i(String tag, String format, Object... args) {
    184         if (isLoggable(tag, INFO)) {
    185             return Log.i(tag, String.format(format, args));
    186         }
    187         return 0;
    188     }
    189 
    190     /**
    191      * Send a {@link #INFO} log message.
    192      * @param tag Used to identify the source of a log message.  It usually identifies
    193      *        the class or activity where the log call occurs.
    194      * @param tr An exception to log
    195      * @param format the format string (see {@link java.util.Formatter#format})
    196      * @param args
    197      *            the list of arguments passed to the formatter. If there are
    198      *            more arguments than required by {@code format},
    199      *            additional arguments are ignored.
    200      */
    201     public static int i(String tag, Throwable tr, String format, Object... args) {
    202         if (isLoggable(tag, INFO)) {
    203             return Log.i(tag, String.format(format, args), tr);
    204         }
    205         return 0;
    206     }
    207 
    208     /**
    209      * Send a {@link #WARN} log message.
    210      * @param tag Used to identify the source of a log message.  It usually identifies
    211      *        the class or activity where the log call occurs.
    212      * @param format the format string (see {@link java.util.Formatter#format})
    213      * @param args
    214      *            the list of arguments passed to the formatter. If there are
    215      *            more arguments than required by {@code format},
    216      *            additional arguments are ignored.
    217      */
    218     public static int w(String tag, String format, Object... args) {
    219         if (isLoggable(tag, WARN)) {
    220             return Log.w(tag, String.format(format, args));
    221         }
    222         return 0;
    223     }
    224 
    225     /**
    226      * Send a {@link #WARN} log message.
    227      * @param tag Used to identify the source of a log message.  It usually identifies
    228      *        the class or activity where the log call occurs.
    229      * @param tr An exception to log
    230      * @param format the format string (see {@link java.util.Formatter#format})
    231      * @param args
    232      *            the list of arguments passed to the formatter. If there are
    233      *            more arguments than required by {@code format},
    234      *            additional arguments are ignored.
    235      */
    236     public static int w(String tag, Throwable tr, String format, Object... args) {
    237         if (isLoggable(tag, WARN)) {
    238             return Log.w(tag, String.format(format, args), tr);
    239         }
    240         return 0;
    241     }
    242 
    243     /**
    244      * Send a {@link #ERROR} log message.
    245      * @param tag Used to identify the source of a log message.  It usually identifies
    246      *        the class or activity where the log call occurs.
    247      * @param format the format string (see {@link java.util.Formatter#format})
    248      * @param args
    249      *            the list of arguments passed to the formatter. If there are
    250      *            more arguments than required by {@code format},
    251      *            additional arguments are ignored.
    252      */
    253     public static int e(String tag, String format, Object... args) {
    254         if (isLoggable(tag, ERROR)) {
    255             return Log.e(tag, String.format(format, args));
    256         }
    257         return 0;
    258     }
    259 
    260     /**
    261      * Send a {@link #ERROR} log message.
    262      * @param tag Used to identify the source of a log message.  It usually identifies
    263      *        the class or activity where the log call occurs.
    264      * @param tr An exception to log
    265      * @param format the format string (see {@link java.util.Formatter#format})
    266      * @param args
    267      *            the list of arguments passed to the formatter. If there are
    268      *            more arguments than required by {@code format},
    269      *            additional arguments are ignored.
    270      */
    271     public static int e(String tag, Throwable tr, String format, Object... args) {
    272         if (isLoggable(tag, ERROR)) {
    273             return Log.e(tag, String.format(format, args), tr);
    274         }
    275         return 0;
    276     }
    277 
    278     /**
    279      * What a Terrible Failure: Report a condition that should never happen.
    280      * The error will always be logged at level ASSERT with the call stack.
    281      * Depending on system configuration, a report may be added to the
    282      * {@link android.os.DropBoxManager} and/or the process may be terminated
    283      * immediately with an error dialog.
    284      * @param tag Used to identify the source of a log message.  It usually identifies
    285      *        the class or activity where the log call occurs.
    286      * @param format the format string (see {@link java.util.Formatter#format})
    287      * @param args
    288      *            the list of arguments passed to the formatter. If there are
    289      *            more arguments than required by {@code format},
    290      *            additional arguments are ignored.
    291      */
    292     public static int wtf(String tag, String format, Object... args) {
    293         return Log.wtf(tag, String.format(format, args), new Error());
    294     }
    295 
    296     /**
    297      * What a Terrible Failure: Report a condition that should never happen.
    298      * The error will always be logged at level ASSERT with the call stack.
    299      * Depending on system configuration, a report may be added to the
    300      * {@link android.os.DropBoxManager} and/or the process may be terminated
    301      * immediately with an error dialog.
    302      * @param tag Used to identify the source of a log message.  It usually identifies
    303      *        the class or activity where the log call occurs.
    304      * @param tr An exception to log
    305      * @param format the format string (see {@link java.util.Formatter#format})
    306      * @param args
    307      *            the list of arguments passed to the formatter. If there are
    308      *            more arguments than required by {@code format},
    309      *            additional arguments are ignored.
    310      */
    311     public static int wtf(String tag, Throwable tr, String format, Object... args) {
    312         return Log.wtf(tag, String.format(format, args), tr);
    313     }
    314 
    315 
    316     /**
    317      * Try to make a date MIME(RFC 2822/5322)-compliant.
    318      *
    319      * It fixes:
    320      * - "Thu, 10 Dec 09 15:08:08 GMT-0700" to "Thu, 10 Dec 09 15:08:08 -0700"
    321      *   (4 digit zone value can't be preceded by "GMT")
    322      *   We got a report saying eBay sends a date in this format
    323      */
    324     public static String cleanUpMimeDate(String date) {
    325         if (TextUtils.isEmpty(date)) {
    326             return date;
    327         }
    328         date = DATE_CLEANUP_PATTERN_WRONG_TIMEZONE.matcher(date).replaceFirst("$1");
    329         return date;
    330     }
    331 
    332 
    333     public static String byteToHex(int b) {
    334         return byteToHex(new StringBuilder(), b).toString();
    335     }
    336 
    337     public static StringBuilder byteToHex(StringBuilder sb, int b) {
    338         b &= 0xFF;
    339         sb.append("0123456789ABCDEF".charAt(b >> 4));
    340         sb.append("0123456789ABCDEF".charAt(b & 0xF));
    341         return sb;
    342     }
    343 
    344 }
    345