Skip to content

Commit

Permalink
feat: save debug logs to file
Browse files Browse the repository at this point in the history
  • Loading branch information
cafeed28 committed Feb 29, 2024
1 parent 071b3d0 commit 18bcc92
Show file tree
Hide file tree
Showing 10 changed files with 729 additions and 576 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId 'com.cafeed28.omori'
minSdk 26
targetSdk 34
versionCode 4
versionName '1.2.0'
versionCode 5
versionName '1.2.1'
}

buildTypes {
Expand Down
101 changes: 101 additions & 0 deletions app/src/main/java/com/cafeed28/omori/Debug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.cafeed28.omori;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.util.Date;

public class Debug {
private final String TAG = "omori";

private final DateFormat mDateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
private final String mInternalFileName;
private final String mExternalFileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/omori.log";
private final PrintWriter mPrintWriter;

private static Debug mInstance = null;

public static Debug i() {
return mInstance;
}

public Debug(String filesPath) throws IOException {
mInternalFileName = filesPath + "/debug.log";
var fileWriter = new FileWriter(mInternalFileName, true);
var bufferedWriter = new BufferedWriter(fileWriter);
mPrintWriter = new PrintWriter(bufferedWriter);
mPrintWriter.flush();
mInstance = this;
}

public void log(int level, String format, Object... args) {
String logLine;
try {
if (args.length > 0) logLine = String.format(format, args);
else logLine = format;
} catch (IllegalArgumentException e) {
logLine = format;
}

final String levelName;
switch (level) {
case Log.VERBOSE:
Log.v(TAG, logLine);
levelName = "VERBOSE";
break;
case Log.DEBUG:
Log.d(TAG, logLine);
levelName = "DEBUG";
break;
case Log.INFO:
Log.i(TAG, logLine);
levelName = "INFO";
break;
case Log.WARN:
Log.w(TAG, logLine);
levelName = "WARN";
break;
case Log.ERROR:
Log.e(TAG, logLine);
levelName = "ERROR";
break;
default:
throw new IllegalArgumentException();
}

String dateLine = String.format("[%s] %s: %s", mDateFormat.format(new Date()), levelName, logLine);
mPrintWriter.println(dateLine);
mPrintWriter.flush();
}

public void clear(Context context) {
try {
Files.deleteIfExists(Paths.get(mInternalFileName));
} catch (IOException e) {
Toast.makeText(context, "Failed to clear logs, check 'adb logcat' for more info", Toast.LENGTH_LONG).show();
this.log(Log.ERROR, "Failed to clear logs from '%s'", mInternalFileName);
e.printStackTrace();
}
}

public void save(Context context) {
try {
clear(context);
Files.copy(Paths.get(mInternalFileName), Paths.get(mExternalFileName));
Toast.makeText(context, String.format("Logs saved in %s", mExternalFileName), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(context, "Failed to save logs, check 'adb logcat' for more info", Toast.LENGTH_LONG).show();
this.log(Log.ERROR, "Failed to save logs to '%s'", mExternalFileName);
e.printStackTrace();
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/cafeed28/omori/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
new Debug(getFilesDir().getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Fatal error: failed to init logging", Toast.LENGTH_LONG).show();
finish();
}

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Expand Down
Loading

0 comments on commit 18bcc92

Please sign in to comment.