Log_OC.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.owncloud.android.utils;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.Locale;
  9. import com.owncloud.android.MainApp;
  10. import android.util.Log;
  11. public class Log_OC {
  12. private static boolean isEnabled = false;
  13. private static File logFile;
  14. private static File folder;
  15. private static BufferedWriter buf;
  16. public static void i(String TAG, String message){
  17. // Printing the message to LogCat console
  18. int a = Log.i(TAG, message);
  19. // Write the log message to the file
  20. appendLog(TAG+" : "+message);
  21. }
  22. public static void d(String TAG, String message){
  23. Log.d(TAG, message);
  24. appendLog(TAG + " : " + message);
  25. }
  26. public static void d(String TAG, String message, Exception e) {
  27. Log.d(TAG, message, e);
  28. appendLog(TAG + " : " + message + " Exception : "+ e.getStackTrace());
  29. }
  30. public static void e(String TAG, String message){
  31. Log.e(TAG, message);
  32. appendLog(TAG + " : " + message);
  33. }
  34. public static void e(String TAG, String message, Throwable e) {
  35. Log.e(TAG, message, e);
  36. appendLog(TAG+" : " + message +" Exception : " + e.getStackTrace());
  37. }
  38. public static void v(String TAG, String message){
  39. Log.v(TAG, message);
  40. appendLog(TAG+" : "+ message);
  41. }
  42. public static void w(String TAG, String message) {
  43. Log.w(TAG,message);
  44. appendLog(TAG+" : "+ message);
  45. }
  46. public static void wtf(String TAG, String message) {
  47. Log.wtf(TAG,message);
  48. appendLog(TAG+" : "+ message);
  49. }
  50. public static void startLogging(String logPath) {
  51. folder = new File(logPath);
  52. logFile = new File(folder + File.separator + "log.txt");
  53. if (!folder.exists()) {
  54. folder.mkdirs();
  55. }
  56. if (logFile.exists()) {
  57. logFile.delete();
  58. }
  59. try {
  60. logFile.createNewFile();
  61. buf = new BufferedWriter(new FileWriter(logFile, true));
  62. isEnabled = true;
  63. appendPhoneInfo();
  64. }catch (IOException e){
  65. e.printStackTrace();
  66. }
  67. }
  68. public static void stopLogging() {
  69. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
  70. String currentDateandTime = sdf.format(new Date());
  71. if (logFile != null) {
  72. logFile.renameTo(new File(folder + File.separator + MainApp.getLogName() + currentDateandTime+".log"));
  73. isEnabled = false;
  74. try {
  75. buf.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. }
  81. private static void appendPhoneInfo() {
  82. appendLog("Model : " + android.os.Build.MODEL);
  83. appendLog("Brand : " + android.os.Build.BRAND);
  84. appendLog("Product : " + android.os.Build.PRODUCT);
  85. appendLog("Device : " + android.os.Build.DEVICE);
  86. appendLog("Version-Codename : " + android.os.Build.VERSION.CODENAME);
  87. appendLog("Version-Release : " + android.os.Build.VERSION.RELEASE);
  88. }
  89. private static void appendLog(String text) {
  90. if (isEnabled) {
  91. try {
  92. buf.append(text);
  93. buf.newLine();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. }
  99. }