common.core: Add Counter events
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 23 Mar 2017 01:55:28 +0000 (21:55 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 20 Apr 2017 02:42:24 +0000 (22:42 -0400)
These events can be used to fill line charts.

Change-Id: I223f3329d4f4d3d04fa365146ed5f7267f9a6d80
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/93666
Reviewed-by: Hudson CI
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/log/LoggerTest.java
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/log/TraceCompassLogUtils.java

index c8a3a5394d03d1f5a076274d529d0ee54fffb95c..f946e77357342c3de54b8dad7b7a1ddd8316d034 100644 (file)
@@ -531,4 +531,22 @@ public class LoggerTest {
         assertEquals("INFO: {\"ts\":0,\"ph\":\"i\",\"tid\":1,\"name\":\"test null key\",\"args\":{\"null\":\"value\"}}", fLog.getMessages().get(1));
     }
 
+    /**
+     * Test counters
+     */
+    @Test
+    public void testCounter() {
+        Logger logger = fLogger;
+        assertNotNull(logger);
+
+        TraceCompassLogUtils.traceCounter(logger, Level.FINER, "counter", "cats", 0);
+        TraceCompassLogUtils.traceCounter(logger, Level.FINER, "counter", "cats", 10);
+        TraceCompassLogUtils.traceCounter(logger, Level.FINER, "counter", "cats", 0);
+
+        fStreamHandler.flush();
+        assertEquals("FINER: {\"ts\":0,\"ph\":\"C\",\"tid\":1,\"name\":\"counter\",\"args\":{\"cats\":0}}", fLog.getMessages().get(0));
+        assertEquals("FINER: {\"ts\":0,\"ph\":\"C\",\"tid\":1,\"name\":\"counter\",\"args\":{\"cats\":10}}", fLog.getMessages().get(1));
+        assertEquals("FINER: {\"ts\":0,\"ph\":\"C\",\"tid\":1,\"name\":\"counter\",\"args\":{\"cats\":0}}", fLog.getMessages().get(2));
+    }
+
 }
index c1d717ffd1c9a46c6f164858b9b2a3ca2992cd9d..3ddfb880af283017a977e1768a0df0fbc5947ed7 100644 (file)
@@ -63,6 +63,12 @@ import org.eclipse.jdt.annotation.Nullable;
  * <ul>
  * <li><strong>R</strong>, Marker event</li>
  * </ul>
+ * </li>
+ * <li>CounterEvents - events that count items
+ * <ul>
+ * <li><strong>C</strong>, Counter event</li>
+ * </ul>
+ * </li>
  * </ul>
  * <p>
  * To use <strong>durations</strong> and/or <strong>flows</strong>, see
@@ -441,8 +447,8 @@ public final class TraceCompassLogUtils {
      * then the object can be re-used, however, the resulting analyses may yield
      * erroneous data if precautions are not taken.
      *
-     * For mutable objects, save the return value of the call. This will be passed
-     * to the destruction of the object and then it can be matched.
+     * For mutable objects, save the return value of the call. This will be
+     * passed to the destruction of the object and then it can be matched.
      *
      * @param logger
      *            The JUL logger
@@ -647,6 +653,31 @@ public final class TraceCompassLogUtils {
         });
     }
 
+    /**
+     * The counter events can track a value or multiple values as they change
+     * over time.
+     *
+     * @param logger
+     *            The Logger
+     * @param level
+     *            The {@link Level} of this event.
+     * @param name
+     *            The name of the asynchronous message
+     * @param args
+     *            The counters to log in the format : "title", value
+     */
+    public static void traceCounter(Logger logger, Level level, @Nullable String name, Object... args) {
+        long time = System.nanoTime();
+        long threadId = Thread.currentThread().getId();
+        logger.log(level, () -> {
+            StringBuilder sb = new StringBuilder();
+            sb.append('{');
+            appendCommon(sb, 'C', time, threadId);
+            appendName(sb, name);
+            return appendArgs(sb, args).append('}').toString();
+        });
+    }
+
     // -------------------------------------------------------------------------
     // Helpers
     // -------------------------------------------------------------------------
This page took 0.028017 seconds and 5 git commands to generate.