FairEmail/app/src/main/java/eu/faircode/email/Log.java

108 lines
3.7 KiB
Java
Raw Normal View History

2018-12-24 12:27:45 +00:00
package eu.faircode.email;
2018-12-31 08:04:33 +00:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
2019-01-25 14:40:46 +00:00
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
2019-05-10 06:53:45 +00:00
import com.bugsnag.android.Bugsnag;
import com.bugsnag.android.Severity;
2019-04-24 16:39:32 +00:00
import java.lang.reflect.Array;
2019-01-25 14:40:46 +00:00
import java.util.Set;
2018-12-24 12:27:45 +00:00
public class Log {
static final String TAG = "fairemail";
public static int i(String msg) {
2018-12-28 09:56:40 +00:00
if (BuildConfig.BETA_RELEASE)
return android.util.Log.i(TAG, msg);
else
return 0;
2018-12-24 12:27:45 +00:00
}
public static int w(String msg) {
return android.util.Log.w(TAG, msg);
}
public static int e(String msg) {
return android.util.Log.e(TAG, msg);
}
public static int w(Throwable ex) {
2019-05-10 18:55:48 +00:00
Bugsnag.notify(ex, Severity.INFO);
2018-12-24 12:27:45 +00:00
return android.util.Log.w(TAG, ex + "\n" + android.util.Log.getStackTraceString(ex));
}
public static int e(Throwable ex) {
2019-05-10 18:55:48 +00:00
Bugsnag.notify(ex, Severity.WARNING);
2018-12-24 12:27:45 +00:00
return android.util.Log.e(TAG, ex + "\n" + android.util.Log.getStackTraceString(ex));
}
public static int w(String prefix, Throwable ex) {
2019-05-10 18:55:48 +00:00
Bugsnag.notify(ex, Severity.INFO);
2018-12-24 12:27:45 +00:00
return android.util.Log.w(TAG, prefix + " " + ex + "\n" + android.util.Log.getStackTraceString(ex));
}
public static int e(String prefix, Throwable ex) {
2019-05-10 18:55:48 +00:00
Bugsnag.notify(ex, Severity.WARNING);
2018-12-24 12:27:45 +00:00
return android.util.Log.e(TAG, prefix + " " + ex + "\n" + android.util.Log.getStackTraceString(ex));
}
2019-01-25 14:40:46 +00:00
public static void logExtras(Intent intent) {
if (intent != null)
logBundle(intent.getExtras());
}
public static void logBundle(Bundle data) {
if (data != null) {
Set<String> keys = data.keySet();
StringBuilder stringBuilder = new StringBuilder();
for (String key : keys) {
2019-04-24 16:39:32 +00:00
Object v = data.get(key);
Object value = v;
if (v != null && v.getClass().isArray()) {
int length = Array.getLength(v);
if (length <= 10) {
String[] elements = new String[length];
for (int i = 0; i < length; i++) {
Object element = Array.get(v, i);
2019-04-24 17:34:23 +00:00
if (element instanceof Long)
elements[i] = "0x" + Long.toHexString((Long) element);
else
elements[i] = (element == null ? null : element.toString());
2019-04-24 16:39:32 +00:00
}
value = TextUtils.join(",", elements);
}
2019-04-24 17:34:23 +00:00
} else if (v instanceof Long)
value = "0x" + Long.toHexString((Long) v);
2019-01-25 14:40:46 +00:00
stringBuilder.append(key)
.append("=")
.append(value)
2019-04-24 16:39:32 +00:00
.append(value == null ? "" : " (" + v.getClass().getSimpleName() + ")")
2019-01-25 14:40:46 +00:00
.append("\r\n");
}
i(stringBuilder.toString());
}
}
2018-12-24 12:27:45 +00:00
}