mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-16 08:05:34 -04:00
Fully implemented LoggerView with singleton Logger
This commit is contained in:
parent
46481cdcaa
commit
f0a8448ac2
@ -1,8 +1,11 @@
|
||||
package com.kdt;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Typeface;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.TextView;
|
||||
import android.widget.ToggleButton;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@ -16,7 +19,7 @@ import net.kdt.pojavlaunch.R;
|
||||
* It has support for the Logger class
|
||||
*/
|
||||
public class LoggerView extends ConstraintLayout {
|
||||
private Logger logger = null;
|
||||
private Logger.eventLogListener logListener;
|
||||
private TextView log;
|
||||
|
||||
|
||||
@ -29,29 +32,40 @@ public class LoggerView extends ConstraintLayout {
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inflate the layout, and add component behaviors
|
||||
*/
|
||||
private void init(){
|
||||
inflate(getContext(), R.layout.loggerview_layout, this);
|
||||
log = findViewById(R.id.content_log_view);
|
||||
log.setTypeface(Typeface.MONOSPACE);
|
||||
//TODO clamp the max text so it doesn't go oob
|
||||
log.setMaxLines(Integer.MAX_VALUE);
|
||||
log.setEllipsize(null);
|
||||
log.setVisibility(GONE);
|
||||
|
||||
// Toggle log visibility
|
||||
ToggleButton toggleButton = findViewById(R.id.content_log_toggle_log);
|
||||
toggleButton.setOnCheckedChangeListener(
|
||||
(compoundButton, isChecked) -> {
|
||||
log.setVisibility(isChecked ? VISIBLE : GONE);
|
||||
if(!isChecked) log.setText("");
|
||||
});
|
||||
toggleButton.setChecked(false);
|
||||
|
||||
// Remove the loggerView from the user View
|
||||
ImageButton cancelButton = findViewById(R.id.log_view_cancel);
|
||||
cancelButton.setOnClickListener(view -> LoggerView.this.setVisibility(GONE));
|
||||
|
||||
// Listen to logs
|
||||
logListener = text -> {
|
||||
if(log.getVisibility() != VISIBLE) return;
|
||||
post(() -> log.append(text));
|
||||
|
||||
};
|
||||
Logger.getInstance().setLogListener(logListener);
|
||||
}
|
||||
|
||||
/** Create the logger */
|
||||
public void setLogFileName(String fileName){
|
||||
if(logger != null){
|
||||
logger.shutdown();
|
||||
}
|
||||
logger = new Logger(fileName);
|
||||
}
|
||||
|
||||
/** Indirect Wrapper for the logger object */
|
||||
public void appendToLog(String text){
|
||||
log.append(text);
|
||||
logger.appendToLog(text);
|
||||
}
|
||||
|
||||
/** Indirect wrapper for the shutdown system */
|
||||
public void shutdown(){
|
||||
logger.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package net.kdt.pojavlaunch;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/** Singleton class made to log on one file
|
||||
* The singleton part can be removed but will require more implementation from the end-dev
|
||||
@ -13,13 +14,14 @@ public class Logger {
|
||||
/* Instance variables */
|
||||
private final File logFile;
|
||||
private PrintStream logStream;
|
||||
private WeakReference<eventLogListener> logListenerWeakReference = null;
|
||||
|
||||
/* No public construction */
|
||||
public Logger(){
|
||||
private Logger(){
|
||||
this("latestlog.txt");
|
||||
}
|
||||
|
||||
public Logger(String fileName){
|
||||
private Logger(String fileName){
|
||||
logFile = new File(Tools.ASSETS_PATH, fileName);
|
||||
// Make a new instance of the log file
|
||||
logFile.delete();
|
||||
@ -45,12 +47,13 @@ public class Logger {
|
||||
/** Print the text to the log file if not censored */
|
||||
public void appendToLog(String text){
|
||||
if(shouldCensorLog(text)) return;
|
||||
logStream.println(text);
|
||||
appendToLogUnchecked(text);
|
||||
}
|
||||
|
||||
/** Print the text to the log file, no china censoring there */
|
||||
public void appendToLogUnchecked(String text){
|
||||
logStream.println(text);
|
||||
notifyLogListener(text);
|
||||
}
|
||||
|
||||
/** Reset the log file, effectively erasing any previous logs */
|
||||
@ -72,8 +75,29 @@ public class Logger {
|
||||
* @param text The text to check
|
||||
* @return Whether the log should be censored
|
||||
*/
|
||||
public static boolean shouldCensorLog(String text){
|
||||
private static boolean shouldCensorLog(String text){
|
||||
if(text.contains("Session ID is")) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Small listener for anything listening to the log */
|
||||
public interface eventLogListener {
|
||||
void onEventLogged(String text);
|
||||
}
|
||||
|
||||
/** Link a log listener to the logger */
|
||||
public void setLogListener(eventLogListener logListener){
|
||||
this.logListenerWeakReference = new WeakReference<>(logListener);
|
||||
}
|
||||
|
||||
/** Notifies the event listener, if it exists */
|
||||
private void notifyLogListener(String text){
|
||||
if(logListenerWeakReference == null) return;
|
||||
eventLogListener logListener = logListenerWeakReference.get();
|
||||
if(logListener == null){
|
||||
logListenerWeakReference = null;
|
||||
return;
|
||||
}
|
||||
logListener.onEventLogged(text);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user