Addev
Addev

Reputation: 32263

Debug mode in a library

I have an Android application with several "Log.d" calls along the code in order to following the events of the app. In order to enable or disable the debug messages I call the Log with

 if (MyApp.debug) Log.d("Doing something");

Where MyApp.debug is a final boolean that I change before compiling.

Now I want to use some classes from the application as a library for another app, so I copied them into a new library project. The problem is that now in the library I have no a MyApp class.

How can I make something similar for controlling from the app if the library must print the debug messages or not?

Thanks in advance

Upvotes: 0

Views: 148

Answers (2)

Ewald
Ewald

Reputation: 5761

I'd suggest placing the variable inside a static class called Log, which you could then, at runtime, have checking a static boolean variable to figure out if it should log or not.

That way, you still have the convenience, but it's all contained inside the logger.

For example:

public class Log {

  public static boolean mustLog = false;

  // methods etc.
}

Then, in your app, just use if(Log.mustLog) ...

Hope that helps.

Upvotes: 1

alexfernandez
alexfernandez

Reputation: 1988

You can create a different custom class in your library

public Class LogPrefs
{
  public static final boolean enabled = true;
}

and set it on and off as you need. Then just

if (LogPrefs.enabled) Log.d("Doing something");

I always erase debug messages as I go, since they tend to clutter the screen. I keep however Info and Error messages; eventually they can be recovered and sent back after a crash.

Upvotes: 0

Related Questions