X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/3e196f1f7dc64eb483df96ab3e950be32a5e718b..c8fe95b914f27bff1bcc696e12fc2f87dbfbe59c:/src/com/owncloud/android/utils/Log_OC.java diff --git a/src/com/owncloud/android/utils/Log_OC.java b/src/com/owncloud/android/utils/Log_OC.java index d958ffd6..de156b4d 100644 --- a/src/com/owncloud/android/utils/Log_OC.java +++ b/src/com/owncloud/android/utils/Log_OC.java @@ -6,28 +6,27 @@ import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; -import java.util.Date; -import java.util.Locale; import android.util.Log; -import com.owncloud.android.MainApp; +public class Log_OC { + 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; -public class Log_OC { - + private static String[] mLogFileNames = {"currentLog.txt", "olderLog.txt"}; + private static String mLogPath = FileStorageUtils.getLogPath(); + + private static boolean isMaxFileSizeReached = false; - private static boolean isEnabled = false; - private static File logFile; - private static File folder; - private static BufferedWriter buf; - public static void i(String TAG, String message){ - // Printing the message to LogCat console - int a = Log.i(TAG, message); + // Write the log message to the file - appendLog(TAG+" : "+message); + appendLog(TAG+" : "+ message); } public static void d(String TAG, String message){ @@ -62,53 +61,71 @@ public class Log_OC { Log.wtf(TAG,message); appendLog(TAG+" : "+ message); } - + + /** + * Start doing logging + * @param logPath : path of log file + */ public static void startLogging(String logPath) { - folder = new File(logPath); - logFile = new File(folder + File.separator + "log.txt"); + mFolder = new File(logPath); + mLogFile = new File(mFolder + File.separator + mLogFileNames[0]); boolean isFileCreated = false; - if (!folder.exists()) { - folder.mkdirs(); + if (!mFolder.exists()) { + mFolder.mkdirs(); isFileCreated = true; Log.d("LOG_OC", "Log file created"); } -// if (logFile.exists()) { -// logFile.delete(); -// } + try { - logFile.createNewFile(); - buf = new BufferedWriter(new FileWriter(logFile, true)); - isEnabled = true; + + if (isMaxFileSizeReached) { + + // Move current log file info to another file (old logs) + File olderFile = new File(mFolder + File.separator + mLogFileNames[1]); + if (mLogFile.exists()) { + mLogFile.renameTo(olderFile); + } + + // Construct a new file for current log info + mLogFile = new File(mFolder + File.separator + mLogFileNames[0]); + isMaxFileSizeReached = false; + } + + // Create the current log file if does not exist + mLogFile.createNewFile(); + mBuf = new BufferedWriter(new FileWriter(mLogFile, true)); if (isFileCreated) { appendPhoneInfo(); } - }catch (IOException e){ + + // Check if current log file size is bigger than the max file size defined + if (mLogFile.length() > MAX_FILE_SIZE) { + isMaxFileSizeReached = true; + } + } catch (IOException e) { e.printStackTrace(); } } - public static void stopLogging() { - SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()); -// String currentDateandTime = sdf.format(new Date()); - if (logFile != null) { -// logFile.renameTo(new File(folder + File.separator + MainApp.getLogName() + currentDateandTime+".log")); - - - isEnabled = false; - try { - buf = new BufferedWriter(new FileWriter(logFile, false)); - buf.append(""); - buf.close(); - } catch (IOException e) { - e.printStackTrace(); - } - + /** + * Delete history logging + */ + public static void deleteHistoryLogging() { + File folderLogs = new File(mFolder + File.separator); + if(folderLogs.isDirectory()){ + String[] myFiles = folderLogs.list(); + for (int i=0; i " +text); - buf.newLine(); - buf.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } -} + startLogging(mLogPath); + String timeStamp = new SimpleDateFormat(SIMPLE_DATE_FORMAT).format(Calendar.getInstance().getTime()); - - + try { + mBuf = new BufferedWriter(new FileWriter(mLogFile, true)); + 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; + } }