208 lines
6.7 KiB
Java
208 lines
6.7 KiB
Java
package com.alibaba.sdk.android.oss.common;
|
||
|
||
import android.content.Context;
|
||
import android.os.Build;
|
||
import android.os.Environment;
|
||
import android.os.StatFs;
|
||
import com.alibaba.sdk.android.oss.ClientConfiguration;
|
||
import java.io.File;
|
||
import java.io.FileWriter;
|
||
import java.io.IOException;
|
||
import java.io.PrintWriter;
|
||
import java.io.Writer;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Date;
|
||
|
||
/* loaded from: classes.dex */
|
||
public class OSSLogToFileUtils {
|
||
private static Context c;
|
||
private static OSSLogToFileUtils d;
|
||
private static File e;
|
||
private boolean a = true;
|
||
private static LogThreadPoolManager b = LogThreadPoolManager.b();
|
||
private static SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
private static long g = 5242880;
|
||
|
||
private static class WriteCall implements Runnable {
|
||
private Object a;
|
||
|
||
public WriteCall(Object obj) {
|
||
this.a = obj;
|
||
}
|
||
|
||
private PrintWriter a(PrintWriter printWriter) {
|
||
printWriter.println("crash_time:" + OSSLogToFileUtils.f.format(new Date()));
|
||
((Throwable) this.a).printStackTrace(printWriter);
|
||
return printWriter;
|
||
}
|
||
|
||
@Override // java.lang.Runnable
|
||
public void run() {
|
||
if (OSSLogToFileUtils.e != null) {
|
||
OSSLogToFileUtils.f();
|
||
if (OSSLogToFileUtils.c(OSSLogToFileUtils.e) > OSSLogToFileUtils.g) {
|
||
OSSLogToFileUtils.f().a();
|
||
}
|
||
try {
|
||
PrintWriter printWriter = new PrintWriter((Writer) new FileWriter(OSSLogToFileUtils.e, true), true);
|
||
if (this.a instanceof Throwable) {
|
||
a(printWriter);
|
||
} else {
|
||
printWriter.println(OSSLogToFileUtils.f().a((StackTraceElement[]) null) + " - " + this.a.toString());
|
||
}
|
||
printWriter.println("------>end of log");
|
||
printWriter.println();
|
||
printWriter.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private OSSLogToFileUtils() {
|
||
}
|
||
|
||
public static OSSLogToFileUtils f() {
|
||
if (d == null) {
|
||
synchronized (OSSLogToFileUtils.class) {
|
||
if (d == null) {
|
||
d = new OSSLogToFileUtils();
|
||
}
|
||
}
|
||
}
|
||
return d;
|
||
}
|
||
|
||
/* JADX INFO: Access modifiers changed from: private */
|
||
public File g() {
|
||
File file;
|
||
File file2 = null;
|
||
boolean z = false;
|
||
try {
|
||
boolean z2 = true;
|
||
if (this.a && Environment.getExternalStorageState().equals("mounted") && Build.VERSION.SDK_INT < 29) {
|
||
if (h() <= g / 1024) {
|
||
z2 = false;
|
||
}
|
||
file = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "OSSLog");
|
||
} else {
|
||
if (i() <= g / 1024) {
|
||
z2 = false;
|
||
}
|
||
file = new File(c.getFilesDir().getPath() + File.separator + "OSSLog");
|
||
}
|
||
z = z2;
|
||
} catch (Exception unused) {
|
||
file = null;
|
||
}
|
||
if (z) {
|
||
if (!file.exists()) {
|
||
file.mkdirs();
|
||
}
|
||
file2 = new File(file.getPath() + "/logs.csv");
|
||
if (!file2.exists()) {
|
||
a(file2);
|
||
}
|
||
}
|
||
return file2;
|
||
}
|
||
|
||
private long h() {
|
||
long j = 0;
|
||
if ("mounted".equals(Environment.getExternalStorageState())) {
|
||
try {
|
||
j = (Build.VERSION.SDK_INT >= 18 ? new StatFs(Environment.getExternalStorageDirectory().getPath()).getAvailableBlocksLong() : r3.getAvailableBlocks()) * r3.getBlockSize();
|
||
} catch (Exception unused) {
|
||
}
|
||
}
|
||
OSSLog.b("sd卡存储空间:" + String.valueOf(j) + "kb", false);
|
||
return j;
|
||
}
|
||
|
||
private long i() {
|
||
long j;
|
||
try {
|
||
j = ((Build.VERSION.SDK_INT >= 18 ? new StatFs(Environment.getDataDirectory().getPath()).getAvailableBlocksLong() : r1.getAvailableBlocks()) * r1.getBlockSize()) / 1024;
|
||
} catch (Exception unused) {
|
||
j = 0;
|
||
}
|
||
OSSLog.b("内部存储空间:" + String.valueOf(j) + "kb", false);
|
||
return j;
|
||
}
|
||
|
||
public static long c(File file) {
|
||
if (file == null || !file.exists()) {
|
||
return 0L;
|
||
}
|
||
return file.length();
|
||
}
|
||
|
||
public static void a(Context context, ClientConfiguration clientConfiguration) {
|
||
File file;
|
||
OSSLog.b("init ...", false);
|
||
if (clientConfiguration != null) {
|
||
g = clientConfiguration.g();
|
||
}
|
||
if (c != null && d != null && (file = e) != null && file.exists()) {
|
||
OSSLog.b("LogToFileUtils has been init ...", false);
|
||
return;
|
||
}
|
||
c = context.getApplicationContext();
|
||
d = f();
|
||
b.a(new Runnable() { // from class: com.alibaba.sdk.android.oss.common.OSSLogToFileUtils.1
|
||
@Override // java.lang.Runnable
|
||
public void run() {
|
||
File unused = OSSLogToFileUtils.e = OSSLogToFileUtils.d.g();
|
||
if (OSSLogToFileUtils.e != null) {
|
||
OSSLog.d("LogFilePath is: " + OSSLogToFileUtils.e.getPath(), false);
|
||
if (OSSLogToFileUtils.g < OSSLogToFileUtils.c(OSSLogToFileUtils.e)) {
|
||
OSSLog.d("init reset log file", false);
|
||
OSSLogToFileUtils.d.a();
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
public void a() {
|
||
OSSLog.b("Reset Log File ... ", false);
|
||
if (!e.getParentFile().exists()) {
|
||
OSSLog.b("Reset Log make File dir ... ", false);
|
||
e.getParentFile().mkdir();
|
||
}
|
||
File file = new File(e.getParent() + "/logs.csv");
|
||
if (file.exists()) {
|
||
file.delete();
|
||
}
|
||
a(file);
|
||
}
|
||
|
||
public void a(File file) {
|
||
try {
|
||
file.createNewFile();
|
||
} catch (Exception e2) {
|
||
OSSLog.c("Create log file failure !!! " + e2.toString(), false);
|
||
}
|
||
}
|
||
|
||
/* JADX INFO: Access modifiers changed from: private */
|
||
public String a(StackTraceElement[] stackTraceElementArr) {
|
||
if (stackTraceElementArr != null) {
|
||
return null;
|
||
}
|
||
return "[" + f.format(new Date()) + "]";
|
||
}
|
||
|
||
public synchronized void a(Object obj) {
|
||
if (OSSLog.b()) {
|
||
if (c != null && d != null && e != null) {
|
||
if (!e.exists()) {
|
||
a();
|
||
}
|
||
b.a(new WriteCall(obj));
|
||
}
|
||
}
|
||
}
|
||
}
|