android.util.Log发布时 - 我能做什么/不做 [英] android.util.Log when publishing - what can I do / not do
问题描述
我想要扩展android.util.log,只需要一个布尔开关,所以我可以关闭日志,当我发布,并在开发时打开它,但这个类是最后的,我错过了一个技巧? / p>
我真的不想通过我的代码删除所有,真的如果最坏的情况是最坏的我可以做一个ctrl + h全局替换日志//日志,这也是一个答案。
我也意识到Log.d在运行时被剥离,但仍然运行(失去一点表现),所以不运行这个是一个额外的奖金。
是的,所以基本上我正在寻找一种方法来切换我的调试开和关编程,这也可以让我以后使它成为一个喜欢或某些东西,如果人们想要查看或帮助和发送它。
你们有什么
谢谢
由于Octavian指出插入记录常数将是最好的方法。编写一个新的类,调用原始日志记录方法,如果启用调试不是一个好主意。
良好做法:
if(CD){Log.d(CT,your log text here+ foo + bar);
不良做法:
YourLog.d(你的日志文本+ foo + bar);
//和YourLog.java的d()方法:
... {if(debugging)Log.d(tag,text);
如果类C的常量D为假,则第一个解决方案非常快。如果您有复杂的字符串操作来创建日志字符串,那么如果调试被禁用,它们将不会被执行。如果D为false,编译器甚至可以在编译时删除这些操作,这可能导致零运行时开销。第二个(坏)解决方案将始终构建整个字符串并调用一个方法,这是您不需要的开销。
一般来说,第一个解决方案将是最好的。是的,我打电话给类和成员C,D和T(常量/调试/标签) - 出于性能原因打字。 ; - )
I've got a hell of a lot of Log.i Log.d Log.e in my code for a recent app I've done. I'm about to publish this app and I don't really want people seeing it when they plug there phone into adb, but I do want it there for my own debugging.
I was wanting to extend android.util.log and just have a boolean switch in there so I could just turn off the log when I publish and turn it on when developing but this class is final, am I missing a trick?
I don't really want to go through my code an remove all, true if worst comes to worst I could do a ctrl+h global replace Log for //Log but that does suck as an answer.
I also realise that Log.d is stripped out at runtime but it is still ran (losing a little performance) so not running this would be an added bonus.
Yeah so basically I'm looking for a way to toggle my debug on and off programatically, this can also allow me later on to make it a preference or something if people want to view it or help out and send it on.
What do you guys implement for this?
Thanks
As Octavian points out inserting a logging constant would be the best way to do this. Writing a new class for this that calls the original logging methods if debugging is enabled is not a good idea.
Good practice:
if (C.D) { Log.d(C.T, "your log text here " + foo + bar); }
Bad practice:
YourLog.d("your log text here " + foo + bar);
// and in YourLog.java's d() method:
... { if (debugging) Log.d(tag, text); }
The first solution is very fast if the constant D of class C is false. If you have complex string operations for creating your logging string they will not be executed if debugging is deactivated. The compiler can even remove these operations at compile time if D is false, which may result in zero runtime overhead. The second (bad) solution will always build the whole string and call a method, which is overhead you don't need.
In general the first solution would be best. And yes, I really call the class and members C, D and T (Constants/Debugging/Tag) - for performance reasons during typing. ;-)
这篇关于android.util.Log发布时 - 我能做什么/不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!