优化日志的处理

增加日志的时候不做行数处理,避免滚动条上下跳动
serial
BlueMatthew 1 year ago
parent aa8667a7c7
commit 7b3061c8b3

@ -57,8 +57,8 @@ public class MainActivity extends AppCompatActivity {
} }
public static final int MAX_LOG_LINES = 480; public static final int MAX_LOG_LINES = 480;
public static final int MIN_LOG_LINES = 120;
private int mLines = 0;
private ActivityMainBinding binding; private ActivityMainBinding binding;
private Handler mHandler = null; private Handler mHandler = null;
@ -141,27 +141,6 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void handleMessage(@NonNull Message msg) { public void handleMessage(@NonNull Message msg) {
switch (msg.what) { switch (msg.what) {
case MicroPhotoService.MSG_WHAT_LOG: {
String log = (String) msg.obj;
binding.logs.append(log);
// Reply
// Messenger clientMessenger = msg.replyTo;
// Message replyMessage = Message.obtain(null, 2);
// 设置回复消息中的数据
Bundle replyData = new Bundle();
replyData.putString("key", "reply value");
// replyMessage.setData(replyData);
/*
try {
clientMessenger.send(replyMessage);
} catch (RemoteException e) {
e.printStackTrace();
}
*/
}
break;
case MSG_WHAT_LOG_OBSERVER: case MSG_WHAT_LOG_OBSERVER:
{ {
byte[] bytes = (byte[])msg.obj; byte[] bytes = (byte[])msg.obj;
@ -170,35 +149,14 @@ public class MainActivity extends AppCompatActivity {
try { try {
log = new String(bytes, 0, bytesRead, "UTF-8"); log = new String(bytes, 0, bytesRead, "UTF-8");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} }
if (log != null) { if (log != null) {
long lines = log.chars().filter(ch -> ch == '\n').count();
mLines += lines;
if (mLines + lines > MAX_LOG_LINES) {
int excessLineNumber = MAX_LOG_LINES / 2;
int eolIndex = -1;
CharSequence charSequence = binding.logs.getText();
for(int i = 0; i < excessLineNumber; i++) {
do {
eolIndex++;
} while(eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n');
}
if (eolIndex < charSequence.length()) {
binding.logs.getEditableText().delete(0, eolIndex+1);
}
mLines -= excessLineNumber;
}
binding.logs.append(log); binding.logs.append(log);
// if (line > 9) {//超出屏幕自动滚动显示(9是当前页面显示的最大行数)
int offset = mLines * binding.logs.getLineHeight(); int offset = mLines * binding.logs.getLineHeight();
if (offset > binding.logs.getHeight()) { if (offset > binding.logs.getHeight()) {
binding.logs.scrollTo(0, offset - binding.logs.getHeight() + binding.logs.getLineHeight()); binding.logs.scrollTo(0, offset - binding.logs.getHeight() + binding.logs.getLineHeight());
} }
// }
} }
} }
break; break;
@ -567,12 +525,31 @@ public class MainActivity extends AppCompatActivity {
// call the superclass method first // call the superclass method first
super.onResume(); super.onResume();
try {
String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext()); String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext());
logFilePath += "logs/log.txt"; logFilePath += "logs/log.txt";
mLogFileObserver = new LogFileObserver(logFilePath); mLogFileObserver = new LogFileObserver(logFilePath);
mLogFileObserver.startWatching(); mLogFileObserver.startWatching();
Log.i(TAG, "Log Observer Started"); Log.i(TAG, "Log Observer Started");
int lines = binding.logs.getLineCount();
if (lines > MAX_LOG_LINES) {
int excessLineNumber = lines - MIN_LOG_LINES;
int eolIndex = -1;
CharSequence charSequence = binding.logs.getText();
for (int i = 0; i < excessLineNumber; i++) {
do {
eolIndex++;
} while (eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n');
}
if (eolIndex < charSequence.length()) {
binding.logs.getEditableText().delete(0, eolIndex + 1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} }
@Override @Override
@ -580,16 +557,19 @@ public class MainActivity extends AppCompatActivity {
// call the superclass method first // call the superclass method first
super.onPause(); super.onPause();
try {
if (mLogFileObserver != null) { if (mLogFileObserver != null) {
mLogFileObserver.stopWatching(); mLogFileObserver.stopWatching();
mLogFileObserver = null; mLogFileObserver = null;
Log.i(TAG, "Log Observer Stopped"); Log.i(TAG, "Log Observer Stopped");
} }
} catch (Exception e) {
e.printStackTrace();
}
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
} }

Loading…
Cancel
Save