Set maximum file size for logs and format logs info
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / Log_OC.java
index defae21..de156b4 100644 (file)
@@ -7,19 +7,20 @@ import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 
-import android.os.Environment;
 import android.util.Log;
 
 
 public class Log_OC {
-    private static final String SIMPLE_DATE_FORMAT = "HH:mm:ss";
-    private static final long MAX_FILE_SIZE = 10000;
+    private static final String SIMPLE_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
+    private static final long MAX_FILE_SIZE = 1000000; // 1MB
 
     private static File mLogFile;
     private static File mFolder;
     private static BufferedWriter mBuf;
 
-    private static String[] mLogFileNames = {"currentLog.txt", "backupLog.txt"};
+    private static String[] mLogFileNames = {"currentLog.txt", "olderLog.txt"};
+    private static String mLogPath =  FileStorageUtils.getLogPath();
+
     private static boolean isMaxFileSizeReached = false;
 
     public static void i(String TAG, String message){
@@ -60,7 +61,7 @@ public class Log_OC {
         Log.wtf(TAG,message); 
         appendLog(TAG+" : "+ message);
     }
-    
+
     /**
      * Start doing logging
      * @param logPath : path of log file
@@ -81,10 +82,10 @@ public class Log_OC {
 
             if (isMaxFileSizeReached) {
 
-                // Move current log file info to another file
-                File secondLogFile = new File(mFolder + File.separator + mLogFileNames[1]);
+                // Move current log file info to another file (old logs)
+                File olderFile = new File(mFolder + File.separator + mLogFileNames[1]);
                 if (mLogFile.exists()) {
-                    mLogFile.renameTo(secondLogFile);
+                    mLogFile.renameTo(olderFile);
                 }
 
                 // Construct a new file for current log info
@@ -109,17 +110,16 @@ public class Log_OC {
     }
     
     /**
-     * Stop doing logging
+     * Delete history logging
      */
-    public static void stopLogging() {
-        if (mLogFile != null) {
-            try {
-                mBuf = new BufferedWriter(new FileWriter(mLogFile, false));
-                mBuf.append("");
-                mBuf.close();
-            } catch (IOException e) {
-                e.printStackTrace();
-            } 
+    public static void deleteHistoryLogging() {
+        File folderLogs = new File(mFolder + File.separator);
+        if(folderLogs.isDirectory()){
+            String[] myFiles = folderLogs.list();
+            for (int i=0; i<myFiles.length; i++) {
+                File myFile = new File(folderLogs, myFiles[i]);
+                myFile.delete();
+            }
         }
     }
     
@@ -140,17 +140,27 @@ public class Log_OC {
      * @param text : text for adding to the log file
      */
     private static void appendLog(String text) { 
-        String logPath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
-        startLogging(logPath);
+        startLogging(mLogPath);
         String timeStamp = new SimpleDateFormat(SIMPLE_DATE_FORMAT).format(Calendar.getInstance().getTime());
 
         try {
            mBuf = new BufferedWriter(new FileWriter(mLogFile, true));
-           mBuf.write(timeStamp + " -> " +text);
+           mBuf.newLine();
+           mBuf.write(timeStamp);
+           mBuf.newLine();
+           mBuf.write(text);
            mBuf.newLine();
            mBuf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
+
+    public static String[] getLogFileNames() {
+        return mLogFileNames;
+    }
+
+    public static void setmLogFileNames(String[] logFileNames) {
+        Log_OC.mLogFileNames = logFileNames;
+    }
 }