/mp3playerlib/src/main/java/com/zlw/main/mp3playerlib/utils/Logger.java

https://github.com/zhaolewei/MusicVisualizer · Java · 282 lines · 204 code · 43 blank · 35 comment · 26 complexity · 7eccf8365794c5b86afd49bd67d06759 MD5 · raw file

  1. package com.zlw.main.mp3playerlib.utils;
  2. import android.annotation.TargetApi;
  3. import android.content.Context;
  4. import android.os.Build;
  5. import android.os.Environment;
  6. import android.os.SystemClock;
  7. import android.util.Log;
  8. import java.io.File;
  9. import java.util.Locale;
  10. public class Logger {
  11. private static final String PRE = "^_^";
  12. private static final String TAG = Logger.class.getSimpleName();
  13. private static final int LOG_LENGTH_LIMITATION = 4000;
  14. public static boolean IsDebug = true;
  15. private static final String space = "====================================================================================================";
  16. private static boolean LOGV = true;
  17. private static boolean LOGD = true;
  18. private static boolean LOGI = true;
  19. private static boolean LOGW = true;
  20. private static boolean LOGE = true;
  21. public enum LogLevel {
  22. V, D, I, W, E
  23. }
  24. // private static boolean LOGV = false;
  25. // private static boolean LOGD = false;
  26. // private static boolean LOGI = false;
  27. // private static boolean LOGW = false;
  28. // private static boolean LOGE = false;
  29. public static void v(String tag, String format, Object... args) {
  30. if (LOGV) {
  31. String message = buildMessage(format, args);
  32. tag = formatLength(PRE + tag, 28);
  33. Log.v(tag, message);
  34. cacheLongLog(tag, message);
  35. }
  36. }
  37. public static void v(Throwable throwable, String tag, String format, Object... args) {
  38. if (LOGV) {
  39. String message = buildMessage(format, args);
  40. tag = formatLength(PRE + tag, 28);
  41. Log.v(tag, message, throwable);
  42. cacheLongLog(tag, message, throwable);
  43. }
  44. }
  45. public static void d(String tag, String format, Object... args) {
  46. if (LOGD) {
  47. String message = buildMessage(format, args);
  48. tag = formatLength(PRE + tag, 28);
  49. Log.d(tag, message);
  50. cacheLongLog(tag, message);
  51. }
  52. }
  53. public static void d(Throwable throwable, String tag, String format, Object... args) {
  54. if (LOGD) {
  55. String message = buildMessage(format, args);
  56. tag = formatLength(PRE + tag, 28);
  57. Log.d(tag, message, throwable);
  58. cacheLongLog(tag, message, throwable);
  59. }
  60. }
  61. public static void i(String tag, String format, Object... args) {
  62. if (LOGI) {
  63. String message = buildMessage(format, args);
  64. tag = formatLength(PRE + tag, 28);
  65. Log.i(tag, message);
  66. cacheLongLog(tag, message);
  67. }
  68. }
  69. public static void i(Throwable throwable, String tag, String format, Object... args) {
  70. if (LOGI) {
  71. String message = buildMessage(format, args);
  72. tag = formatLength(PRE + tag, 28);
  73. Log.i(tag, message, throwable);
  74. cacheLongLog(tag, message, throwable);
  75. }
  76. }
  77. public static void w(String tag, String format, Object... args) {
  78. if (LOGW) {
  79. String message = buildMessage(format, args);
  80. tag = formatLength(PRE + tag, 28);
  81. Log.w(tag, message);
  82. cacheLongLog(tag, message);
  83. }
  84. }
  85. public static void w(Throwable throwable, String tag, String format, Object... args) {
  86. if (LOGW) {
  87. String message = buildMessage(format, args);
  88. tag = formatLength(PRE + tag, 28);
  89. Log.w(tag, message, throwable);
  90. cacheLongLog(tag, message, throwable);
  91. }
  92. }
  93. public static void e(String tag, String format, Object... args) {
  94. if (LOGE) {
  95. String message = buildMessage(format, args);
  96. tag = formatLength(PRE + tag, 28);
  97. Log.e(tag, message);
  98. cacheLongLog(tag, message);
  99. }
  100. }
  101. public static void e(Throwable throwable, String tag, String format, Object... args) {
  102. if (LOGE) {
  103. String message = buildMessage(format, args);
  104. tag = formatLength(PRE + tag, 28);
  105. Log.e(tag, message, throwable);
  106. cacheLongLog(tag, message, throwable);
  107. }
  108. }
  109. /**
  110. * Please refer to comment of {@link #cacheLongLog(String, String, Throwable)}
  111. *
  112. * @param tag TAG name.
  113. * @param logContent Log content.
  114. */
  115. private static void cacheLongLog(String tag, String logContent) {
  116. cacheLongLog(tag, logContent, null);
  117. }
  118. /**
  119. * Due to length limitation of Logcat, over long log content won't be shown completely in log window,<br/>
  120. * so cache it to local file at particular path for convenient checking.
  121. *
  122. * @param tag TAG name.
  123. * @param logContent Log content.
  124. * @param throwable Throwable instance, for printing stack trace.
  125. */
  126. @TargetApi(Build.VERSION_CODES.KITKAT)
  127. private static void cacheLongLog(String tag, String logContent, Throwable throwable) {
  128. }
  129. /**
  130. * 打印调用者栈信息
  131. * 本方法使用System.out输出, 对log进行过滤时请注意
  132. */
  133. public static void printCaller() {
  134. if (!IsDebug) {
  135. return;
  136. }
  137. try {
  138. String caller, callingClass, callFile;
  139. int lineNumber;
  140. StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
  141. StringBuilder infoBuffer = new StringBuilder();
  142. infoBuffer.append("print caller info\n==========BEGIN OF CALLER INFO============\n");
  143. for (int i = 2; i < trace.length; i++) {
  144. callingClass = trace[i].getClassName();
  145. callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
  146. caller = trace[i].getMethodName();
  147. callFile = trace[i].getFileName();
  148. lineNumber = trace[i].getLineNumber();
  149. String method = String.format(Locale.US, "[%03d] %s.%s(%s:%d)"
  150. , Thread.currentThread().getId(), callingClass, caller, callFile, lineNumber);
  151. infoBuffer.append(method);
  152. infoBuffer.append("\n");
  153. }
  154. infoBuffer.append("==========END OF CALLER INFO============");
  155. Logger.i(TAG, infoBuffer.toString());
  156. } catch (Exception e) {
  157. Logger.e(e, TAG, e.getMessage());
  158. }
  159. }
  160. private static String buildMessage(String format, Object[] args) {
  161. try {
  162. String msg = (args == null || args.length == 0) ? format : String.format(Locale.US, format, args);
  163. if (!IsDebug) {
  164. return msg;
  165. }
  166. StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
  167. String caller = "";
  168. String callingClass = "";
  169. String callFile = "";
  170. int lineNumber = 0;
  171. for (int i = 2; i < trace.length; i++) {
  172. Class<?> clazz = trace[i].getClass();
  173. if (!clazz.equals(Logger.class)) {
  174. callingClass = trace[i].getClassName();
  175. callingClass = callingClass.substring(callingClass
  176. .lastIndexOf('.') + 1);
  177. caller = trace[i].getMethodName();
  178. callFile = trace[i].getFileName();
  179. lineNumber = trace[i].getLineNumber();
  180. break;
  181. }
  182. }
  183. String method = String.format(Locale.US, "[%03d] %s.%s(%s:%d)"
  184. , Thread.currentThread().getId(), callingClass, caller, callFile, lineNumber);
  185. return String.format(Locale.US, "%s> %s", formatLength(method, 93), msg);
  186. } catch (Exception e) {
  187. Logger.e(e, TAG, e.getMessage());
  188. }
  189. return "----->ERROR LOG STRING<------";
  190. }
  191. private static String formatLength(String src, int len) {
  192. StringBuilder sb = new StringBuilder();
  193. if (src.length() >= len) {
  194. sb.append(src);
  195. } else {
  196. sb.append(src);
  197. sb.append(space.substring(0, len - src.length()));
  198. }
  199. return sb.toString();
  200. }
  201. @TargetApi(Build.VERSION_CODES.KITKAT)
  202. public static File getAvailableExternalCacheDir(Context ctx) {
  203. File[] cacheDirectories = ctx.getExternalCacheDirs();
  204. for (int index = cacheDirectories.length - 1; index >= 0; index--) {
  205. File file = cacheDirectories[index];
  206. if (null != file && Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(file))) {
  207. return file;
  208. }
  209. }
  210. return null;
  211. }
  212. /**
  213. * 判断文件是否存在
  214. *
  215. * @param filePath 文件路径
  216. * @return {@code true}: 存在<br>{@code false}: 不存在
  217. */
  218. public static boolean isFileExists(String filePath) {
  219. return isFileExists(new File(filePath));
  220. }
  221. /**
  222. * 判断文件是否存在
  223. *
  224. * @param file 文件
  225. * @return {@code true}: 存在<br>{@code false}: 不存在
  226. */
  227. public static boolean isFileExists(File file) {
  228. return file != null && file.exists();
  229. }
  230. public static class TimeCalculator {
  231. long start;
  232. public TimeCalculator() {
  233. start = SystemClock.elapsedRealtime();
  234. }
  235. public long end() {
  236. return SystemClock.elapsedRealtime() - start;
  237. }
  238. }
  239. }