Rewrite 'log' to Kotlin
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package com.github.gotify.log;
|
||||
|
||||
import android.content.Context;
|
||||
import com.hypertrack.hyperlog.LogFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Format extends LogFormat {
|
||||
Format(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormattedLogMessage(
|
||||
String logLevelName,
|
||||
String tag,
|
||||
String message,
|
||||
String timeStamp,
|
||||
String senderName,
|
||||
String osVersion,
|
||||
String deviceUUID) {
|
||||
return String.format(Locale.ENGLISH, "%s %s: %s", timeStamp, logLevelName, message);
|
||||
}
|
||||
}
|
||||
17
app/src/main/java/com/github/gotify/log/Format.kt
Normal file
17
app/src/main/java/com/github/gotify/log/Format.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import android.content.Context
|
||||
import com.hypertrack.hyperlog.LogFormat
|
||||
import java.util.*
|
||||
|
||||
internal class Format(context: Context) : LogFormat(context) {
|
||||
override fun getFormattedLogMessage(
|
||||
logLevelName: String,
|
||||
tag: String,
|
||||
message: String,
|
||||
timeStamp: String,
|
||||
senderName: String,
|
||||
osVersion: String,
|
||||
deviceUuid: String
|
||||
) = String.format(Locale.ENGLISH, "%s %s: %s", timeStamp, logLevelName, message)
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.github.gotify.log;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import com.hypertrack.hyperlog.HyperLog;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Log {
|
||||
private static String TAG = "gotify";
|
||||
|
||||
public static void init(Context content) {
|
||||
HyperLog.initialize(content, new Format(content));
|
||||
HyperLog.setLogLevel(android.util.Log.INFO); // TODO configurable
|
||||
}
|
||||
|
||||
public static String get() {
|
||||
List<String> logs = HyperLog.getDeviceLogsAsStringList(false);
|
||||
Collections.reverse(logs);
|
||||
return TextUtils.join("\n", logs.subList(0, Math.min(200, logs.size())));
|
||||
}
|
||||
|
||||
public static void e(String message) {
|
||||
HyperLog.e(TAG, message);
|
||||
}
|
||||
|
||||
public static void e(String message, Throwable e) {
|
||||
HyperLog.e(TAG, message + '\n' + android.util.Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
public static void i(String message) {
|
||||
HyperLog.i(TAG, message);
|
||||
}
|
||||
|
||||
public static void i(String message, Throwable e) {
|
||||
HyperLog.i(TAG, message + '\n' + android.util.Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
public static void w(String message) {
|
||||
HyperLog.w(TAG, message);
|
||||
}
|
||||
|
||||
public static void w(String message, Throwable e) {
|
||||
HyperLog.w(TAG, message + '\n' + android.util.Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
HyperLog.deleteLogs();
|
||||
}
|
||||
}
|
||||
48
app/src/main/java/com/github/gotify/log/Log.kt
Normal file
48
app/src/main/java/com/github/gotify/log/Log.kt
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.hypertrack.hyperlog.HyperLog
|
||||
|
||||
internal object Log {
|
||||
private const val TAG = "gotify"
|
||||
|
||||
fun init(content: Context) {
|
||||
HyperLog.initialize(content, Format(content))
|
||||
HyperLog.setLogLevel(Log.INFO) // TODO configurable
|
||||
}
|
||||
|
||||
fun get(): String {
|
||||
val logs = HyperLog.getDeviceLogsAsStringList(false)
|
||||
logs.reverse()
|
||||
return logs.subList(0, 200.coerceAtMost(logs.size)).joinToString("\n")
|
||||
}
|
||||
|
||||
fun e(message: String) {
|
||||
HyperLog.e(TAG, message)
|
||||
}
|
||||
|
||||
fun e(message: String, e: Throwable) {
|
||||
HyperLog.e(TAG, "$message\n${Log.getStackTraceString(e)}")
|
||||
}
|
||||
|
||||
fun i(message: String) {
|
||||
HyperLog.i(TAG, message)
|
||||
}
|
||||
|
||||
fun i(message: String, e: Throwable) {
|
||||
HyperLog.i(TAG, "$message\n${Log.getStackTraceString(e)}")
|
||||
}
|
||||
|
||||
fun w(message: String) {
|
||||
HyperLog.w(TAG, message)
|
||||
}
|
||||
|
||||
fun w(message: String, e: Throwable) {
|
||||
HyperLog.w(TAG, "$message\n${Log.getStackTraceString(e)}")
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
HyperLog.deleteLogs()
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package com.github.gotify.log;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.TextView;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import com.github.gotify.R;
|
||||
import com.github.gotify.Utils;
|
||||
import com.github.gotify.databinding.ActivityLogsBinding;
|
||||
|
||||
public class LogsActivity extends AppCompatActivity {
|
||||
|
||||
private ActivityLogsBinding binding;
|
||||
private Handler handler = new Handler();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
binding = ActivityLogsBinding.inflate(getLayoutInflater());
|
||||
setContentView(binding.getRoot());
|
||||
Log.i("Entering " + getClass().getSimpleName());
|
||||
updateLogs();
|
||||
setSupportActionBar(binding.appBarDrawer.toolbar);
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
actionBar.setDisplayShowCustomEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateLogs() {
|
||||
new RefreshLogs().execute();
|
||||
if (!isDestroyed()) {
|
||||
handler.postDelayed(this::updateLogs, 5000);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.logs_action, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
finish();
|
||||
}
|
||||
if (item.getItemId() == R.id.action_delete_logs) {
|
||||
Log.clear();
|
||||
}
|
||||
if (item.getItemId() == R.id.action_copy_logs) {
|
||||
TextView content = binding.logContent;
|
||||
ClipboardManager clipboardManager =
|
||||
(ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
ClipData clipData = ClipData.newPlainText("GotifyLog", content.getText().toString());
|
||||
clipboardManager.setPrimaryClip(clipData);
|
||||
Utils.showSnackBar(this, getString(R.string.logs_copied));
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
class RefreshLogs extends AsyncTask<Void, Void, String> {
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... voids) {
|
||||
return com.github.gotify.log.Log.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String s) {
|
||||
TextView content = binding.logContent;
|
||||
if (content.getSelectionStart() == content.getSelectionEnd()) {
|
||||
content.setText(s);
|
||||
}
|
||||
super.onPostExecute(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
92
app/src/main/java/com/github/gotify/log/LogsActivity.kt
Normal file
92
app/src/main/java/com/github/gotify/log/LogsActivity.kt
Normal file
@@ -0,0 +1,92 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.os.AsyncTask
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.github.gotify.R
|
||||
import com.github.gotify.Utils
|
||||
import com.github.gotify.databinding.ActivityLogsBinding
|
||||
|
||||
internal class LogsActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityLogsBinding
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityLogsBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
Log.i("Entering ${javaClass.simpleName}")
|
||||
updateLogs()
|
||||
setSupportActionBar(binding.appBarDrawer.toolbar)
|
||||
val actionBar = supportActionBar
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true)
|
||||
actionBar.setDisplayShowCustomEnabled(true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLogs() {
|
||||
RefreshLogs { result ->
|
||||
val content = binding.logContent
|
||||
if (content.selectionStart == content.selectionEnd) {
|
||||
content.text = result
|
||||
}
|
||||
}.execute()
|
||||
|
||||
if (!isDestroyed) {
|
||||
handler.postDelayed(this::updateLogs, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.logs_action, menu)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
finish()
|
||||
true
|
||||
}
|
||||
R.id.action_delete_logs -> {
|
||||
Log.clear()
|
||||
binding.logContent.text = null
|
||||
true
|
||||
}
|
||||
R.id.action_copy_logs -> {
|
||||
val content = binding.logContent
|
||||
val clipboardManager =
|
||||
getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clipData = ClipData.newPlainText("GotifyLog", content.text.toString())
|
||||
clipboardManager.setPrimaryClip(clipData)
|
||||
Utils.showSnackBar(this, getString(R.string.logs_copied))
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
class RefreshLogs(private val action: (result: String) -> Unit)
|
||||
: AsyncTask<Unit, Unit, String>() {
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun doInBackground(vararg params: Unit?): String {
|
||||
return Log.get()
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onPostExecute(result: String?) {
|
||||
action(result ?: "")
|
||||
super.onPostExecute(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.github.gotify.log;
|
||||
|
||||
public class UncaughtExceptionHandler {
|
||||
public static void registerCurrentThread() {
|
||||
Thread.setDefaultUncaughtExceptionHandler((t, e) -> Log.e("uncaught exception", e));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.github.gotify.log
|
||||
|
||||
import com.github.gotify.log.Log.e
|
||||
|
||||
internal object UncaughtExceptionHandler {
|
||||
fun registerCurrentThread() {
|
||||
Thread.setDefaultUncaughtExceptionHandler { _, e: Throwable? ->
|
||||
e("uncaught exception", e!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user