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