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
.util
.Log
;
14 private static final String SIMPLE_DATE_FORMAT
= "yyyy/MM/dd HH:mm:ss";
15 private static final long MAX_FILE_SIZE
= 10000;
17 private static File mLogFile
;
18 private static File mFolder
;
19 private static BufferedWriter mBuf
;
21 private static String
[] mLogFileNames
= {"currentLog.txt", "olderLog.txt"};
22 private static String mLogPath
= FileStorageUtils
.getLogPath();
24 private static boolean isMaxFileSizeReached
= false
;
26 public static void i(String TAG
, String message
){
28 // Write the log message to the file
29 appendLog(TAG
+" : "+ message
);
32 public static void d(String TAG
, String message
){
34 appendLog(TAG
+ " : " + message
);
36 public static void d(String TAG
, String message
, Exception e
) {
37 Log
.d(TAG
, message
, e
);
38 appendLog(TAG
+ " : " + message
+ " Exception : "+ e
.getStackTrace());
40 public static void e(String TAG
, String message
){
42 appendLog(TAG
+ " : " + message
);
45 public static void e(String TAG
, String message
, Throwable e
) {
46 Log
.e(TAG
, message
, e
);
47 appendLog(TAG
+" : " + message
+" Exception : " + e
.getStackTrace());
50 public static void v(String TAG
, String message
){
52 appendLog(TAG
+" : "+ message
);
55 public static void w(String TAG
, String message
) {
57 appendLog(TAG
+" : "+ message
);
60 public static void wtf(String TAG
, String message
) {
62 appendLog(TAG
+" : "+ message
);
67 * @param logPath : path of log file
69 public static void startLogging(String logPath
) {
70 mFolder
= new File(logPath
);
71 mLogFile
= new File(mFolder
+ File
.separator
+ mLogFileNames
[0]);
73 boolean isFileCreated
= false
;
75 if (!mFolder
.exists()) {
78 Log
.d("LOG_OC", "Log file created");
83 if (isMaxFileSizeReached
) {
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
);
91 // Construct a new file for current log info
92 mLogFile
= new File(mFolder
+ File
.separator
+ mLogFileNames
[0]);
93 isMaxFileSizeReached
= false
;
96 // Create the current log file if does not exist
97 mLogFile
.createNewFile();
98 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, true
));
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
;
107 } catch (IOException e
) {
113 * Delete history logging
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
]);
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 startLogging(mLogPath
);
144 String timeStamp
= new SimpleDateFormat(SIMPLE_DATE_FORMAT
).format(Calendar
.getInstance().getTime());
147 mBuf
= new BufferedWriter(new FileWriter(mLogFile
, true
));
148 mBuf
.write(timeStamp
+ " -> " +text
);
151 } catch (IOException e
) {
156 public static String
[] getLogFileNames() {
157 return mLogFileNames
;
160 public static void setmLogFileNames(String
[] logFileNames
) {
161 Log_OC
.mLogFileNames
= logFileNames
;