Use Timber for logging

This commit is contained in:
veyndan 2017-04-17 16:24:50 +01:00
parent 052d0c9c8e
commit d33935c70f
35 changed files with 238 additions and 259 deletions

View file

@ -4,7 +4,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.support.v7.app.AlertDialog;
import org.mediawiki.api.ApiResult;
import org.mediawiki.api.MWApi;
@ -12,11 +12,10 @@ import org.mediawiki.api.MWApi;
import java.io.IOException;
import java.util.ArrayList;
import android.support.v7.app.AlertDialog;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionsActivity;
import timber.log.Timber;
/**
* Sends asynchronous queries to the Commons MediaWiki API to check that file doesn't already exist
@ -24,8 +23,6 @@ import fr.free.nrw.commons.contributions.ContributionsActivity;
*/
public class ExistingFileAsync extends AsyncTask<Void, Void, Boolean> {
private static final String TAG = ExistingFileAsync.class.getName();
private String fileSHA1;
private Context context;
@ -51,14 +48,14 @@ public class ExistingFileAsync extends AsyncTask<Void, Void, Boolean> {
.param("list", "allimages")
.param("aisha1", fileSHA1)
.get();
Log.d(TAG, "Searching Commons API for existing file: " + result.toString());
Timber.d("Searching Commons API for existing file: %s", result);
} catch (IOException e) {
Log.e(TAG, "IO Exception: ", e);
Timber.e(e, "IO Exception: ");
return false;
}
ArrayList<ApiResult> resultNodes = result.getNodes("/api/query/allimages/img");
Log.d(TAG, "Result nodes: " + resultNodes);
Timber.d("Result nodes: %s", resultNodes);
boolean fileExists;
if (!resultNodes.isEmpty()) {
@ -67,7 +64,7 @@ public class ExistingFileAsync extends AsyncTask<Void, Void, Boolean> {
fileExists = false;
}
Log.d(TAG, "File already exists in Commons:" + fileExists);
Timber.d("File already exists in Commons: %s", fileExists);
return fileExists;
}

View file

@ -10,10 +10,11 @@ import android.media.ExifInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.util.Log;
import java.io.IOException;
import timber.log.Timber;
/**
* Extracts geolocation to be passed to API for category suggestions. If a picture with geolocation
* is uploaded, extract latitude and longitude from EXIF data of image. If a picture without
@ -21,8 +22,6 @@ import java.io.IOException;
*/
public class GPSExtractor {
private static final String TAG = GPSExtractor.class.getName();
private String filePath;
private double decLatitude, decLongitude;
private Double currentLatitude = null;
@ -46,7 +45,7 @@ public class GPSExtractor {
private boolean gpsPreferenceEnabled() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
boolean gpsPref = sharedPref.getBoolean("allowGps", false);
Log.d(TAG, "Gps pref set to: " + gpsPref);
Timber.d("Gps pref set to: %b", gpsPref);
return gpsPref;
}
@ -66,9 +65,9 @@ public class GPSExtractor {
myLocationListener.onLocationChanged(location);
}
} catch (IllegalArgumentException e) {
Log.e(TAG, "Illegal argument exception", e);
Timber.e(e, "Illegal argument exception");
} catch (SecurityException e) {
Log.e(TAG, "Security exception", e);
Timber.e(e, "Security exception");
}
}
@ -76,7 +75,7 @@ public class GPSExtractor {
try {
locationManager.removeUpdates(myLocationListener);
} catch (SecurityException e) {
Log.e(TAG, "Security exception", e);
Timber.e(e, "Security exception");
}
}
@ -98,10 +97,10 @@ public class GPSExtractor {
try {
exif = new ExifInterface(filePath);
} catch (IOException e) {
Log.w(TAG, e);
Timber.w(e);
return null;
} catch (IllegalArgumentException e) {
Log.w(TAG, e);
Timber.w(e);
return null;
}
@ -110,14 +109,15 @@ public class GPSExtractor {
registerLocationManager();
imageCoordsExists = false;
Log.d(TAG, "EXIF data has no location info");
Timber.d("EXIF data has no location info");
//Check what user's preference is for automatic location detection
boolean gpsPrefEnabled = gpsPreferenceEnabled();
//Check that currentLatitude and currentLongitude have been explicitly set by MyLocationListener and do not default to (0.0,0.0)
if (gpsPrefEnabled && currentLatitude != null && currentLongitude != null) {
Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude);
Timber.d("Current location values: Lat = %f Long = %f",
currentLatitude, currentLongitude);
return String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
} else {
// No coords found
@ -128,7 +128,7 @@ public class GPSExtractor {
} else {
//If image has EXIF data, extract image coords
imageCoordsExists = true;
Log.d(TAG, "EXIF data has location info");
Timber.d("EXIF data has location info");
latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
@ -136,8 +136,8 @@ public class GPSExtractor {
longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
if (latitude!=null && latitude_ref!=null && longitude!=null && longitude_ref!=null) {
Log.d(TAG, "Latitude: " + latitude + " " + latitude_ref);
Log.d(TAG, "Longitude: " + longitude + " " + longitude_ref);
Timber.d("Latitude: %s %s", latitude, latitude_ref);
Timber.d("Longitude: %s %s", longitude, longitude_ref);
decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);
return decimalCoords;
@ -160,17 +160,17 @@ public class GPSExtractor {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, provider + "'s status changed to " + status);
Timber.d("%s's status changed to %d", provider, status);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "Provider " + provider + " enabled");
Timber.d("Provider %s enabled", provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "Provider " + provider + " disabled");
Timber.d("Provider %s disabled", provider);
}
}
@ -201,7 +201,7 @@ public class GPSExtractor {
}
String decimalCoords = String.valueOf(decLatitude) + "|" + String.valueOf(decLongitude);
Log.d(TAG, "Latitude and Longitude are " + decimalCoords);
Timber.d("Latitude and Longitude are %s", decimalCoords);
return decimalCoords;
}

View file

@ -14,7 +14,6 @@ import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
@ -36,6 +35,7 @@ import fr.free.nrw.commons.modifications.CategoryModifier;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.modifications.TemplateRemoveModifier;
import timber.log.Timber;
public class MultipleShareActivity
extends AuthenticatedActivity
@ -116,7 +116,7 @@ public class MultipleShareActivity
private void multipleUploadBegins() {
Log.d("MultipleShareActivity", "Multiple upload begins");
Timber.d("Multiple upload begins");
final ProgressDialog dialog = new ProgressDialog(MultipleShareActivity.this);
dialog.setIndeterminate(false);

View file

@ -2,7 +2,6 @@ package fr.free.nrw.commons.upload;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.android.volley.Cache;
import com.android.volley.NetworkResponse;
@ -22,6 +21,8 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import timber.log.Timber;
/**
* Uses the Volley library to implement asynchronous calls to the Commons MediaWiki API to match
* GPS coordinates with nearby Commons categories. Parses the results using GSON to obtain a list
@ -38,7 +39,6 @@ public class MwVolleyApi {
private static List<String> categoryList;
private static final String MWURL = "https://commons.wikimedia.org/";
private static final String TAG = MwVolleyApi.class.getName();
public MwVolleyApi(Context context) {
this.context = context;
@ -52,13 +52,13 @@ public class MwVolleyApi {
public static void setGpsCat(List<String> cachedList) {
categoryList = new ArrayList<>();
categoryList.addAll(cachedList);
Log.d(TAG, "Setting GPS cats from cache: " + categoryList.toString());
Timber.d("Setting GPS cats from cache: %s", categoryList);
}
public void request(String coords) {
coordsLog = coords;
String apiUrl = buildUrl(coords);
Log.d(TAG, "URL: " + apiUrl);
Timber.d("URL: %s", apiUrl);
JsonRequest<QueryResponse> request = new QueryRequest(apiUrl,
new LogResponseListener<QueryResponse>(), new LogResponseErrorListener());
@ -110,7 +110,7 @@ public class MwVolleyApi {
@Override
public void onResponse(T response) {
Log.d(TAG, response.toString());
Timber.d(response.toString());
}
}
@ -118,12 +118,11 @@ public class MwVolleyApi {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.toString());
Timber.e(error.toString());
}
}
private static class QueryRequest extends JsonRequest<QueryResponse> {
private static final String TAG = QueryRequest.class.getName();
public QueryRequest(String url,
Response.Listener<QueryResponse> listener,
@ -169,11 +168,11 @@ public class MwVolleyApi {
private String printSet() {
if (categorySet == null || categorySet.isEmpty()) {
GpsCatExists.setGpsCatExists(false);
Log.d(TAG, "gpsCatExists=" + GpsCatExists.getGpsCatExists());
Timber.d("gpsCatExists=%b", GpsCatExists.getGpsCatExists());
return "No collection of categories";
} else {
GpsCatExists.setGpsCatExists(true);
Log.d(TAG, "gpsCatExists=" + GpsCatExists.getGpsCatExists());
Timber.d("gpsCatExists=%b", GpsCatExists.getGpsCatExists());
return "CATEGORIES FOUND" + categorySet.toString();
}

View file

@ -12,7 +12,6 @@ import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NavUtils;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
@ -38,6 +37,7 @@ import fr.free.nrw.commons.modifications.CategoryModifier;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.modifications.TemplateRemoveModifier;
import timber.log.Timber;
/**
* Activity for the title/desc screen after image is selected. Also starts processing image
@ -48,8 +48,6 @@ public class ShareActivity
implements SingleUploadFragment.OnUploadActionInitiated,
CategorizationFragment.OnCategoriesSaveHandler {
private static final String TAG = ShareActivity.class.getName();
private SingleUploadFragment shareView;
private CategorizationFragment categorizationFragment;
@ -118,7 +116,7 @@ public class ShareActivity
if (cacheFound == false) {
//Has to be called after apiCall.request()
app.cacheData.cacheCategory();
Log.d(TAG, "Cache the categories found");
Timber.d("Cache the categories found");
}
uploadController.startUpload(title, mediaUri, description, mimeType, source, decimalCoords, new UploadController.ContributionUploadProgress() {
@ -245,15 +243,15 @@ public class ShareActivity
//Test SHA1 of image to see if it matches SHA1 of a file on Commons
try {
InputStream inputStream = getContentResolver().openInputStream(mediaUri);
Log.d(TAG, "Input stream created from " + mediaUriString);
Timber.d("Input stream created from %s", mediaUriString);
String fileSHA1 = Utils.getSHA1(inputStream);
Log.d(TAG, "File SHA1 is: " + fileSHA1);
Timber.d("File SHA1 is: %s", fileSHA1);
ExistingFileAsync fileAsyncTask = new ExistingFileAsync(fileSHA1, this);
fileAsyncTask.execute();
} catch (IOException e) {
Log.d(TAG, "IO Exception: ", e);
Timber.d(e, "IO Exception: ");
}
}
@ -263,8 +261,8 @@ public class ShareActivity
requestAuthToken();
Log.d(TAG, "Uri: " + mediaUriString);
Log.d(TAG, "Ext storage dir: " + Environment.getExternalStorageDirectory());
Timber.d("Uri: %s", mediaUriString);
Timber.d("Ext storage dir: %s", Environment.getExternalStorageDirectory());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
useNewPermissions = true;
@ -378,8 +376,8 @@ public class ShareActivity
*/
public void getFileMetadata(boolean gpsEnabled) {
filePath = FileUtils.getPath(this, mediaUri);
Log.d(TAG, "Filepath: " + filePath);
Log.d(TAG, "Calling GPSExtractor");
Timber.d("Filepath: %s", filePath);
Timber.d("Calling GPSExtractor");
if(imageObj == null) {
imageObj = new GPSExtractor(filePath, this);
}
@ -397,7 +395,7 @@ public class ShareActivity
*/
public void useImageCoords() {
if(decimalCoords != null) {
Log.d(TAG, "Decimal coords of image: " + decimalCoords);
Timber.d("Decimal coords of image: %s", decimalCoords);
// Only set cache for this point if image has coords
if (imageObj.imageCoordsExists) {
@ -415,10 +413,10 @@ public class ShareActivity
if (catListEmpty) {
cacheFound = false;
apiCall.request(decimalCoords);
Log.d(TAG, "displayCatList size 0, calling MWAPI" + displayCatList.toString());
Timber.d("displayCatList size 0, calling MWAPI %s", displayCatList);
} else {
cacheFound = true;
Log.d(TAG, "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString());
Timber.d("Cache found, setting categoryList in MwVolleyApi to %s", displayCatList);
MwVolleyApi.setGpsCat(displayCatList);
}
}
@ -429,10 +427,10 @@ public class ShareActivity
super.onPause();
try {
imageObj.unregisterLocationManager();
Log.d(TAG, "Unregistered locationManager");
Timber.d("Unregistered locationManager");
}
catch (NullPointerException e) {
Log.d(TAG, "locationManager does not exist, not unregistered");
Timber.d("locationManager does not exist, not unregistered");
}
}

View file

@ -11,7 +11,6 @@ import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -37,6 +36,7 @@ import butterknife.OnTouch;
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import timber.log.Timber;
public class SingleUploadFragment extends Fragment {
private SharedPreferences prefs;
@ -54,8 +54,6 @@ public class SingleUploadFragment extends Fragment {
private OnUploadActionInitiated uploadActionInitiatedHandler;
private static final String TAG = SingleUploadFragment.class.getName();
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activity_share, menu);
@ -103,7 +101,7 @@ public class SingleUploadFragment extends Fragment {
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
license = prefs.getString(Prefs.DEFAULT_LICENSE, Prefs.Licenses.CC_BY_SA_3);
Log.d("Single Upload fragment", license);
Timber.d(license);
ArrayAdapter<String> adapter;
if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("theme",true)) {
@ -117,7 +115,7 @@ public class SingleUploadFragment extends Fragment {
licenseSpinner.setAdapter(adapter);
int position = licenseItems.indexOf(getString(Utils.licenseNameFor(license)));
Log.d("Single Upload fragment", "Position:"+position+" "+getString(Utils.licenseNameFor(license)));
Timber.d("Position: %d %s", position, getString(Utils.licenseNameFor(license)));
licenseSpinner.setSelection(position);
TextWatcher uploadEnabler = new TextWatcher() {
@ -190,7 +188,7 @@ public class SingleUploadFragment extends Fragment {
SharedPreferences titleDesc = PreferenceManager.getDefaultSharedPreferences(getActivity());
String title = titleDesc.getString("Title", "");
String desc = titleDesc.getString("Desc", "");
Log.d(TAG, "Title: " + title + ", Desc: " + desc);
Timber.d("Title: %s, Desc: %s", title, desc);
titleEdit.setText(title);
descEdit.setText(desc);

View file

@ -13,7 +13,6 @@ import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import java.io.IOException;
import java.util.Date;
@ -23,6 +22,7 @@ import fr.free.nrw.commons.HandlerService;
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.contributions.Contribution;
import timber.log.Timber;
public class UploadController {
private UploadService uploadService;
@ -115,11 +115,11 @@ public class UploadController {
contribution.setDataLength(length);
}
} catch(IOException e) {
Log.e("UploadController", "IO Exception: ", e);
Timber.e(e, "IO Exception: ");
} catch(NullPointerException e) {
Log.e("UploadController", "Null Pointer Exception: ", e);
Timber.e(e, "Null Pointer Exception: ");
} catch(SecurityException e) {
Log.e("UploadController", "Security Exception: ", e);
Timber.e(e, "Security Exception: ");
}
String mimeType = (String)contribution.getTag("mimeType");
@ -132,7 +132,7 @@ public class UploadController {
if(mimeType != null) {
contribution.setTag("mimeType", mimeType);
imagePrefix = mimeType.startsWith("image/");
Log.d("UploadController", "MimeType is: " + mimeType);
Timber.d("MimeType is: %s", mimeType);
}
if(imagePrefix && contribution.getDateCreated() == null) {

View file

@ -10,7 +10,6 @@ import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
@ -36,6 +35,7 @@ import fr.free.nrw.commons.contributions.ContributionsActivity;
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import in.yuvi.http.fluent.ProgressListener;
import timber.log.Timber;
public class UploadService extends HandlerService<Contribution> {
@ -87,7 +87,7 @@ public class UploadService extends HandlerService<Contribution> {
@Override
public void onProgress(long transferred, long total) {
Log.d("Commons", String.format("Uploaded %d of %d", transferred, total));
Timber.d("Uploaded %d of %d", transferred, total);
if(!notificationTitleChanged) {
curProgressNotification.setContentTitle(notificationProgressTitle);
notificationTitleChanged = true;
@ -112,7 +112,7 @@ public class UploadService extends HandlerService<Contribution> {
public void onDestroy() {
super.onDestroy();
contributionsProviderClient.release();
Log.d("Commons", "UploadService.onDestroy; " + unfinishedUploads + " are yet to be uploaded");
Timber.d("UploadService.onDestroy; %s are yet to be uploaded", unfinishedUploads);
}
@Override
@ -149,7 +149,7 @@ public class UploadService extends HandlerService<Contribution> {
toUpload++;
if (curProgressNotification != null && toUpload != 1) {
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
Log.d("Commons", String.format("%d uploads left", toUpload));
Timber.d("%d uploads left", toUpload);
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
}
@ -173,8 +173,8 @@ public class UploadService extends HandlerService<Contribution> {
Contribution.Table.COLUMN_STATE + " = ? OR " + Contribution.Table.COLUMN_STATE + " = ?",
new String[]{ String.valueOf(Contribution.STATE_QUEUED), String.valueOf(Contribution.STATE_IN_PROGRESS) }
);
Log.d("Commons", "Set " + updated + " uploads to failed");
Log.d("Commons", "Flags is" + flags + " id is" + startId);
Timber.d("Set %d uploads to failed", updated);
Timber.d("Flags is %d id is %d", flags, startId);
freshStart = false;
}
return START_REDELIVER_INTENT;
@ -192,12 +192,12 @@ public class UploadService extends HandlerService<Contribution> {
//FIXME: Google Photos bug
file = this.getContentResolver().openInputStream(contribution.getLocalUri());
} catch(FileNotFoundException e) {
Log.d("Exception", "File not found");
Timber.d("File not found");
Toast fileNotFound = Toast.makeText(this, R.string.upload_failed, Toast.LENGTH_LONG);
fileNotFound.show();
}
Log.d("Commons", "Before execution!");
Timber.d("Before execution!");
curProgressNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
@ -218,16 +218,16 @@ public class UploadService extends HandlerService<Contribution> {
MimeTypeMap.getSingleton().getExtensionFromMimeType((String)contribution.getTag("mimeType")));
synchronized (unfinishedUploads) {
Log.d("Commons", "making sure of uniqueness of name: " + filename);
Timber.d("making sure of uniqueness of name: %s", filename);
filename = findUniqueFilename(filename);
unfinishedUploads.add(filename);
}
if(!api.validateLogin()) {
// Need to revalidate!
if(app.revalidateAuthToken()) {
Log.d("Commons", "Successfully revalidated token!");
Timber.d("Successfully revalidated token!");
} else {
Log.d("Commons", "Unable to revalidate :(");
Timber.d("Unable to revalidate :(");
// TODO: Put up a new notification, ask them to re-login
stopForeground(true);
Toast failureToast = Toast.makeText(this, R.string.authentication_failed, Toast.LENGTH_LONG);
@ -242,7 +242,7 @@ public class UploadService extends HandlerService<Contribution> {
);
result = api.upload(filename, file, contribution.getDataLength(), contribution.getPageContents(), contribution.getEditSummary(), notificationUpdater);
Log.d("Commons", "Response is" + Utils.getStringFromDOM(result.getDocument()));
Timber.d("Response is %s", Utils.getStringFromDOM(result.getDocument()));
curProgressNotification = null;
@ -277,7 +277,7 @@ public class UploadService extends HandlerService<Contribution> {
.log();
}
} catch(IOException e) {
Log.d("Commons", "I have a network fuckup");
Timber.d("I have a network fuckup");
showFailedNotification(contribution);
return;
} finally {