76 lines
2.4 KiB
Java
76 lines
2.4 KiB
Java
package util;
|
|
|
|
import android.os.Environment;
|
|
import android.util.Log;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import java.text.DateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.Locale;
|
|
|
|
/**
|
|
* Created by JettenM on 14.10.2016.
|
|
*/
|
|
|
|
public class BeWoLog {
|
|
public static String LOGFILENAME = "BeWoLog.txt";
|
|
|
|
public static void writeToLogFile(String data) {
|
|
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
|
Log.e("BEWOLOG", "Kein externes Speichermedium vorhanden.");
|
|
return;
|
|
}
|
|
|
|
Date currentDate = Calendar.getInstance().getTime();
|
|
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS", Locale.GERMAN);
|
|
|
|
String dateString = dateFormat.format(currentDate);
|
|
|
|
File root = android.os.Environment.getExternalStorageDirectory();
|
|
|
|
File dir = new File(root.getAbsolutePath() + "/" + Util.LOGFILEDIRECTORYNAME);
|
|
dir.mkdirs();
|
|
|
|
File file = new File(dir, LOGFILENAME);
|
|
try {
|
|
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
|
|
PrintWriter printWriter = new PrintWriter(fileOutputStream);
|
|
|
|
printWriter.println(dateString + ": " + data + "\n");
|
|
|
|
printWriter.flush();
|
|
printWriter.close();
|
|
fileOutputStream.close();
|
|
} catch(IOException ioException) {
|
|
ioException.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static Boolean deleteLogFile() {
|
|
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
|
Log.e("BEWOLOG", "Kein externes Speichermedium vorhanden.");
|
|
return false;
|
|
}
|
|
|
|
File root = android.os.Environment.getExternalStorageDirectory();
|
|
File logFile = new File(root.getAbsolutePath() + "/" + Util.LOGFILEDIRECTORYNAME + "/" + LOGFILENAME);
|
|
|
|
return logFile.delete();
|
|
}
|
|
|
|
public static void writeExceptionToLog(Throwable ex) {
|
|
String logText = "Ausnahme: " + ex.toString() + "; Message: " + ex.getMessage() + "; Cause: " + ex.getCause();
|
|
for(StackTraceElement ste : ex.getStackTrace()) {
|
|
String steAsString = ste.toString();
|
|
logText += "\t" + steAsString;
|
|
}
|
|
|
|
writeToLogFile(logText);
|
|
}
|
|
}
|