-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- move classes in sub packages for a better structure - add a log view - add log handlers to write log files. The format is one json serialized `LogEntity` per line
- Loading branch information
1 parent
dc993a2
commit ccf3374
Showing
22 changed files
with
370 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
app/src/main/java/de/bentigorlich/mimic3ttsenginewrapper/activities/LogActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package de.bentigorlich.mimic3ttsenginewrapper.activities; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import android.view.MenuItem; | ||
import android.widget.LinearLayout; | ||
import android.widget.TextView; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
import de.bentigorlich.mimic3ttsenginewrapper.R; | ||
import de.bentigorlich.mimic3ttsenginewrapper.entities.LogEntity; | ||
|
||
public class LogActivity extends AppCompatActivity { | ||
Timer refreshLogInterval; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_log); | ||
|
||
Activity parent = this; | ||
refreshLogInterval = new Timer(); | ||
refreshLogInterval.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
parent.runOnUiThread(() -> populateLogView()); | ||
} | ||
}, 0, 10000); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
refreshLogInterval.cancel(); | ||
} | ||
|
||
private void populateLogView() { | ||
Gson gson = new Gson(); | ||
List<LogEntity> logs = new ArrayList<>(); | ||
for (File logFile : getLogFiles()) { | ||
try { | ||
BufferedReader br = new BufferedReader(new FileReader(logFile)); | ||
br.lines().forEach(line -> { | ||
LogEntity lr = gson.fromJson(line, LogEntity.class); | ||
logs.add(lr); | ||
}); | ||
br.close(); | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
|
||
logs.sort(Comparator.comparing(l -> l.Timestamp)); | ||
|
||
LinearLayout logView = findViewById(R.id.logContainer); | ||
logView.removeAllViews(); | ||
for (LogEntity curr : logs) { | ||
TextView t = new TextView(this); | ||
t.setText(curr.GetText(false)); | ||
t.setTextColor(getLevelColor(curr.Level)); | ||
t.setTextIsSelectable(true); | ||
logView.addView(t); | ||
} | ||
} | ||
|
||
private int getLevelColor(String level) { | ||
switch (level.toLowerCase()) { | ||
case "info": | ||
default: | ||
return getResources().getColor(com.google.android.material.R.color.design_default_color_surface, getTheme()); | ||
|
||
case "warning": | ||
return getResources().getColor(R.color.colorTextWarning, getTheme()); | ||
|
||
case "severe": | ||
return getResources().getColor(R.color.colorTextSevere, getTheme()); | ||
} | ||
} | ||
|
||
private List<File> getLogFiles() { | ||
File cacheDir = getDataDir(); | ||
List<File> logList = new ArrayList<>(); | ||
File[] fileList = cacheDir.listFiles(); | ||
for (File f: fileList) { | ||
String fileName = f.getName(); | ||
int pointIndex = fileName.lastIndexOf("."); | ||
if(f.exists() && pointIndex > 0) { | ||
String ext = fileName.substring(pointIndex); | ||
if(ext.equals(".log")) { | ||
logList.add(f); | ||
} | ||
} | ||
} | ||
return logList; | ||
} | ||
|
||
public void onMenuItemClick(@NonNull MenuItem menuItem) { | ||
if (menuItem.getItemId() == R.id.menu_logs_delete) { | ||
for(File f : getLogFiles()) { | ||
try { | ||
FileWriter fw = new FileWriter(f.getAbsolutePath(), false); | ||
fw.write(""); | ||
fw.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
populateLogView(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ch/mimic3ttsenginewrapper/CacheEntry.java → ...ttsenginewrapper/entities/CacheEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/java/de/bentigorlich/mimic3ttsenginewrapper/entities/LogEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package de.bentigorlich.mimic3ttsenginewrapper.entities; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.logging.LogRecord; | ||
|
||
public class LogEntity implements Cloneable { | ||
public String Message; | ||
public String Level; | ||
public String SourceClassName; | ||
public String SourceMethodName; | ||
public long Timestamp; | ||
public String LoggerName; | ||
|
||
public LogEntity(LogRecord record) { | ||
Message = record.getMessage(); | ||
Level = record.getLevel().toString(); | ||
SourceClassName = record.getSourceClassName(); | ||
SourceMethodName = record.getSourceMethodName(); | ||
Timestamp = record.getMillis(); | ||
LoggerName = record.getLoggerName(); | ||
} | ||
|
||
public LogEntity() {} | ||
|
||
@NonNull | ||
@Override | ||
public LogEntity clone() { | ||
LogEntity copy = new LogEntity(); | ||
copy.LoggerName = LoggerName; | ||
copy.Level = Level; | ||
copy.Timestamp = Timestamp; | ||
copy.Message = Message; | ||
copy.SourceClassName = SourceClassName; | ||
copy.SourceMethodName = SourceMethodName; | ||
return copy; | ||
} | ||
|
||
public String GetText(boolean includeSource) { | ||
Calendar c = Calendar.getInstance(); | ||
c.setTimeInMillis(Timestamp); | ||
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy|HH:mm:ss"); | ||
String source = ""; | ||
if(includeSource) { | ||
if (LoggerName != null && !LoggerName.equals("")) | ||
source += "[" + LoggerName + "]"; | ||
|
||
if (SourceClassName != null && !SourceClassName.equals("")) | ||
source += "[" + SourceClassName + "]"; | ||
|
||
if (SourceMethodName != null && !SourceMethodName.equals("")) | ||
source += "[" + SourceMethodName + "]"; | ||
} | ||
return "<" + formatter.format(c.getTime()) + " " + Level + ">" + source + " " + Message; | ||
} | ||
} |
Oops, something went wrong.