1 package com
.owncloud
.android
.utils
;
3 import java
.io
.BufferedWriter
;
5 import java
.io
.FileWriter
;
6 import java
.io
.IOException
;
7 import java
.text
.SimpleDateFormat
;
8 import java
.util
.Calendar
;
10 import android
.os
.Environment
;
11 import android
.util
.Log
;
15 private static final String SIMPLE_DATE_FORMAT
= "HH:mm:ss";
16 private static final long MAX_FILE_SIZE
= 10000;
18 private static File mLogFile
;
19 private static File mFolder
;
20 private static BufferedWriter mBuf
;
22 private static String
[] mLogFileNames
= {"currentLog.txt", "olderLog.txt"};
23 private static String mLogPath
= FileStorageUtils
.getLogPath();
25 private static boolean isMaxFileSizeReached
= false
;
27 public static void i(String TAG
, String message
){
29 // Write the log message to the file
30 appendLog(TAG
+" : "+ message
);
33 public static void d(String TAG
, String message
){
35 appendLog(TAG
+ " : " + message
);
37 public static void d(String TAG
, String message
, Exception e
) {
38 Log
.d(TAG
, message
, e
);
39 appendLog(TAG
+ " : " + message
+ " Exception : "+ e
.getStackTrace());
41 public static void e(String TAG
, String message
){
43 appendLog(TAG
+ " : " + message
);
46 public static void e(String TAG
, String message
, Throwable e
) {
47 Log
.e(TAG
, message
, e
);
48 appendLog(TAG
+" : " + message
+" Exception : " + e
.getStackTrace());
51 public static void v(String TAG
, String message
){
53 appendLog(TAG
+" : "+ message
);
56 public static void w(String TAG
, String message
) {
58 appendLog(TAG
+" : "+ message
);
61 public static void wtf(String TAG
, String message
) {
63 appendLog(TAG
+" : "+ message
);
68 * @param logPath : path of log file
70 public static void startLogging(String logPath
) {
71 mFolder
= new File(logPath
);
72 mLogFile
= new File(mFolder
+ File
.separator
+ mLogFileNames
[0]);
74 boolean isFileCreated
= false
;
76 if (!mFolder
.exists()) {
79 Log
.d("LOG_OC", "Log file created");
84 if (isMaxFileSizeReached
) {
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
);
92 // Construct a new file for current log info
93 mLogFile
= new File(mFolder
+ File
.separator
+ mLogFileNames
[0]);
94 isMaxFileSizeReached
= false
;
97 // Create the current log file if does not exist
98 mLogFile
.createNewFile();
99 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, true
));
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
;
108 } catch (IOException e
) {
116 public static void stopLogging() {
117 if (mLogFile
!= null
) {
119 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, false
));
122 } catch (IOException e
) {
129 * Append the info of the device
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
);
141 * Append to the log file the info passed
142 * @param text : text for adding to the log file
144 private static void appendLog(String text
) {
145 startLogging(mLogPath
);
146 String timeStamp
= new SimpleDateFormat(SIMPLE_DATE_FORMAT
).format(Calendar
.getInstance().getTime());
149 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, true
));
150 mBuf
.write(timeStamp
+ " -> " +text
);
153 } catch (IOException e
) {
158 public static String
[] getLogFileNames() {
159 return mLogFileNames
;
162 public static void setmLogFileNames(String
[] logFileNames
) {
163 Log_OC
.mLogFileNames
= logFileNames
;