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", "backupLog.txt"};
23 private static boolean isMaxFileSizeReached
= false
;
25 public static void i(String TAG
, String message
){
27 // Write the log message to the file
28 appendLog(TAG
+" : "+ message
);
31 public static void d(String TAG
, String message
){
33 appendLog(TAG
+ " : " + message
);
35 public static void d(String TAG
, String message
, Exception e
) {
36 Log
.d(TAG
, message
, e
);
37 appendLog(TAG
+ " : " + message
+ " Exception : "+ e
.getStackTrace());
39 public static void e(String TAG
, String message
){
41 appendLog(TAG
+ " : " + message
);
44 public static void e(String TAG
, String message
, Throwable e
) {
45 Log
.e(TAG
, message
, e
);
46 appendLog(TAG
+" : " + message
+" Exception : " + e
.getStackTrace());
49 public static void v(String TAG
, String message
){
51 appendLog(TAG
+" : "+ message
);
54 public static void w(String TAG
, String message
) {
56 appendLog(TAG
+" : "+ message
);
59 public static void wtf(String TAG
, String message
) {
61 appendLog(TAG
+" : "+ message
);
66 * @param logPath : path of log file
68 public static void startLogging(String logPath
) {
69 mFolder
= new File(logPath
);
70 mLogFile
= new File(mFolder
+ File
.separator
+ mLogFileNames
[0]);
72 boolean isFileCreated
= false
;
74 if (!mFolder
.exists()) {
77 Log
.d("LOG_OC", "Log file created");
82 if (isMaxFileSizeReached
) {
84 // Move current log file info to another file
85 File secondLogFile
= new File(mFolder
+ File
.separator
+ mLogFileNames
[1]);
86 if (mLogFile
.exists()) {
87 mLogFile
.renameTo(secondLogFile
);
90 // Construct a new file for current log info
91 mLogFile
= new File(mFolder
+ File
.separator
+ mLogFileNames
[0]);
92 isMaxFileSizeReached
= false
;
95 // Create the current log file if does not exist
96 mLogFile
.createNewFile();
97 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, true
));
102 // Check if current log file size is bigger than the max file size defined
103 if (mLogFile
.length() > MAX_FILE_SIZE
) {
104 isMaxFileSizeReached
= true
;
106 } catch (IOException e
) {
114 public static void stopLogging() {
115 if (mLogFile
!= null
) {
117 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, false
));
120 } catch (IOException e
) {
127 * Append the info of the device
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
);
139 * Append to the log file the info passed
140 * @param text : text for adding to the log file
142 private static void appendLog(String text
) {
143 String logPath
= Environment
.getExternalStorageDirectory()+File
.separator
+"owncloud"+File
.separator
+"log";
144 startLogging(logPath
);
145 String timeStamp
= new SimpleDateFormat(SIMPLE_DATE_FORMAT
).format(Calendar
.getInstance().getTime());
148 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, true
));
149 mBuf
.write(timeStamp
+ " -> " +text
);
152 } catch (IOException e
) {