From: Marco Zanon Date: Sun, 16 Nov 2014 14:40:12 +0000 (+0000) Subject: Implemented a log message queue in order to pause/resume logging. X-Git-Tag: 4.0~22 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=1b0ec86af9b98e717810b507316f663b69ff92e2;p=Macaco Implemented a log message queue in order to pause/resume logging. --- diff --git a/src/java/com/marcozanon/macaco/logging/MLogFilter.java b/src/java/com/marcozanon/macaco/logging/MLogFilter.java index ee98b3a..2c771fb 100644 --- a/src/java/com/marcozanon/macaco/logging/MLogFilter.java +++ b/src/java/com/marcozanon/macaco/logging/MLogFilter.java @@ -21,6 +21,9 @@ public class MLogFilter extends MObject { protected LinkedList logTargets = new LinkedList(); + protected boolean pausedState = false; + protected LinkedList logMessageQueue = new LinkedList(); + /* */ 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/src/java/com/marcozanon/macaco/logging/MLogMessage.java b/src/java/com/marcozanon/macaco/logging/MLogMessage.java new file mode 100644 index 0000000..09ddc27 --- /dev/null +++ b/src/java/com/marcozanon/macaco/logging/MLogMessage.java @@ -0,0 +1,40 @@ +/** + * Macaco + * Copyright (c) 2009-2013 Marco Zanon . + * 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; + } + +}