美文网首页
日志输出

日志输出

作者: 皓皓amous | 来源:发表于2023-11-19 17:41 被阅读0次
    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);
           }
       }
    
    }
    

    相关文章

      网友评论

          本文标题:日志输出

          本文链接:https://www.haomeiwen.com/subject/tdxmwdtx.html