Add LOGD checks so Fennec can skip logging (and log message construction) when logcat is not listening




7 years ago
6 years ago


(Reporter: cpeterson, Assigned: cpeterson)



Firefox Tracking Flags

(Not tracked)




7 years ago
This would be a run-time, not compile-time, check so testers in the field could still collect logs if they want.

For example, bug 717349 calculates checkboard regions (on every frame), but these calculations are unnecessary if adb logcat is not listening for our Log.d() messages. This is the normal use case for 99% of our users.

    private static final boolean LOGD = Log.isLoggable(LOGTAG);

    if (LOGD) {
        String msg = DoSomeExpensiveOperationsToConstructDebugMessage();
        Log.d(LOGTAG, msg);

Note that Log.isLoggable() is more expensive than it seems. It calls out to JNI, mallocs some strings, and does an O(n) search of system properties flags. So we would want to cache isLoggable() values when the app is launched.

PMD has describes its "ProtectLogD" and "ProtectLogV" checks here:
does this help at all with perf?
Assignee: nobody → cpeterson
(In reply to Doug Turner (:dougt) from comment #1)
> does this help at all with perf?

It would help some with perf, assuming the conditional were false. To quote:

  Tip: Don't forget that when you make a call like

  Log.v(TAG, "index=" + i);

  that when you're building the string to pass into Log.d, the compiler uses a
  StringBuilder and at least three allocations occur: the StringBuilder itself,
  the buffer, and the String object. Realistically, there is also another buffer
  allocation and copy, and even more pressure on the gc. That means that if your
  log message is filtered out, you might be doing significant work and incurring
  significant overhead. 

The log call itself isn't too bad, but the constant string building and GC can't be good.

Community wisdom says to do exactly as Chris suggests, and furthermore to use ProGuard to eliminate debug logging entirely in release builds:

The Android release checklist even mentions this, though a quick look at logcat output suggests that it's not enforced, of course. (Because Android.)

Comment 3

7 years ago

I love Google's "Configuring our Application for Release" advice: "You can deactivate logging by removing calls to Log methods in your source files."

> Community wisdom says to do exactly as Chris suggests, and furthermore to use ProGuard to 
> eliminate debug logging entirely in release builds:

Note that ProGuard can eliminate the Log() method calls, but the expensive string-building of Log() parameters may not be removed.
Also proguard is compile-time, not run-time. We should enable/disable logging based on prefs. I had a script to clean up log lines, it shouldn't be too hard to modify it to do what we want here.
The script is attached to bug 703059. Download it to $SOMEFOLDER/, compile it, and run like so from inside mobile/android/base/ (and the other folders)

grep -l "Log" *.java* | xargs java -cp $SOMEFOLDER logtag

It will update *.java* and clean up log lines to be consistent. You can modify line 42 of to insert something like "if (LogHelper.loggingEnabled)" before each log line, and create a LogHelper class that updates a loggingEnabled variable with a pref or whatever.
(In reply to Kartikaya Gupta (:kats) from comment #5)

> grep -l "Log" *.java* | xargs java -cp $SOMEFOLDER logtag

You probably want

  --exclude sync/

in your grep invocation. We already have methods in place to handle this.

Also, wow. You wrote a text transformation in Java, not sed! Go you!
The invocation I had above actually doesn't even recurse into subfolders. Back when I first wrote it everything was in a couple of folders so I just ran it manually on each folder. But yeah, adjust as necessary to get the right files.

I did consider sed, but inserting the LOGTAG line seemed like it would be too convoluted in sed or even awk and java is my default language for when i don't have a more specific tool :)

Comment 8

7 years ago
If we are going to update every Log() call, I suggest we standardize on:

1. Log.d() for all developer log output (replacing a mess of calls to Log.e/w/i/v)
2. and [1] for conditions that should be reported in Bugzilla if seen in logcat. is only available >= Froyo, so if we continue to support Eclair, we can call Log.e(LOGTAG, msg, new AssertionError()) instead.

Sounds good to me.
Priority: -- → P3


7 years ago
Priority: P3 → P5

Comment 10

6 years ago
WONTFIX because bug 815684 will read logcat retroactively when uploading a crash report.
Last Resolved: 6 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.