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