收藏 分销(赏)

android 将log保存到文本.doc

上传人:s4****5z 文档编号:8927806 上传时间:2025-03-08 格式:DOC 页数:5 大小:89.50KB
下载 相关 举报
android 将log保存到文本.doc_第1页
第1页 / 共5页
android 将log保存到文本.doc_第2页
第2页 / 共5页
点击查看更多>>
资源描述
1.先加上此类 [java] view plaincopy 1 package com.leader.hsj.utils; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.text.SimpleDateFormat; 10 import java.util.Date; 11 12 import android.content.Context; 13 import android.os.Environment; 14 15 /** 16 * log日志统计保存 17 * 18 * @author 陈伟斌 19 * 20 */ 21 22 public class LogcatHelper { 23 24 private static LogcatHelper INSTANCE = null; 25 private static String PATH_LOGCAT; 26 private LogDumper mLogDumper = null; 27 private int mPId; 28 private final static String LOG_FILE_DIR="leaderlog"; 29 /** 30 * 31 * 初始化目录 32 * 33 * */ 34 public void init(Context context) { 35 if (Environment.getExternalStorageState().equals( 36 Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中 37 PATH_LOGCAT = Environment.getExternalStorageDirectory() 38 .getAbsolutePath() + File.separator + LOG_FILE_DIR; 39 } else {// 如果SD卡不存在,就保存到本应用的目录下 40 PATH_LOGCAT = context.getFilesDir().getAbsolutePath() 41 + File.separator + LOG_FILE_DIR; 42 } 43 File file = new File(PATH_LOGCAT); 44 if (!file.exists()) { 45 file.mkdirs(); 46 } 47 } 48 49 public static LogcatHelper getInstance(Context context) { 50 if (INSTANCE == null) { 51 INSTANCE = new LogcatHelper(context); 52 } 53 return INSTANCE; 54 } 55 56 private LogcatHelper(Context context) { 57 init(context); 58 mPId = android.os.Process.myPid(); 59 } 60 61 public void start() { 62 if (mLogDumper == null) 63 mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT); 64 mLogDumper.start(); 65 } 66 67 public void stop() { 68 if (mLogDumper != null) { 69 mLogDumper.stopLogs(); 70 mLogDumper = null; 71 } 72 } 73 74 private class LogDumper extends Thread { 75 76 private Process logcatProc; 77 private BufferedReader mReader = null; 78 private boolean mRunning = true; 79 String cmds = null; 80 private String mPID; 81 private FileOutputStream out = null; 82 83 public LogDumper(String pid, String dir) { 84 mPID = pid; 85 try { 86 out = new FileOutputStream(new File(dir, "leader-" 87 + getFileName() + ".log")); 88 } catch (FileNotFoundException e) { 89 // TODO Auto-generated catch block 90 e.printStackTrace(); 91 } 92 93 /** 94 * 95 * 日志等级:*:v , *:d , *:w , *:e , *:f , *:s 96 * 97 * 显示当前mPID程序的 E和W等级的日志. 98 * 99 * */ 100 101 // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; 102 // cmds = "logcat | grep \"(" + mPID + ")\"";//打印所有日志信息 103 // cmds = "logcat -s way";//打印标签过滤信息 104 cmds = "logcat *:e *:i | grep \"(" + mPID + ")\""; 105 106 } 107 108 public void stopLogs() { 109 mRunning = false; 110 } 111 112 @Override 113 public void run() { 114 try { 115 logcatProc = Runtime.getRuntime().exec(cmds); 116 mReader = new BufferedReader(new InputStreamReader( 117 logcatProc.getInputStream()), 1024); 118 String line = null; 119 while (mRunning && (line = mReader.readLine()) != null) { 120 if (!mRunning) { 121 break; 122 } 123 if (line.length() == 0) { 124 continue; 125 } 126 if (out != null && line.contains(mPID)) { 127 out.write((getDateEN() + " " + line + "\n") 128 .getBytes()); 129 } 130 } 131 132 } catch (IOException e) { 133 e.printStackTrace(); 134 } finally { 135 if (logcatProc != null) { 136 logcatProc.destroy(); 137 logcatProc = null; 138 } 139 if (mReader != null) { 140 try { 141 mReader.close(); 142 mReader = null; 143 } catch (IOException e) { 144 e.printStackTrace(); 145 } 146 } 147 if (out != null) { 148 try { 149 out.close(); 150 } catch (IOException e) { 151 e.printStackTrace(); 152 } 153 out = null; 154 } 155 156 } 157 158 } 159 160 } 161 162 public static String getFileName() { 163 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 164 String date = format.format(new Date(System.currentTimeMillis())); 165 return date;// 2012年10月03日 23:41:31 166 } 167 168 public static String getDateEN() { 169 SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 170 String date1 = format1.format(new Date(System.currentTimeMillis())); 171 return date1;// 2012-10-03 23:41:31 172 } 173 174 } 2.加上权限 [html] view plaincopy 175 <uses-permission android:name="android.permission.READ_LOGS" /> 176 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 3.调用 [java] view plaincopy 177 LogcatHelper.getInstance(this).start(); // 将log保存到文件,便于调试,实际发布时请注释掉 使用 adb logcat -d > logcat.txt 这个命令也行 转载请声明 牛智网 (http://www.niuzhi.cc) 专业的手机游戏软件下载平台
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服