日志输出

2023-11-19  本文已影响0人  皓皓amous
public class SaveLogUtils {
   private static volatile SaveLogUtils saveLogUtils = null;
   private static long lasterTime;

   private static Context context;
   private static String PATH = "testsavelog.txt";
   private static String savePath;
   private static File saveFileTxt;
   private static FileOutputStream fileOutputStream;
   private static ExecutorService executorService;
   private static long logPhoneInfoTime;

   public SaveLogUtils(Context context) {
       this.context = context;
   }

   public static SaveLogUtils getInstace(Context context) {
       if (saveLogUtils == null) {
           synchronized (SaveLogUtils.class) {
               saveLogUtils = new SaveLogUtils(context);
           }
       }
       return saveLogUtils;
   }

   public static void saveLog(String logStr) {
       if (TextUtils.isEmpty(logStr)) {
           return;
       }
       synchronized (SaveLogUtils.class) {
           if (executorService == null) {
               executorService = Executors.newSingleThreadExecutor();
           }
       }
       executorService.submit(() -> {
           // 创建新的文件
           createFile();
           lasterTime = System.currentTimeMillis();
           // 到了第二天生成新的日志文本
           if (!farmatDay(System.currentTimeMillis()).equals(farmatDay(lasterTime))) {
               lasterTime = System.currentTimeMillis();
               createFile();
           }
           try {
               fileOutputStream = new FileOutputStream(saveFileTxt);
               // 第一次和每隔15分钟打印一次手机所有信息
               if (logPhoneInfoTime == 0 || System.currentTimeMillis() - lasterTime > 5 * 60 * 1000) {
                   String model = Build.MODEL;
                   outStreamWrite(fileOutputStream, model, false);
                   String version = getVersion();
                   outStreamWrite(fileOutputStream, version, false);
                   logPhoneInfoTime = lasterTime;
               }
               outStreamWrite(fileOutputStream, logStr, true);
           } catch (FileNotFoundException e) {
               throw new RuntimeException(e);
           } finally {
               if (fileOutputStream != null) {
                   try {
                       fileOutputStream.flush();
                   } catch (IOException e) {
                       throw new RuntimeException(e);
                   }
                   try {
                       fileOutputStream.close();
                   } catch (IOException e) {
                       throw new RuntimeException(e);
                   }
               }
           }


       });
   }

   public static void shutdownThreadPool() {
       if (executorService != null) {
           executorService.shutdown();
       }
   }

   private static void createFile() {
       savePath = context.getFilesDir().getPath() + File.separator + "test" + File.separator + getLogTitle() + "_" + PATH;
       saveFileTxt = new File(savePath);
       if (!saveFileTxt.exists()) {
           saveFileTxt.mkdir();
       }
   }

   public static String getLogTitle() {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
       Date date = new Date(System.currentTimeMillis());
       String format = simpleDateFormat.format(date);
       return format;
   }

   public static String farmatDay(long time) {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
       Date date = new Date(time);
       String format = simpleDateFormat.format(date);
       return format;
   }

   public static String farmatLogTime(long time) {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_mm_ss");
       Date date = new Date(time);
       String format = simpleDateFormat.format(date);
       return format;
   }

   public static String getVersion() {
       try {
           PackageManager manager = context.getPackageManager();
           PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
           String version = info.versionName;
           return version;
       } catch (Exception e) {
           e.printStackTrace();
           return "无法获取到版本号";
       }
   }

   public static void outStreamWrite(FileOutputStream fileOutputStream, String str, boolean isLogTxt) {
       try {
           if (!isLogTxt) {
               fileOutputStream.write(str.getBytes());
               fileOutputStream.write("\\r\\n".getBytes());
           } else {
               fileOutputStream.write(farmatLogTime(System.currentTimeMillis()).getBytes());
               fileOutputStream.write("\\u0020".getBytes());
               fileOutputStream.write("\\u0020".getBytes());
               fileOutputStream.write(str.getBytes());
           }
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
   }

}
上一篇 下一篇

猜你喜欢

热点阅读