Commit 50ad9037 authored by Administrator's avatar Administrator

日志页面

parent 34974e57
......@@ -2,6 +2,7 @@ package com.virjar.echo.adr;
import android.app.Application;
import com.virjar.echo.adr.component.LogServices;
import com.virjar.echo.adr.repo.EchoConfig;
public class EchoApplication extends Application {
......@@ -9,6 +10,7 @@ public class EchoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LogServices.init();
EchoConfig.init(this);
}
}
package com.virjar.echo.adr.component;
import com.virjar.echo.nat.log.EchoLogger;
import com.virjar.echo.nat.log.ILogger;
import java.util.concurrent.atomic.AtomicLong;
public class LogServices {
private static final int bufferSize = 30;
private static String[] buffer;
private static AtomicLong index = new AtomicLong(0);
private static class EchoPrintLogger implements ILogger {
private ILogger delegate;
public EchoPrintLogger(ILogger delegate) {
this.delegate = delegate;
}
@Override
public void info(String msg) {
addLog(msg);
delegate.info(msg);
}
@Override
public void info(String msg, Throwable throwable) {
addLog(msg);
delegate.info(msg, throwable);
}
@Override
public void warn(String msg) {
addLog(msg);
delegate.warn(msg);
}
@Override
public void warn(String msg, Throwable throwable) {
addLog(msg);
delegate.warn(msg, throwable);
}
@Override
public void error(String msg) {
addLog(msg);
delegate.error(msg);
}
@Override
public void error(String msg, Throwable throwable) {
addLog(msg);
delegate.error(msg, throwable);
}
}
public static void init() {
buffer = new String[bufferSize];
ILogger logger = EchoLogger.getLogger();
EchoLogger.setLogger(new EchoPrintLogger(logger));
}
public static void addLog(String message) {
long insertIndex = index.getAndIncrement();
buffer[(int) (insertIndex % buffer.length)] = message;
}
public static StringBuffer logSnapshot() {
StringBuffer stringBuffer = new StringBuffer(20 * 32);
int readStartIndex = (int) (index.get() % buffer.length) + buffer.length - 1;
for (int i = 0; i < buffer.length; i++) {
int readIndex = (readStartIndex - i) % buffer.length;
String log = buffer[readIndex];
if (log == null) {
break;
}
stringBuffer.append(log).append("\n");
}
return stringBuffer;
}
}
package com.virjar.echo.adr.ui;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.virjar.echo.adr.R;
import com.virjar.echo.adr.component.LogServices;
public class LogFragment extends Fragment {
private String fragmentText;
public LogFragment(String fragmentText) {
this.fragmentText = fragmentText;
private TextView logView;
private boolean running = false;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_log, container, false);
logView = view.findViewById(R.id.tv_log);
logView.setText(LogServices.logSnapshot());
return view;
}
public void start() {
if (running) {
return;
}
running = true;
updateLog();
}
public void stop() {
running = false;
}
private void updateLog() {
if (logView != null) {
logView.setText(LogServices.logSnapshot());
}
if (!running) {
return;
}
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
updateLog();
}
}, 500);
}
}
......@@ -53,16 +53,20 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
FragmentTransaction fragmentTransaction = mfragmentManger.beginTransaction();//只能是局部变量,不能为全局变量,否则不能重复commit
//FragmentTransaction只能使用一次
hideAllFragment(fragmentTransaction);
if (R.id.main_log != view.getId() && logFragment != null) {
logFragment.stop();
}
switch (view.getId()) {
case R.id.main_log:
setAllFalse();
logLinear.setSelected(true);
if (logFragment == null) {
logFragment = new LogFragment("Log");
logFragment = new LogFragment();
fragmentTransaction.add(R.id.fragment_frame, logFragment);
} else {
fragmentTransaction.show(logFragment);
}
logFragment.start();
break;
case R.id.main_setting:
setAllFalse();
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.LogFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前出口:" />
<TextView
android:id="@+id/tv_mapping_proxy"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/tv_log"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment