/Debug

Android logging utility

Primary LanguageJavaApache License 2.0Apache-2.0

Debug - Android logging tool

Android Arsenal

Why?

  • Lightweight
  • Simple
  • Powerful

Debug library provides a lot of useful information. For ex., Java file name as a tag, method name and a line number where Debug function was called. Library provides the ability to jump to the source right from the logcat output.

It has no impact on release code - no additional information will be collected at runtime (aka stacktrace). The only thing is no do is to send a bool to the Debug.init() method. If you are using Gradle - it just simplifies the initialization to a line of code:

Debug.init(BuildConfig.DEBUG);

Well, if you are not using Gradle, where surely must be a way...

Note that if you are using this library in multi-process application, you must call Debug.init(boolean) for every process.

Also, it wraps String.format(), so you can create any message with nearly any quantity of variables to check at almost no pain (and time).

What's new (1.1.3

  • Decreased number of method calls inside Debug.java
  • Empty Timer implementation if !isDebug

What's new (1.1.2)

  • Methods in logcat are now clickable
  • Small change to trace pattern to exclude Debug.java

How to do it

Debug.i();
Debug.v("My message");
Debug.w("x: %d, y: %d, z: %s", 1, 72, .0F);
Debug.d(new Throwable(), "mLastItemIndex: %d", mLastItemIndex);
Debug.e(new Throwable());

Installation

Maven Central

Gradle:

compile 'ru.noties:debug:x.x.x'

The best place to initialise Debug library is an Application's onCreate() method. Debug.init() takes a boolean indicating whether logging should be done or skipped.

public class DebugApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Debug.init(BuildConfig.DEBUG);
    }
}

Usage

  • Trace method calls till current method
Debug.trace();

Output: trace

  • All Android Log levels
Debug.v();
Debug.d();
Debug.i();
Debug.w();
Debug.e();
Debug.wtf();

Output: someMethod

simpleMethod

onCreate

  • Simple exception handling
try {
    new AssertionError("This is exception");
} catch (Throwable throwable) {
     Debug.e(e);
}

Output: someMethodWithException

  • Timer with millis & nanos

see sample application for code

Output millis: timer

What's next? @Deprecated - Do not use this, as long as you can lose your own live templates

If you are using Intellij Idea or Android Studio, you can make debugging even simpler. I wrote some Live Templates for this lib. Download ru.noties.debug.live_templates.jar and import it in your IDE. After that you could do something like that:

    di
    de
    dw
    dd
    dv

and hit tab (once at a time of cause), you will get:

    Debug.i("", );
    Debug.e("", );
    Debug.w("", );
    Debug.d("", );
    Debug.v("", );

also, there are dii, dee, dww, ddd and dvv. After typing them you will get:

    Debug.e("view: %s, firstVisibleItem: %s, visibleItemCount: %s, totalItemCount: %s", view, firstVisibleItem, visibleItemCount, totalItemCount);
    Debug.i("view: %s, firstVisibleItem: %s, visibleItemCount: %s, totalItemCount: %s", view, firstVisibleItem, visibleItemCount, totalItemCount);
    Debug.v("view: %s, firstVisibleItem: %s, visibleItemCount: %s, totalItemCount: %s", view, firstVisibleItem, visibleItemCount, totalItemCount);
    Debug.d("view: %s, firstVisibleItem: %s, visibleItemCount: %s, totalItemCount: %s", view, firstVisibleItem, visibleItemCount, totalItemCount);
    Debug.w("view: %s, firstVisibleItem: %s, visibleItemCount: %s, totalItemCount: %s", view, firstVisibleItem, visibleItemCount, totalItemCount);

for a OnScrollListener.onScroll() method. Yes, the pattern will be created from method's parameters and you will not have to write it yourself.