Implemented a log message queue in order to pause/resume logging.
authorMarco Zanon <info@marcozanon.com>
Sun, 16 Nov 2014 14:40:12 +0000 (14:40 +0000)
committerMarco Zanon <info@marcozanon.com>
Sun, 16 Nov 2014 14:40:12 +0000 (14:40 +0000)
3.x/src/java/com/marcozanon/macaco/logging/MLogFilter.java
3.x/src/java/com/marcozanon/macaco/logging/MLogMessage.java [new file with mode: 0644]

index ee98b3ac9a8cec778f62d65242b8c7012ebfcac5..2c771fbdb782abfe401867c9216658dd72e0276b 100644 (file)
@@ -21,6 +21,9 @@ public class MLogFilter extends MObject {
 
     protected LinkedList<MLogTarget> logTargets = new LinkedList<MLogTarget>();
 
+    protected boolean pausedState = false;
+    protected LinkedList<MLogMessage> logMessageQueue = new LinkedList<MLogMessage>();
+
     /* */
 
     public MLogFilter(MLogFilter.Threshold threshold) {
@@ -75,6 +78,18 @@ public class MLogFilter extends MObject {
 
     /* Output */
 
+    public void setPausedState(boolean pausedState) throws MLoggingException {
+        this.pausedState = pausedState;
+        //
+        if (!this.getPausedState()) {
+            this.flushMessages();
+        }
+    }
+
+    public boolean getPausedState() {
+        return this.pausedState;
+    }
+
     public void appendMessage(MLogFilter.Threshold level, String message) throws MLoggingException {
         this.appendMessage(level, message, 0);
     }
@@ -87,8 +102,23 @@ public class MLogFilter extends MObject {
         if (level.ordinal() > this.getThreshold().ordinal()) {
             return;
         }
-        for (MLogTarget t: this.getLogTargetsReference()) {
-            t.appendMessage(message, indentation);
+        //
+        this.logMessageQueue.add(new MLogMessage(message, indentation));
+        //
+        if (!this.getPausedState()) {
+            this.flushMessages();
+        }
+    }
+
+    protected void flushMessages() throws MLoggingException {
+        while (0 < this.logMessageQueue.size()) {
+            MLogMessage logMessage = this.logMessageQueue.remove();
+            String message = logMessage.getMessage();
+            int indentation = logMessage.getIndentation();
+            //
+            for (MLogTarget logTarget: this.getLogTargetsReference()) {
+                logTarget.appendMessage(message, indentation);
+            }
         }
     }
 
diff --git a/3.x/src/java/com/marcozanon/macaco/logging/MLogMessage.java b/3.x/src/java/com/marcozanon/macaco/logging/MLogMessage.java
new file mode 100644 (file)
index 0000000..09ddc27
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * Macaco
+ * Copyright (c) 2009-2013 Marco Zanon <info@marcozanon.com>.
+ * Released under MIT license (see LICENSE for details).
+ */
+
+package com.marcozanon.macaco.logging;
+
+import com.marcozanon.macaco.MObject;
+
+public class MLogMessage extends MObject {
+
+    protected String message = null;
+    protected int indentation = 0;
+
+    /* */
+
+    public MLogMessage(String message, int indentation) {
+        if (null == message) {
+            throw new IllegalArgumentException("Invalid 'message': null.");
+        }
+        if (0 > indentation) {
+            throw new IllegalArgumentException(String.format("Invalid 'indentation': %s: cannot be negative.", indentation));
+        }
+        //
+        this.message = message;
+        this.indentation = indentation;
+    }
+
+    /* Values */
+
+    public String getMessage() {
+        return this.message;
+    }
+
+    public int getIndentation() {
+        return this.indentation;
+    }
+
+}