I’m using this kind of pattern:
public class FooProvider extends ContentProvider {
private static final String TAG = FooProvider.class.getSimpleName();
...
@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
l.v(TAG, TAG + ".update() Uri:" + uri.toString() + " " + db.getPath());
Where l is my wrapper round Androids Log.v which takes String… args. This gives logcat output like this:
FooProvider.update() Uri: foo bar etc
What I’m looking for is a way to log the method name, in this case update(), automatically and in a way which survives refactoring the way TAG does. I’ve seen the class.getMethods() call but can’t see a way to index the returned list with the current method at runtime.
You may want to wrap these statements in a constant, so they get compiled out for your published version (which won’t be printing Log.v statements anyway), but you can do something like this:
It will depend upon how you call it to determine which
StackTraceElementindex you’ll want, just debug it or play around a few times to find it.When you compile your published version, just set FINAL_CONSTANT_IS_LOCAL to false and that code block will go away, always returning TAG.