mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-16 16:16:04 -04:00
Fully implemented LoggerView with singleton Logger
This commit is contained in:
parent
46481cdcaa
commit
f0a8448ac2
@ -1,8 +1,11 @@
|
|||||||
package com.kdt;
|
package com.kdt;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Typeface;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.widget.ImageButton;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.ToggleButton;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
@ -16,7 +19,7 @@ import net.kdt.pojavlaunch.R;
|
|||||||
* It has support for the Logger class
|
* It has support for the Logger class
|
||||||
*/
|
*/
|
||||||
public class LoggerView extends ConstraintLayout {
|
public class LoggerView extends ConstraintLayout {
|
||||||
private Logger logger = null;
|
private Logger.eventLogListener logListener;
|
||||||
private TextView log;
|
private TextView log;
|
||||||
|
|
||||||
|
|
||||||
@ -29,29 +32,40 @@ public class LoggerView extends ConstraintLayout {
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inflate the layout, and add component behaviors
|
||||||
|
*/
|
||||||
private void init(){
|
private void init(){
|
||||||
inflate(getContext(), R.layout.loggerview_layout, this);
|
inflate(getContext(), R.layout.loggerview_layout, this);
|
||||||
log = findViewById(R.id.content_log_view);
|
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
/** Singleton class made to log on one file
|
/** Singleton class made to log on one file
|
||||||
* The singleton part can be removed but will require more implementation from the end-dev
|
* The singleton part can be removed but will require more implementation from the end-dev
|
||||||
@ -13,13 +14,14 @@ public class Logger {
|
|||||||
/* Instance variables */
|
/* Instance variables */
|
||||||
private final File logFile;
|
private final File logFile;
|
||||||
private PrintStream logStream;
|
private PrintStream logStream;
|
||||||
|
private WeakReference<eventLogListener> logListenerWeakReference = null;
|
||||||
|
|
||||||
/* No public construction */
|
/* No public construction */
|
||||||
public Logger(){
|
private Logger(){
|
||||||
this("latestlog.txt");
|
this("latestlog.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Logger(String fileName){
|
private Logger(String fileName){
|
||||||
logFile = new File(Tools.ASSETS_PATH, fileName);
|
logFile = new File(Tools.ASSETS_PATH, fileName);
|
||||||
// Make a new instance of the log file
|
// Make a new instance of the log file
|
||||||
logFile.delete();
|
logFile.delete();
|
||||||
@ -45,12 +47,13 @@ public class Logger {
|
|||||||
/** Print the text to the log file if not censored */
|
/** Print the text to the log file if not censored */
|
||||||
public void appendToLog(String text){
|
public void appendToLog(String text){
|
||||||
if(shouldCensorLog(text)) return;
|
if(shouldCensorLog(text)) return;
|
||||||
logStream.println(text);
|
appendToLogUnchecked(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Print the text to the log file, no china censoring there */
|
/** Print the text to the log file, no china censoring there */
|
||||||
public void appendToLogUnchecked(String text){
|
public void appendToLogUnchecked(String text){
|
||||||
logStream.println(text);
|
logStream.println(text);
|
||||||
|
notifyLogListener(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Reset the log file, effectively erasing any previous logs */
|
/** Reset the log file, effectively erasing any previous logs */
|
||||||
@ -72,8 +75,29 @@ public class Logger {
|
|||||||
* @param text The text to check
|
* @param text The text to check
|
||||||
* @return Whether the log should be censored
|
* @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;
|
if(text.contains("Session ID is")) return true;
|
||||||
return false;
|
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