Logger.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.yalantis.phoenix.util;
  2. import android.text.TextUtils;
  3. public final class Logger {
  4. private static final String TAG = "Phoenix";
  5. /**
  6. * Set true or false if you want read logs or not
  7. */
  8. private static boolean logEnabled_d = false;
  9. private static boolean logEnabled_i = false;
  10. private static boolean logEnabled_e = false;
  11. public static void d() {
  12. if (logEnabled_d) {
  13. android.util.Log.v(TAG, getLocation());
  14. }
  15. }
  16. public static void d(String msg) {
  17. if (logEnabled_d) {
  18. android.util.Log.v(TAG, getLocation() + msg);
  19. }
  20. }
  21. public static void i(String msg) {
  22. if (logEnabled_i) {
  23. android.util.Log.i(TAG, getLocation() + msg);
  24. }
  25. }
  26. public static void i() {
  27. if (logEnabled_i) {
  28. android.util.Log.i(TAG, getLocation());
  29. }
  30. }
  31. public static void e(String msg) {
  32. if (logEnabled_e) {
  33. android.util.Log.e(TAG, getLocation() + msg);
  34. }
  35. }
  36. public static void e(String msg, Throwable e) {
  37. if (logEnabled_e) {
  38. android.util.Log.e(TAG, getLocation() + msg, e);
  39. }
  40. }
  41. public static void e(Throwable e) {
  42. if (logEnabled_e) {
  43. android.util.Log.e(TAG, getLocation(), e);
  44. }
  45. }
  46. public static void e() {
  47. if (logEnabled_e) {
  48. android.util.Log.e(TAG, getLocation());
  49. }
  50. }
  51. private static String getLocation() {
  52. final String className = Logger.class.getName();
  53. final StackTraceElement[] traces = Thread.currentThread()
  54. .getStackTrace();
  55. boolean found = false;
  56. for (StackTraceElement trace : traces) {
  57. try {
  58. if (found) {
  59. if (!trace.getClassName().startsWith(className)) {
  60. Class<?> clazz = Class.forName(trace.getClassName());
  61. return "[" + getClassName(clazz) + ":"
  62. + trace.getMethodName() + ":"
  63. + trace.getLineNumber() + "]: ";
  64. }
  65. } else if (trace.getClassName().startsWith(className)) {
  66. found = true;
  67. }
  68. } catch (ClassNotFoundException ignored) {
  69. }
  70. }
  71. return "[]: ";
  72. }
  73. private static String getClassName(Class<?> clazz) {
  74. if (clazz != null) {
  75. if (!TextUtils.isEmpty(clazz.getSimpleName())) {
  76. return clazz.getSimpleName();
  77. }
  78. return getClassName(clazz.getEnclosingClass());
  79. }
  80. return "";
  81. }
  82. }