添加 'Android/Demo1/Logcat.java'

适用于安卓的日志收集,通过logcat来采集
This commit is contained in:
yutou 2022-11-12 15:21:38 +08:00
parent 199bd7ee3c
commit 4a6db058d4

148
Android/Demo1/Logcat.java Normal file
View File

@ -0,0 +1,148 @@
import static java.text.DateFormat.DEFAULT;
import android.content.Context;
import com.yunbao.common.CommonAppConfig;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
public class Logcat {
/**
* 采集所有日志
*/
public static void start(Context context) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String[] exec = new String[]{"logcat", "-c"};
Runtime.getRuntime().exec(exec).waitFor();
exec = new String[]{"logcat", "-v", "color", "-D"};
Process process = Runtime.getRuntime().exec(exec);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
PrintWriter writer = null;
String title = "[PDLIVE]" + CommonAppConfig.getInstance().getUid() + "_";
String today = title + SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date());
File dir = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator);
if (dir.listFiles() != null) {
for (File file : dir.listFiles()) {
if (!file.getName().endsWith(today + ".log") && !"error.log".equals(file.getName())) {
String fileName = file.getName().replace(title, "");
if (isDelLog(fileName)) {
file.delete();
}
}
}
}
File saveFile = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".log");
FileOutputStream os = new FileOutputStream(saveFile);
writer = new PrintWriter(os);
while ((line = bufferedReader.readLine()) != null) {
writer.append(line).write("\n");
}
writer.flush();
writer.close();
exec = new String[]{"logcat", "-c"};
Runtime.getRuntime().exec(exec).waitFor();
bufferedReader.close();
start(context);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 判断是否是要删除的日志
**/
private static boolean isDelLog(String time) {
try {
Calendar timeCal = Calendar.getInstance(Locale.CHINA);
timeCal.setTime(Objects.requireNonNull(SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).parse(time)));
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(new Date());
calendar.add(Calendar.DATE, -3);
return calendar.after(timeCal);
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
public static void shareFile(Context context) {
if (PermissionChecker.checkCallingOrSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
return;
}
String title = "[PDLIVE]" + CommonAppConfig.getInstance().getUid() + "_";
String today = title + SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date());
File dir = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator);
File zip = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".zip");
try {
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zip)));
FileInputStream fis = null;
WritableByteChannel writableByteChannel = Channels.newChannel(zos);
if (dir.listFiles() != null) {
for (File file : dir.listFiles()) {
if (file.getName().endsWith(".log") && !"error.log".equals(file.getName())) {
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
fis = new FileInputStream(file);
FileChannel channel = fis.getChannel();
channel.transferTo(0, file.length() - 1, writableByteChannel);
fis.close();
}
}
}
zos.close();
writableByteChannel.close();
} catch (Exception e) {
e.printStackTrace();
}
if (zip.exists()) {
Intent share = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", zip);
share.putExtra(Intent.EXTRA_STREAM, contentUri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(zip));
}
share.setType("application/vnd.ms-excel");
share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "分享文件"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
context.startActivity(intent);
}
}
File out = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + zip.getName());
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.copy(zip.toPath(), out.toPath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}