开发者

Debug mode in a library

开发者 https://www.devze.com 2023-04-12 08:16 出处:网络
I have an Android application with several \"Log.d\" calls along the code in order to following the events of the app.

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消