package fi_81.cwp_morse_mangle;

import java.util.concurrent.atomic.AtomicLong;

import android.os.Debug;
import android.util.Log;

/* Log wrapper with on/off switch and formatting input */
public class EventLog {
	private static final boolean logging = true;
	private static final boolean tracing = false;
	private static final AtomicLong recvSignalTime = new AtomicLong(0);
	private static final AtomicLong sendSignalTime = new AtomicLong(0);

	/* Tracing dumps to sd-card */
	public static void startTracing() {
		if (!tracing)
			return;

		Debug.startMethodTracing("cwp_morse_mangle");
		Debug.startAllocCounting();
	}

	public static void endTracing() {
		if (!tracing)
			return;

		Debug.stopMethodTracing();
	}

	/*
	 * Performance profiling for received signals.
	 * 
	 * Note: this is racy, works only when intervals of signals are ~50ms more
	 */
	public static void startProfRecv(long timeReceived) {
		if (!logging)
			return;

		recvSignalTime.set(timeReceived);

		Log.d("profiler", "received signal from network");
	}

	public static void endProgRecv(long timeProcessed, String info) {
		if (!logging)
			return;

		long recvTime = recvSignalTime.getAndSet(0);
		if (recvTime == 0)
			return;

		long duration = timeProcessed - recvTime;

		Log.d("profiler",
				"duration receiving signal from network to handling: "
						+ duration + " ms. (" + info + ')');
	}

	public static void startProfSend(long timeReceived, String info) {
		if (!logging)
			return;

		sendSignalTime.set(timeReceived);

		Log.d("profiler", "sending signal (" + info + ')');
	}

	public static void endProgSend(long timeProcessed) {
		if (!logging)
			return;

		long sendTime = sendSignalTime.getAndSet(0);
		if (sendTime == 0)
			return;

		long duration = timeProcessed - sendTime;

		Log.d("profiler", "duration from sending signal to network: "
				+ duration + " ms.");
	}

	/* Log.[deiw] wrappers */
	public static void d(String tag, String info) {
		if (!logging)
			return;

		Log.d(tag, info);
	}

	public static void i(String tag, String info) {
		if (!logging)
			return;

		Log.i(tag, info);
	}

	public static void w(String tag, String info) {
		if (!logging)
			return;

		Log.w(tag, info);
	}

	public static void e(String tag, String info) {
		if (!logging)
			return;

		Log.e(tag, info);
	}
}

@@ -506,33 +506,32 @@
 acing() %7B%0A%09%09if (
-!
 tracing)%0A%09%09%09retu
@@ -520,35 +520,24 @@
  (tracing)%0A%09
-%09%09return;%0A%0A
 %09%09Debug.star
@@ -572,16 +572,33 @@
 ngle%22);%0A
+%0A%09%09if (logging)%0A%09
 %09%09Debug.
@@ -664,17 +664,16 @@
 %7B%0A%09%09if (
-!
 tracing)
@@ -674,27 +674,16 @@
 acing)%0A%09
-%09%09return;%0A%0A
 %09%09Debug.
@@ -955,32 +955,117 @@
 t(timeReceived);
+%0A%09%09int allocs = Debug.getThreadAllocCount();%0A%09%09int size = Debug.getThreadAllocSize();
 %0A%0A%09%09Log.d(%22profi
@@ -1069,17 +1069,21 @@
 ofiler%22,
- 
+%0A%09%09%09%09
 %22receive
@@ -1103,17 +1103,69 @@
  network
-%22
+. Memory (used/allocs): %22 + size%0A%09%09%09%09%09%09+ %22/%22 + allocs
 );%0A%09%7D%0A%0A%09
@@ -1377,24 +1377,109 @@
  - recvTime;
+%0A%09%09int allocs = Debug.getThreadAllocCount();%0A%09%09int size = Debug.getThreadAllocSize();
 %0A%0A%09%09Log.d(%22p
@@ -1570,17 +1570,16 @@
 n + %22 ms
-.
  (%22 + in
@@ -1572,38 +1572,90 @@
 + %22 ms (%22 + info
- + ')'
+%0A%09%09%09%09%09%09+ %22). Memory (used/allocs): %22 + size + %22/%22 + allocs
 );%0A%09%7D%0A%0A%09public s
@@ -1781,60 +1781,195 @@
 d);%0A
-%0A%09%09Log.d(%22profiler%22, %22sending signal (%22 + info + ')'
+%09%09int allocs = Debug.getThreadAllocCount();%0A%09%09int size = Debug.getThreadAllocSize();%0A%0A%09%09Log.d(%22profiler%22, %22sending signal (%22 + info%0A%09%09%09%09+ %22). Memory (used/allocs): %22 + size + %22/%22 + allocs
 );%0A%09
@@ -2176,16 +2176,101 @@
 endTime;
+%0A%09%09int allocs = Debug.getThreadAllocCount();%0A%09%09int size = Debug.getThreadAllocSize();
 %0A%0A%09%09Log.
@@ -2349,17 +2349,66 @@
  + %22 ms.
-%22
+ Memory (used/allocs): %22 + size + %22/%22%0A%09%09%09%09+ allocs
 );%0A%09%7D%0A%0A%09

Add memory usage logging to profiling methods