log messages get lost in simple while(i Go To StackoverFlow.com

1

In this very simple code example, messages get lost every once in a while. What is wrong here?

public class AndroidTesterActivity extends Activity {
   private static final int END = 500000;
   private static int i = 0;
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      i=0;
   }

   @Override
   public void onResume()
   {
      super.onResume();
      while(i < END)
      {
         Log.d("x", ""+i++);
      }
      Log.e("x", "END");
   }
}

For example i get two consecutive log messages 139371 and 140001 ... so 630 messages are simply lost, even though i is counted up? How can i be counted without the Log being written? I have already searched, if someone else already had the same problem, but didn't find anything.

Thanks for any help

2012-04-04 08:34
by Michi
try showing toast. - ngesh 2012-04-04 08:37
Sorry, but i don't think showing 500000 Toasts is a good idea. And anyway... i want this information to be logged in a file, not as a Toast - Michi 2012-04-04 08:39
@sandy please not. 50k Toasts and you might need to reboot your device as it will take days to process all.. - WarrenFaith 2012-04-04 08:40
@WarrenFaith .. i agree . - ngesh 2012-04-04 08:41


2

Just a uneducated guess: The Log class doesn't push everything directly, I think it collects and pushes under some specific conditions. As your loop should go very fast, it might be that in some conditions the log buffer (or what else) might be overwritten before it was flushed.

I would try to make it in a thread and call sleep() with some ms in between. Maybe you can reproduce this or find a value where this behavior disappears.

2012-04-04 08:46
by WarrenFaith
You might be right. I just noticed, that several Log messages have the same timestamp. (Almost) every time the timestamp changes, some messages are lost. So the messages are always printed in a burst (same timestamp) and then some are lost, then another burst ... and so on. Time between the bursts is about 0.05 seconds - Michi 2012-04-04 08:53
Good prove for the old saying: Never rely on log messages :P Thanks for sharing your insight - WarrenFaith 2012-04-04 08:56
Yes, even the documentation seems to agree with his guess. See the doc's second "Tip:" on the following page: http://developer.android.com/reference/android/util/Log.htm - Stephan Branczyk 2012-04-04 08:57
Thanks for sharing. But i think that's really a pitty, because it means that you really can't rely 100% on Log messages because they "just might be lost" : - Michi 2012-04-04 09:05