it might cause multithread problem i think..
SR1s opened this issue · 3 comments
I saw that className
and methodName
variates is static,
that means when using this class on multithread condition,
it will be incorrect if there is loging too much
Hi @SR1s
What's the difference between DebugLog and Log.java?
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/util/Log.java
Ok, the major difference is that you add extra message like class name, function name and line number. It's a good feature for debug program. But the problem is that you store these extra message in the static variate, in multithread context, it will be change by other thread when it is loging one log but interrupt by the VM.
Let me show you a example.
I did a test, the code is below, using DebugLog to log the message.
public class App extends Application {
private static Context sContext;
@Override
public void onCreate() {
super.onCreate();
Thread t1 = new LogOne();
Thread t2 = new LogTwo();
t1.start();
t2.start();
}
public static Context getContext() {
return sContext;
}
class LogOne extends Thread {
@Override
public void run() {
super.run();
int i = 10000;
while (i-- > 0) {
messageOne();
}
}
private void messageOne() {
DebugLog.i("log by LogOne");
}
}
class LogTwo extends Thread {
@Override
public void run() {
super.run();
int i = 10000;
while (i-- > 0) {
messageTwo();
}
}
private void messageTwo() {
DebugLog.i("log by LogTwo");
}
}
}
It should always only log these two message:
[messageOne:41]log by LogOne
[messageTwo:57]log by LogTwo
However, in the logcat, I saw this:
This is what I mean the multithread problem.
My suggestion is using local variate instead of static variate to avoid these problem.
Another simpler suggestion is to create local variable or private function that returns the tag and the line number, also because every log line is different of the previous, let's say, at 99% of the times. So why keep them in a static variable if they are changed at every call?
I've created a new Log class based on the roboguice/util/Ln.java
class and this one. This class resolves also the issue #10.