Use new log system

This commit is contained in:
fkloft 2014-01-05 19:16:46 +01:00
parent b31d973cff
commit 5d674eee40
15 changed files with 84 additions and 50 deletions

View File

@ -83,7 +83,7 @@ public class IITC_FileManager {
try { try {
return new FileInputStream(file); return new FileInputStream(file);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); Log.w(e);
} }
} }
@ -96,14 +96,14 @@ public class IITC_FileManager {
URL url = new URL(context, filename); URL url = new URL(context, filename);
return url.openStream(); return url.openStream();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
} }
} else { } else {
File file = new File(source + File.separatorChar + filename); File file = new File(source + File.separatorChar + filename);
try { try {
return new FileInputStream(file); return new FileInputStream(file);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); Log.w(e);
} }
} }
} }
@ -117,7 +117,7 @@ public class IITC_FileManager {
try { try {
stream = getAssetFile(uri.getPath().substring(1)); stream = getAssetFile(uri.getPath().substring(1));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
return EMPTY; return EMPTY;
} }
@ -140,7 +140,7 @@ public class IITC_FileManager {
try { try {
stream = new FileInputStream(new File(uri.getPath())); stream = new FileInputStream(new File(uri.getPath()));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
return EMPTY; return EMPTY;
} }
@ -173,7 +173,7 @@ public class IITC_FileManager {
os.write(buffer, 0, read); os.write(buffer, 0, read);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
return ""; return "";
} }
return os.toString(); return os.toString();

View File

@ -68,7 +68,7 @@ public class IITC_JSInterface {
.getPackageInfo(mIitc.getPackageName(), 0); .getPackageInfo(mIitc.getPackageName(), 0);
versionCode = pInfo.versionCode; versionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
e.printStackTrace(); Log.w(e);
} }
return versionCode; return versionCode;
} }
@ -81,7 +81,7 @@ public class IITC_JSInterface {
PackageInfo info = pm.getPackageInfo(mIitc.getPackageName(), 0); PackageInfo info = pm.getPackageInfo(mIitc.getPackageName(), 0);
buildVersion = info.versionName; buildVersion = info.versionName;
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
e.printStackTrace(); Log.w(e);
} }
return buildVersion; return buildVersion;
} }

View File

@ -10,6 +10,8 @@ import android.widget.TextView;
import com.cradle.iitc_mobile.Log.Message; import com.cradle.iitc_mobile.Log.Message;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Receiver { public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Receiver {
@ -50,8 +52,20 @@ public class IITC_LogAdapter extends ArrayAdapter<Log.Message> implements Log.Re
tv = (TextView) v.findViewById(R.id.log_time); tv = (TextView) v.findViewById(R.id.log_time);
tv.setText(FORMATTER.format(item.getDate())); tv.setText(FORMATTER.format(item.getDate()));
String msg = item.getMsg();
if (item.getTr() != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
item.getTr().printStackTrace(pw);
if (msg == null || msg.isEmpty())
msg = sw.toString();
else
msg += "\n" + sw.toString();
}
tv = (TextView) v.findViewById(R.id.log_msg); tv = (TextView) v.findViewById(R.id.log_msg);
tv.setText(item.getMsg()); tv.setText(msg);
return v; return v;
} }

View File

@ -231,7 +231,7 @@ public class IITC_MapSettings implements OnItemSelectedListener, OnItemClickList
base_layers = new JSONArray(base_layer); base_layers = new JSONArray(base_layer);
overlay_layers = new JSONArray(overlay_layer); overlay_layers = new JSONArray(overlay_layer);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); Log.w(e);
return; return;
} }
@ -255,7 +255,7 @@ public class IITC_MapSettings implements OnItemSelectedListener, OnItemClickList
mBaseLayers.add(layer); mBaseLayers.add(layer);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); Log.w(e);
} }
} }
mBaseLayers.notifyDataSetChanged(); mBaseLayers.notifyDataSetChanged();
@ -274,7 +274,7 @@ public class IITC_MapSettings implements OnItemSelectedListener, OnItemClickList
mOverlayLayers.add(layer); mOverlayLayers.add(layer);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); Log.w(e);
} }
} }
mOverlayLayers.notifyDataSetChanged(); mOverlayLayers.notifyDataSetChanged();

View File

@ -205,7 +205,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
handleGeoUri(uri); handleGeoUri(uri);
return; return;
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); Log.w(e);
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setTitle(R.string.intent_error) .setTitle(R.string.intent_error)
.setMessage(e.getReason()) .setMessage(e.getReason())
@ -513,7 +513,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
try { try {
intent.putExtra("iitc_version", mFileManager.getIITCVersion()); intent.putExtra("iitc_version", mFileManager.getIITCVersion());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
return true; return true;
} }
startActivity(intent); startActivity(intent);
@ -680,7 +680,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
try { try {
obj.put("code", code); obj.put("code", code);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); Log.w(e);
return; return;
} }

View File

@ -120,7 +120,7 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
asset_array = am.list("plugins"); asset_array = am.list("plugins");
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); Log.w(e);
} }
if (asset_array == null) { if (asset_array == null) {
asset_array = new String[0]; asset_array = new String[0];
@ -163,11 +163,9 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
Scanner s = null; Scanner s = null;
String src = ""; String src = "";
try { try {
s = new Scanner(getAssets().open("plugins/" + asset)) s = new Scanner(getAssets().open("plugins/" + asset)).useDelimiter("\\A");
.useDelimiter("\\A"); } catch (IOException e) {
} catch (IOException e2) { Log.w(e);
// TODO Auto-generated catch block
e2.printStackTrace();
} }
if (s != null) { if (s != null) {
src = s.hasNext() ? s.next() : ""; src = s.hasNext() ? s.next() : "";
@ -184,7 +182,7 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
try { try {
s = new Scanner(file).useDelimiter("\\A"); s = new Scanner(file).useDelimiter("\\A");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); Log.w(e);
Log.d("failed to parse file " + file); Log.d("failed to parse file " + file);
} }
if (s != null) { if (s != null) {
@ -200,7 +198,7 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
// parse plugin name, description and category // parse plugin name, description and category
// we need default versions here otherwise iitcm may crash // we need default versions here otherwise iitcm may crash
HashMap<String,String> info = IITC_FileManager.getScriptInfo(src); HashMap<String, String> info = IITC_FileManager.getScriptInfo(src);
String plugin_name = info.get("name"); String plugin_name = info.get("name");
String plugin_cat = info.get("category"); String plugin_cat = info.get("category");
String plugin_desc = info.get("description"); String plugin_desc = info.get("description");

View File

@ -64,13 +64,13 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// if the given provider doesn't exist // if the given provider doesn't exist
e.printStackTrace(); Log.w(e);
} }
try { try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// if the given provider doesn't exist // if the given provider doesn't exist
e.printStackTrace(); Log.w(e);
} }
mLocationRegistered = true; mLocationRegistered = true;
} }

View File

@ -122,7 +122,7 @@ public class IITC_WebViewClient extends WebViewClient {
try { try {
return mTileManager.getTile(url); return mTileManager.getTile(url);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); Log.w(e);
return super.shouldInterceptRequest(view, url); return super.shouldInterceptRequest(view, url);
} }
} }

View File

@ -46,6 +46,10 @@ public final class Log {
} }
} }
public static void addReceiver(Log.Receiver receiver) {
RECEIVERS.add(receiver);
}
public static void d(String msg) { public static void d(String msg) {
d(DEFAULT_TAG, msg); d(DEFAULT_TAG, msg);
} }
@ -72,6 +76,10 @@ public final class Log {
d(DEFAULT_TAG, msg, tr); d(DEFAULT_TAG, msg, tr);
} }
public static void d(Throwable tr) {
d("Unexpected " + tr, tr);
}
public static void e(String msg) { public static void e(String msg) {
e(DEFAULT_TAG, msg); e(DEFAULT_TAG, msg);
} }
@ -98,6 +106,10 @@ public final class Log {
e(DEFAULT_TAG, msg, tr); e(DEFAULT_TAG, msg, tr);
} }
public static void e(Throwable tr) {
e("Unexpected " + tr, tr);
}
public static void i(String msg) { public static void i(String msg) {
i(DEFAULT_TAG, msg); i(DEFAULT_TAG, msg);
} }
@ -124,6 +136,10 @@ public final class Log {
i(DEFAULT_TAG, msg, tr); i(DEFAULT_TAG, msg, tr);
} }
public static void i(Throwable tr) {
i("Unexpected " + tr, tr);
}
public static boolean log(ConsoleMessage message) { public static boolean log(ConsoleMessage message) {
String msg = message.sourceId(); String msg = message.sourceId();
Matcher matcher = URL_PATTERN.matcher(msg); Matcher matcher = URL_PATTERN.matcher(msg);
@ -160,6 +176,10 @@ public final class Log {
return android.util.Log.println(priority, tag, msg); return android.util.Log.println(priority, tag, msg);
} }
public static void removeReceiver(Log.Receiver receiver) {
RECEIVERS.remove(receiver);
}
public static void v(String msg) { public static void v(String msg) {
v(DEFAULT_TAG, msg); v(DEFAULT_TAG, msg);
} }
@ -186,6 +206,10 @@ public final class Log {
v(DEFAULT_TAG, msg, tr); v(DEFAULT_TAG, msg, tr);
} }
public static void v(Throwable tr) {
v("Unexpected " + tr, tr);
}
public static void w(String msg) { public static void w(String msg) {
w(DEFAULT_TAG, msg); w(DEFAULT_TAG, msg);
} }
@ -212,6 +236,10 @@ public final class Log {
w(DEFAULT_TAG, msg, tr); w(DEFAULT_TAG, msg, tr);
} }
public static void w(Throwable tr) {
w("Unexpected " + tr, tr);
}
private Log() { private Log() {
// prevent instantiation // prevent instantiation
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -256,12 +284,4 @@ public final class Log {
public static interface Receiver { public static interface Receiver {
void handle(Message message); void handle(Message message);
} }
public static void removeReceiver(Log.Receiver receiver) {
RECEIVERS.remove(receiver);
}
public static void addReceiver(Log.Receiver receiver) {
RECEIVERS.add(receiver);
}
} }

View File

@ -51,7 +51,7 @@ public class CheckHttpResponse extends AsyncTask<String, Void, Boolean> {
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
} }
return false; return false;
} }

View File

@ -35,16 +35,12 @@ public class DownloadTile extends AsyncTask<String, Void, Boolean> {
Log.d("writing to file: " + file.toString()); Log.d("writing to file: " + file.toString());
writeTileToFile(is, file); writeTileToFile(is, file);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false; return false;
} }
return true; return true;
} }
private void writeTileToFile(InputStream inStream, File file) throws Exception { private void writeTileToFile(InputStream inStream, File file) throws IOException {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
FileOutputStream outStream = new FileOutputStream(file); FileOutputStream outStream = new FileOutputStream(file);
int bufferSize = 1024; int bufferSize = 1024;
@ -53,7 +49,7 @@ public class DownloadTile extends AsyncTask<String, Void, Boolean> {
while ((len = inStream.read(buffer)) != -1) { while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len); outStream.write(buffer, 0, len);
} }
if(outStream!=null) outStream.close(); if (outStream != null)
outStream.close();
} }
} }

View File

@ -2,6 +2,8 @@ package com.cradle.iitc_mobile.async;
import android.os.AsyncTask; import android.os.AsyncTask;
import com.cradle.iitc_mobile.Log;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Scanner; import java.util.Scanner;
@ -21,7 +23,7 @@ public class UrlContentToString extends AsyncTask<URL, Integer, String> {
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A") js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A")
.next(); .next();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
} }
return js; return js;
} }

View File

@ -18,6 +18,7 @@ import android.widget.FrameLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.cradle.iitc_mobile.IITC_AboutDialogPreference; import com.cradle.iitc_mobile.IITC_AboutDialogPreference;
import com.cradle.iitc_mobile.Log;
import com.cradle.iitc_mobile.R; import com.cradle.iitc_mobile.R;
public class MainSettings extends PreferenceFragment { public class MainSettings extends PreferenceFragment {
@ -37,7 +38,7 @@ public class MainSettings extends PreferenceFragment {
PackageInfo info = pm.getPackageInfo(getActivity().getPackageName(), 0); PackageInfo info = pm.getPackageInfo(getActivity().getPackageName(), 0);
buildVersion = info.versionName; buildVersion = info.versionName;
} catch (NameNotFoundException e) { } catch (NameNotFoundException e) {
e.printStackTrace(); Log.w(e);
} }
IITC_AboutDialogPreference pref_about = (IITC_AboutDialogPreference) findPreference("pref_about"); IITC_AboutDialogPreference pref_about = (IITC_AboutDialogPreference) findPreference("pref_about");

View File

@ -4,6 +4,8 @@ import android.app.Activity;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import com.cradle.iitc_mobile.Log;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -106,15 +108,15 @@ public class IntentComparator implements Comparator<ResolveInfo> {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// Do nothing // Do nothing
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); Log.w(e);
} finally { } finally {
if (objectIn != null) { if (objectIn != null) {
try { try {
objectIn.close(); objectIn.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
} }
} }
} }
@ -173,13 +175,13 @@ public class IntentComparator implements Comparator<ResolveInfo> {
objectOut.writeObject(mIntentMap); objectOut.writeObject(mIntentMap);
fileOut.getFD().sync(); fileOut.getFD().sync();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
} finally { } finally {
if (objectOut != null) { if (objectOut != null) {
try { try {
objectOut.close(); objectOut.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.w(e);
} }
} }
} }

View File

@ -12,6 +12,7 @@ import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.view.MenuItem; import android.view.MenuItem;
import com.cradle.iitc_mobile.Log;
import com.cradle.iitc_mobile.R; import com.cradle.iitc_mobile.R;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -74,7 +75,7 @@ public class ShareActivity extends FragmentActivity implements ActionBar.TabList
+ "%20(" + URLEncoder.encode(mTitle, "UTF-8") + ")&z=" + mZoom; + "%20(" + URLEncoder.encode(mTitle, "UTF-8") + ")&z=" + mZoom;
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
gMapsUri = "http://maps.google.com/?ll=" + mLl + "&z=" + mZoom; gMapsUri = "http://maps.google.com/?ll=" + mLl + "&z=" + mZoom;
e.printStackTrace(); Log.w(e);
} }
Intent gMapsIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(gMapsUri)); Intent gMapsIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(gMapsUri));
intents.add(gMapsIntent); intents.add(gMapsIntent);