From: jabarros Date: Wed, 10 Sep 2014 16:17:49 +0000 (+0200) Subject: Manage size of log file automatically, handling two log files with a maximun size X-Git-Tag: oc-android-1.7.0_signed~176^2~12 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/9c65f99338b2ea9e4c09f323f1ce206da5131acb?ds=inline Manage size of log file automatically, handling two log files with a maximun size --- diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 59676c87..18f2355c 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -210,8 +210,6 @@ FileFragment.ContainerActivity, OnNavigationListener, OnSslUntrustedCertListener @Override protected void onDestroy() { - Log_OC.d(TAG, "onDestroy() FileDisplyActivty"); -// Log_OC.stopLogging(); super.onDestroy(); } diff --git a/src/com/owncloud/android/ui/activity/LogHistoryActivity.java b/src/com/owncloud/android/ui/activity/LogHistoryActivity.java index 02312223..775bec9a 100644 --- a/src/com/owncloud/android/ui/activity/LogHistoryActivity.java +++ b/src/com/owncloud/android/ui/activity/LogHistoryActivity.java @@ -41,12 +41,14 @@ import com.owncloud.android.utils.Log_OC; public class LogHistoryActivity extends SherlockActivity { - String mLogPath = FileStorageUtils.getLogPath(); private static final String MAIL_ATTACHMENT_TYPE = "plain/text"; - private static final String LOGGER_FILE_NAME = "log.txt"; + private static final String LOGGER_FILE_NAME = "currentLog.txt"; - File logDIR = null; + private static final String TAG = LogHistoryActivity.class.getSimpleName(); + + private String mLogPath = FileStorageUtils.getLogPath(); + private File logDIR = null; @Override @@ -88,7 +90,7 @@ public class LogHistoryActivity extends SherlockActivity { } } - + @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { super.onMenuItemSelected(featureId, item); @@ -149,7 +151,7 @@ public class LogHistoryActivity extends SherlockActivity { } } catch (IOException e) { - + Log_OC.d(TAG, e.getMessage().toString()); } TextView logTV = (TextView) findViewById(R.id.logTV); diff --git a/src/com/owncloud/android/utils/Log_OC.java b/src/com/owncloud/android/utils/Log_OC.java index fbd37fef..defae214 100644 --- a/src/com/owncloud/android/utils/Log_OC.java +++ b/src/com/owncloud/android/utils/Log_OC.java @@ -12,18 +12,20 @@ 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 File mLogFile; + private static File mFolder; + private static BufferedWriter mBuf; + + private static String[] mLogFileNames = {"currentLog.txt", "backupLog.txt"}; + 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){ @@ -59,43 +61,71 @@ public class Log_OC { 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"); } try { - logFile.createNewFile(); - buf = new BufferedWriter(new FileWriter(logFile, true)); - isEnabled = true; + + if (isMaxFileSizeReached) { + + // Move current log file info to another file + File secondLogFile = new File(mFolder + File.separator + mLogFileNames[1]); + if (mLogFile.exists()) { + mLogFile.renameTo(secondLogFile); + } + + // 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(); } } + /** + * Stop doing logging + */ public static void stopLogging() { - if (logFile != null) { - isEnabled = false; + if (mLogFile != null) { try { - buf = new BufferedWriter(new FileWriter(logFile, false)); - buf.append(""); - buf.close(); + mBuf = new BufferedWriter(new FileWriter(mLogFile, false)); + mBuf.append(""); + mBuf.close(); } catch (IOException e) { e.printStackTrace(); } } } + /** + * Append the info of the device + */ private static void appendPhoneInfo() { appendLog("Model : " + android.os.Build.MODEL); appendLog("Brand : " + android.os.Build.BRAND); @@ -105,28 +135,22 @@ public class Log_OC { appendLog("Version-Release : " + android.os.Build.VERSION.RELEASE); } + /** + * Append to the log file the info passed + * @param text : text for adding to the log file + */ private static void appendLog(String text) { - if (isEnabled) { - String logPath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log"; - startLogging(logPath); - String timeStamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()); + String logPath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log"; + startLogging(logPath); + String timeStamp = new SimpleDateFormat(SIMPLE_DATE_FORMAT).format(Calendar.getInstance().getTime()); - try { - buf = new BufferedWriter(new FileWriter(logFile, true)); - buf.write(timeStamp + " -> " +text); - buf.newLine(); - buf.close(); - } catch (IOException e) { - e.printStackTrace(); - } + try { + mBuf = new BufferedWriter(new FileWriter(mLogFile, true)); + mBuf.write(timeStamp + " -> " +text); + mBuf.newLine(); + mBuf.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } - - - - - - - - -}