Skip to content

Commit

Permalink
优化写入逻辑
Browse files Browse the repository at this point in the history
增加一些防御性逻辑
  • Loading branch information
tohodog committed Oct 27, 2021
1 parent e29a2f6 commit 562f419
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 43 deletions.
20 changes: 11 additions & 9 deletions app/src/main/java/com/qsinong/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ public boolean writeData(String folder, String fileName, byte[] bytes) throws Ex
public void onClick(View v) {
//测试性能把 init(...debug(false)) ,安卓自带的日志打印需要损耗性能

new Thread(new Runnable() {
@Override
public void run() {
long t = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
QLog.i("info日志info日志info日志info日志info日志info日志info日志info日志");
Log.e("子线程耗时", "" + (System.currentTimeMillis() - t));
for (int i = 0; i < 1; i++) {
new Thread(new Runnable() {
@Override
public void run() {
long t = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
QLog.i("info日志info日志info日志info日志info日志info日志info日志info日志");
Log.e("子线程耗时", "" + (System.currentTimeMillis() - t));
throw new RuntimeException("测试崩溃子线程");

}
}).start();
}
}).start();
}

long t = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
Expand Down
51 changes: 34 additions & 17 deletions app/src/main/java/com/qsinong/example/single/QLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void init(Application context) {

public static void init(final QLogConfig qLogConfig) {
INSTANCE.qLogConfig = qLogConfig;

ExecutorManager.execute(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -133,7 +134,13 @@ public void uncaughtException(Thread t, Throwable e) {
} catch (Throwable a) {
a.printStackTrace();
} finally {//崩溃事件继续流动,系统或其他程序
defaultUncaughtExceptionHandler.uncaughtException(t, e);
try {
if (defaultUncaughtExceptionHandler != null) {
defaultUncaughtExceptionHandler.uncaughtException(t, e);
}
} catch (Throwable a) {
a.printStackTrace();
}
}
}
});
Expand Down Expand Up @@ -194,7 +201,7 @@ private void log(Level level, String tag, String log) {

if (logInfo.qLogConfig.logFormat() != null) {
//自定义日记格式
logInfo.apply(logInfo.qLogConfig.logFormat().format(level, timeSSS, log, stact));
logInfo.apply(logInfo.qLogConfig.logFormat().format(level, timeSSS, log, stact) + "\n");
} else {
//先组装好一整条日志,不用sbuild了,自动优化
String sb = timeSSS +
Expand All @@ -215,10 +222,10 @@ private static class LogInfo {
private QLogConfig qLogConfig;
private String folder, fileName;

private ByteArrayOutputStream buff = new ByteArrayOutputStream();//日记写入缓存
private ByteArrayOutputStream buff = new ByteArrayOutputStream();//日记写入缓存,非线程安全
private volatile long lastWriteTime = System.currentTimeMillis();//最后一次写入时间

private volatile ScheduledFuture scheduledFuture;//可见性
private volatile ScheduledFuture scheduledFuture, scheduledFuture2;//可见性
private ReentrantLock reentrantLock = new ReentrantLock();

LogInfo(QLogConfig qLogConfig, String fileName) {
Expand Down Expand Up @@ -250,7 +257,7 @@ public void run() {
}
}

//日记写入缓存,线程安全,防止多线程日记乱了
//日记写入缓存,线程安全,防止多线程buff日记乱了
void write(String log) {
try {
reentrantLock.lock();
Expand All @@ -259,32 +266,38 @@ void write(String log) {
} catch (IOException e) {
e.printStackTrace();
}
cancel();
long space = System.currentTimeMillis() - lastWriteTime;
if (space > qLogConfig.delay() || buff.size() > qLogConfig.buffSize()) {
// ExecutorManager.execute(flushRun);
flushRun.run();
} else {
scheduledFuture = ExecutorManager.schedule(flushRun, qLogConfig.delay() - space);
if (space > qLogConfig.delay() || buff.size() > qLogConfig.buffSize()) {//触发立即写入
cancel();
// flushRun.run();
if (scheduledFuture2 == null) {//防止多个任务
scheduledFuture2 = ExecutorManager.schedule(flushRun, 0);
}
} else {//延时写入
if (scheduledFuture == null) {//防止多个任务
scheduledFuture = ExecutorManager.schedule(flushRun, qLogConfig.delay() - space);
}
}
} finally {
reentrantLock.unlock();
}
}

//取消上一个任务
private void cancel() {
//取消延时任务
private boolean cancel() {
boolean flag = true;
ScheduledFuture temp = scheduledFuture;
if (temp != null && !temp.isCancelled() && !temp.isDone())
temp.cancel(false);
if (temp != null && !temp.isCancelled() && !temp.isDone()) {
flag = temp.cancel(false);
}
scheduledFuture = null;
return flag;
}

private Runnable flushRun = new Runnable() {
@Override
public void run() {
flush();
lastWriteTime = System.currentTimeMillis();
}
};

Expand All @@ -303,13 +316,17 @@ void flush() {
else
buff.reset();
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
lastWriteTime = System.currentTimeMillis();
scheduledFuture = null;
scheduledFuture2 = null;
reentrantLock.unlock();
}
}
}


//=============================================内部类===========================================

public static class QLogConfig {
Expand Down
4 changes: 2 additions & 2 deletions qlog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 4
versionName "1.3"
versionCode 5
versionName "1.4"

}

Expand Down
5 changes: 5 additions & 0 deletions qlog/src/main/java/com/qsinong/qlog/ExecutorManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.qsinong.qlog;

import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand All @@ -19,6 +20,10 @@ public static void execute(Runnable runnable) {
SCHEDULED_EXECUTOR_SERVICE.execute(runnable);
}

public static Future<?> submit(Runnable runnable) {
return SCHEDULED_EXECUTOR_SERVICE.submit(runnable);
}

public static ScheduledFuture<?> schedule(Runnable runnable, long delay) {
return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, TimeUnit.MILLISECONDS);
}
Expand Down
48 changes: 33 additions & 15 deletions qlog/src/main/java/com/qsinong/qlog/QLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public static String getPath() {
return INSTANCE.qLogConfig == null ? "" : INSTANCE.qLogConfig.path();
}


private Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler;

private QLog() {
Expand All @@ -119,7 +120,13 @@ public void uncaughtException(Thread t, Throwable e) {
} catch (Throwable a) {
a.printStackTrace();
} finally {//崩溃事件继续流动,系统或其他程序
defaultUncaughtExceptionHandler.uncaughtException(t, e);
try {
if (defaultUncaughtExceptionHandler != null) {
defaultUncaughtExceptionHandler.uncaughtException(t, e);
}
} catch (Throwable a) {
a.printStackTrace();
}
}
}
});
Expand Down Expand Up @@ -201,10 +208,10 @@ private static class LogInfo {
private QLogConfig qLogConfig;
private String folder, fileName;

private ByteArrayOutputStream buff = new ByteArrayOutputStream();//日记写入缓存
private ByteArrayOutputStream buff = new ByteArrayOutputStream();//日记写入缓存,非线程安全
private volatile long lastWriteTime = System.currentTimeMillis();//最后一次写入时间

private volatile ScheduledFuture scheduledFuture;//可见性
private volatile ScheduledFuture scheduledFuture, scheduledFuture2;//可见性
private ReentrantLock reentrantLock = new ReentrantLock();

LogInfo(QLogConfig qLogConfig, String fileName) {
Expand Down Expand Up @@ -236,7 +243,7 @@ public void run() {
}
}

//日记写入缓存,线程安全,防止多线程日记乱了
//日记写入缓存,线程安全,防止多线程buff日记乱了
void write(String log) {
try {
reentrantLock.lock();
Expand All @@ -245,32 +252,38 @@ void write(String log) {
} catch (IOException e) {
e.printStackTrace();
}
cancel();
long space = System.currentTimeMillis() - lastWriteTime;
if (space > qLogConfig.delay() || buff.size() > qLogConfig.buffSize()) {
// ExecutorManager.execute(flushRun);
flushRun.run();
} else {
scheduledFuture = ExecutorManager.schedule(flushRun, qLogConfig.delay() - space);
if (space > qLogConfig.delay() || buff.size() > qLogConfig.buffSize()) {//触发立即写入
cancel();
// flushRun.run();
if (scheduledFuture2 == null) {//防止多个任务
scheduledFuture2 = ExecutorManager.schedule(flushRun, 0);
}
} else {//延时写入
if (scheduledFuture == null) {//防止多个任务
scheduledFuture = ExecutorManager.schedule(flushRun, qLogConfig.delay() - space);
}
}
} finally {
reentrantLock.unlock();
}
}

//取消上一个任务
private void cancel() {
//取消延时任务
private boolean cancel() {
boolean flag = true;
ScheduledFuture temp = scheduledFuture;
if (temp != null && !temp.isCancelled() && !temp.isDone())
temp.cancel(false);
if (temp != null && !temp.isCancelled() && !temp.isDone()) {
flag = temp.cancel(false);
}
scheduledFuture = null;
return flag;
}

private Runnable flushRun = new Runnable() {
@Override
public void run() {
flush();
lastWriteTime = System.currentTimeMillis();
}
};

Expand All @@ -289,7 +302,12 @@ void flush() {
else
buff.reset();
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
lastWriteTime = System.currentTimeMillis();
scheduledFuture = null;
scheduledFuture2 = null;
reentrantLock.unlock();
}
}
Expand Down

0 comments on commit 562f419

Please sign in to comment.