Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions src/com/owncloud/android/lib/common/utils/Log_OC.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.owncloud.android.lib.common.utils;

import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
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 = "yyyy/MM/dd HH:mm:ss";
private static final String LOG_FOLDER_NAME = "log";
private static final long MAX_FILE_SIZE = 1000000; // 1MB

private static String mOwncloudDataFolderLog = "owncloud_log";
private static final String TAG = Log_OC.class.getSimpleName();

private static String mNextcloudDataFolderLog = "nextcloud_log";

private static File mLogFile;
private static File mFolder;
Expand All @@ -28,7 +29,7 @@ public class Log_OC {
private static boolean isEnabled = false;

public static void setLogDataFolder(String logFolder){
mOwncloudDataFolderLog = logFolder;
mNextcloudDataFolderLog = logFolder;
}

public static void i(String TAG, String message){
Expand Down Expand Up @@ -65,17 +66,17 @@ public static void w(String TAG, String message) {
}

public static void wtf(String TAG, String message) {
Log.wtf(TAG,message);
Log.wtf(TAG, message);
appendLog(TAG+" : "+ message);
}

/**
* Start doing logging
* @param logPath : path of log file
* @param storagePath : directory for keeping logs
*/
public static void startLogging() {
String logPath = Environment.getExternalStorageDirectory() + File.separator +
mOwncloudDataFolderLog + File.separator + LOG_FOLDER_NAME;
synchronized public static void startLogging(String storagePath) {
String logPath = storagePath + File.separator +
mNextcloudDataFolderLog + File.separator + LOG_FOLDER_NAME;
mFolder = new File(logPath);
mLogFile = new File(mFolder + File.separator + mLogFileNames[0]);

Expand All @@ -84,7 +85,7 @@ public static void startLogging() {
if (!mFolder.exists()) {
mFolder.mkdirs();
isFileCreated = true;
Log.d("LOG_OC", "Log file created");
Log.d(TAG, "Log file created");
}

try {
Expand All @@ -99,18 +100,41 @@ public static void startLogging() {
}

} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Log initialization failed", e);
} finally {
if(mBuf != null) {
try {
mBuf.close();
} catch(IOException e) {
e.printStackTrace();
Log.e(TAG, "Initialization finishing failed", e);
}
}
}
}

synchronized public static void stopLogging() {
try {
if (mBuf != null)
mBuf.close();
isEnabled = false;

mLogFile = null;
mFolder = null;
mBuf = null;
isMaxFileSizeReached = false;
isEnabled = false;

} catch (IOException e) {
// Because we are stopping logging, we only log to Android console.
Log.e(TAG, "Closing log file failed: ", e);
} catch (Exception e) {
// This catch should never fire because we do null check on mBuf.
// But just for the sake of stability let's log this odd situation.
// Because we are stopping logging, we only log to Android console.
Log.e(TAG, "Stopping logging failed: ", e);
}
}

/**
* Delete history logging
*/
Expand Down Expand Up @@ -141,7 +165,7 @@ private static void appendPhoneInfo() {
* Append to the log file the info passed
* @param text : text for adding to the log file
*/
private static void appendLog(String text) {
synchronized private static void appendLog(String text) {

if (isEnabled) {

Expand All @@ -168,12 +192,12 @@ private static void appendLog(String text) {
mBuf.write(text);
mBuf.newLine();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Writing to logfile failed", e);
} finally {
try {
mBuf.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Cleaning after logging failed", e);
}
}

Expand Down