1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
| public class LL {
public static abstract class Level { public static final String D = "D"; public static final String W = "W"; public static final String E = "E"; }
private static String defTag = "App";
private static boolean showLogcat = true; private static boolean writeFile = true;
private static String logFileDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "rust" + File.separator + "logs";
private static String fileName;
private static List<LogListener> listenerList = new ArrayList<>();
private static HandlerThread handlerThread; private static Handler writerHandler;
private static final int LOG_FILE_GROW_SIZE = 1024 * 10; private static long gCurrentLogPos = 0;
public static void prepare(Context context, @NonNull String fileDir, String logFilePrefix) { gCurrentLogPos = 0; if (TextUtils.isEmpty(fileDir)) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { logFileDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "rust" + File.separator + "logs"; } else { logFileDir = context.getFilesDir().getAbsolutePath() + File.separator + "rust" + File.separator + "logs"; } } else { logFileDir = fileDir; } if (null == handlerThread) { handlerThread = new HandlerThread("LL"); handlerThread.start(); } writerHandler = new Handler(handlerThread.getLooper()); fileName = logFilePrefix + "_" + System.currentTimeMillis() + ".txt"; Log.d(defTag, "[prepare] file: " + fileName); }
public static String getLogFileDir() { return logFileDir; }
public static String getFileName() { return fileName; }
public static void quit() { if (writerHandler != null) { writerHandler.removeCallbacksAndMessages(null); } if (handlerThread != null) { handlerThread.quit(); } }
public static void setDefTag(String t) { LL.defTag = t; }
public static void setWriteFile(boolean w) { LL.writeFile = w; }
public static void d(String content) { d(defTag, content, writeFile); }
public static void d(String tag, String content) { d(tag, content, writeFile); }
public static void dn(String content) { d(defTag, content, false); }
public static void dn(String tag, String content) { d(tag, content, false); }
public static void d(String tag, String content, boolean write) { if (showLogcat) { Log.d(tag, content); } tellLog(Level.D, tag, content); if (write) { if (writerHandler != null) { writerHandler.post(new WriteRunnable(tag, content)); } } }
public static void w(String content) { w(defTag, content, writeFile); }
public static void w(String tag, String content) { w(tag, content, writeFile); }
public static void wn(String content) { w(defTag, content, false); }
public static void wn(String tag, String content) { w(tag, content, false); }
public static void w(String tag, String content, boolean write) { if (showLogcat) { Log.w(tag, content); } tellLog(Level.W, tag, content); if (write) { if (writerHandler != null) { writerHandler.post(new WriteRunnable(tag, content)); } } }
public static void e(String content) { e(defTag, content); }
public static void e(String tag, String content) { e(tag, content, writeFile); }
public static void e(String tag, Exception e) { e(tag, e.getMessage(), writeFile); }
public static void en(String tag, String content) { e(tag, content, false); }
public static void e(String tag, String content, boolean write) { if (showLogcat) { Log.e(tag, content); } tellLog(Level.E, tag, content); if (write) { if (writerHandler != null) { writerHandler.post(new WriteRunnable(tag, content)); } } }
private static void tellLog(String level, String tag, String content) { if (null != listenerList) { for (LogListener l : listenerList) { l.onLog(level, tag, content); } } }
public static void addListener(LogListener l) { if (null == listenerList) { listenerList = new ArrayList<>(); } listenerList.add(l); }
public static void removeListener(LogListener l) { if (null != listenerList) { listenerList.remove(l); } }
static class WriteRunnable implements Runnable { String mmTag; String mmContent;
WriteRunnable(String tag, String content) { this.mmTag = tag; this.mmContent = content; }
@Override public void run() { SimpleDateFormat logTimeFormat = new SimpleDateFormat("HH:mm:ss.SSS", Locale.CHINA); String logContent = logTimeFormat.format(new Date()) + " [" + mmTag + "] " + mmContent + "\r\n"; try { File dir = new File(logFileDir); if (!dir.exists()) { boolean mk = dir.mkdirs(); Log.d(defTag, "make dir " + mk); } File eFile = new File(logFileDir + File.separator + fileName); byte[] strBytes = logContent.getBytes(); try { RandomAccessFile randomAccessFile = new RandomAccessFile(eFile, "rw"); MappedByteBuffer mappedByteBuffer; final int inputLen = strBytes.length; if (!eFile.exists()) { boolean nf = eFile.createNewFile(); Log.d(defTag, "new log file " + nf); mappedByteBuffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, gCurrentLogPos, LOG_FILE_GROW_SIZE); } else { mappedByteBuffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, gCurrentLogPos, inputLen); } if (mappedByteBuffer.remaining() < inputLen) { mappedByteBuffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, gCurrentLogPos, LOG_FILE_GROW_SIZE + inputLen);
}
mappedByteBuffer.put(strBytes); gCurrentLogPos += inputLen; } catch (Exception e) { Log.e(defTag, "WriteRunnable run: ", e); if (!eFile.exists()) { boolean nf = eFile.createNewFile(); Log.d(defTag, "new log file " + nf); } FileOutputStream os = new FileOutputStream(eFile, true); os.write(logContent.getBytes()); os.flush(); os.close(); } } catch (Exception e) { e.printStackTrace(); Log.e(defTag, "写log文件出错: ", e); } } }
}
|