Add delete history login and chang the date format in logs
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / Log_OC.java
1 package com.owncloud.android.utils;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.text.SimpleDateFormat;
8 import java.util.Calendar;
9
10 import android.util.Log;
11
12
13 public class Log_OC {
14 private static final String SIMPLE_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
15 private static final long MAX_FILE_SIZE = 10000;
16
17 private static File mLogFile;
18 private static File mFolder;
19 private static BufferedWriter mBuf;
20
21 private static String[] mLogFileNames = {"currentLog.txt", "olderLog.txt"};
22 private static String mLogPath = FileStorageUtils.getLogPath();
23
24 private static boolean isMaxFileSizeReached = false;
25
26 public static void i(String TAG, String message){
27
28 // Write the log message to the file
29 appendLog(TAG+" : "+ message);
30 }
31
32 public static void d(String TAG, String message){
33 Log.d(TAG, message);
34 appendLog(TAG + " : " + message);
35 }
36 public static void d(String TAG, String message, Exception e) {
37 Log.d(TAG, message, e);
38 appendLog(TAG + " : " + message + " Exception : "+ e.getStackTrace());
39 }
40 public static void e(String TAG, String message){
41 Log.e(TAG, message);
42 appendLog(TAG + " : " + message);
43 }
44
45 public static void e(String TAG, String message, Throwable e) {
46 Log.e(TAG, message, e);
47 appendLog(TAG+" : " + message +" Exception : " + e.getStackTrace());
48 }
49
50 public static void v(String TAG, String message){
51 Log.v(TAG, message);
52 appendLog(TAG+" : "+ message);
53 }
54
55 public static void w(String TAG, String message) {
56 Log.w(TAG,message);
57 appendLog(TAG+" : "+ message);
58 }
59
60 public static void wtf(String TAG, String message) {
61 Log.wtf(TAG,message);
62 appendLog(TAG+" : "+ message);
63 }
64
65 /**
66 * Start doing logging
67 * @param logPath : path of log file
68 */
69 public static void startLogging(String logPath) {
70 mFolder = new File(logPath);
71 mLogFile = new File(mFolder + File.separator + mLogFileNames[0]);
72
73 boolean isFileCreated = false;
74
75 if (!mFolder.exists()) {
76 mFolder.mkdirs();
77 isFileCreated = true;
78 Log.d("LOG_OC", "Log file created");
79 }
80
81 try {
82
83 if (isMaxFileSizeReached) {
84
85 // Move current log file info to another file (old logs)
86 File olderFile = new File(mFolder + File.separator + mLogFileNames[1]);
87 if (mLogFile.exists()) {
88 mLogFile.renameTo(olderFile);
89 }
90
91 // Construct a new file for current log info
92 mLogFile = new File(mFolder + File.separator + mLogFileNames[0]);
93 isMaxFileSizeReached = false;
94 }
95
96 // Create the current log file if does not exist
97 mLogFile.createNewFile();
98 mBuf = new BufferedWriter(new FileWriter(mLogFile, true));
99 if (isFileCreated) {
100 appendPhoneInfo();
101 }
102
103 // Check if current log file size is bigger than the max file size defined
104 if (mLogFile.length() > MAX_FILE_SIZE) {
105 isMaxFileSizeReached = true;
106 }
107 } catch (IOException e) {
108 e.printStackTrace();
109 }
110 }
111
112 /**
113 * Delete history logging
114 */
115 public static void deleteHistoryLogging() {
116 File folderLogs = new File(mFolder + File.separator);
117 if(folderLogs.isDirectory()){
118 String[] myFiles = folderLogs.list();
119 for (int i=0; i<myFiles.length; i++) {
120 File myFile = new File(folderLogs, myFiles[i]);
121 myFile.delete();
122 }
123 }
124 }
125
126 /**
127 * Append the info of the device
128 */
129 private static void appendPhoneInfo() {
130 appendLog("Model : " + android.os.Build.MODEL);
131 appendLog("Brand : " + android.os.Build.BRAND);
132 appendLog("Product : " + android.os.Build.PRODUCT);
133 appendLog("Device : " + android.os.Build.DEVICE);
134 appendLog("Version-Codename : " + android.os.Build.VERSION.CODENAME);
135 appendLog("Version-Release : " + android.os.Build.VERSION.RELEASE);
136 }
137
138 /**
139 * Append to the log file the info passed
140 * @param text : text for adding to the log file
141 */
142 private static void appendLog(String text) {
143 startLogging(mLogPath);
144 String timeStamp = new SimpleDateFormat(SIMPLE_DATE_FORMAT).format(Calendar.getInstance().getTime());
145
146 try {
147 mBuf = new BufferedWriter(new FileWriter(mLogFile, true));
148 mBuf.write(timeStamp + " -> " +text);
149 mBuf.newLine();
150 mBuf.close();
151 } catch (IOException e) {
152 e.printStackTrace();
153 }
154 }
155
156 public static String[] getLogFileNames() {
157 return mLogFileNames;
158 }
159
160 public static void setmLogFileNames(String[] logFileNames) {
161 Log_OC.mLogFileNames = logFileNames;
162 }
163 }