From 42a2705706c6901aec8c864172dd73a588eb1b84 Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Wed, 9 Aug 2023 19:42:54 +0000 Subject: [PATCH] Implemented log filter mark message appender. --- .../com/marcozanon/macaco/MConstants.java | 12 +++ .../marcozanon/macaco/logging/MLogFilter.java | 43 +++++++++-- .../MLogFilterMarkMessageAppender.java | 77 +++++++++++++++++++ 3 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilterMarkMessageAppender.java diff --git a/8.x/src/main/java/com/marcozanon/macaco/MConstants.java b/8.x/src/main/java/com/marcozanon/macaco/MConstants.java index f5f345e..52c6e38 100644 --- a/8.x/src/main/java/com/marcozanon/macaco/MConstants.java +++ b/8.x/src/main/java/com/marcozanon/macaco/MConstants.java @@ -8,8 +8,20 @@ package com.marcozanon.macaco; public class MConstants extends MObject { + /* Generic information. */ + public static final String MACACO_VERSION = "8.x"; + /* Environment configuration. */ + public static final String TEXT_ENCODING = "UTF-8"; + /* Threads. */ + + public static final String LOG_FILTER_MARK_MESSAGE_APPENDER_THREAD_ID = "logFilterMarkMessageAppender"; + public static final long LOG_FILTER_MARK_MESSAGE_APPENDER_SLEEP_INTERVAL = 1000L; // milliseconds + + public static final long LOG_FILTER_MARK_INTERVAL = 60L; // seconds + public static final String LOG_FILTER_MARK_MESSAGE = "."; + } diff --git a/8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilter.java b/8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilter.java index b86d8e3..4c28a8a 100644 --- a/8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilter.java +++ b/8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilter.java @@ -6,7 +6,9 @@ package com.marcozanon.macaco.logging; +import com.marcozanon.macaco.MConstants; import com.marcozanon.macaco.MObject; +import java.time.LocalDateTime; import java.util.LinkedList; public class MLogFilter extends MObject { @@ -23,15 +25,22 @@ public class MLogFilter extends MObject { protected boolean pausedState = false; protected LinkedList logMessageQueue = new LinkedList(); + protected LocalDateTime lastLogMessageDatetime = null; + protected MLogFilterMarkMessageAppender logFilterMarkMessageAppender = null; + /* */ public MLogFilter(MLogFilter.Threshold threshold) { super(); // this.setThreshold(threshold); + // + this.setLogFilterMarkMessageAppender(); } public void close() throws MLoggingException { + this.getLogFilterMarkMessageAppender().setRunMode(MLogFilterMarkMessageAppender.RunMode.STOPPED); + // for (MLogTarget t: this.getLogTargets()) { t.close(); } @@ -76,9 +85,31 @@ public class MLogFilter extends MObject { return this.logTargets; } + /* Mark messages. */ + + protected void setLogFilterMarkMessageAppender() { + this.updateLastLogMessageDatetime(); + // + this.logFilterMarkMessageAppender = new MLogFilterMarkMessageAppender(this); + // + (new Thread(this.getLogFilterMarkMessageAppender(), MConstants.LOG_FILTER_MARK_MESSAGE_APPENDER_THREAD_ID)).start(); + } + + protected MLogFilterMarkMessageAppender getLogFilterMarkMessageAppender() { + return this.logFilterMarkMessageAppender; + } + + protected synchronized void updateLastLogMessageDatetime() { + this.lastLogMessageDatetime = LocalDateTime.now(); + } + + public LocalDateTime getLastLogMessageDatetime() { + return this.lastLogMessageDatetime; + } + /* Output. */ - public void setPausedState(boolean pausedState) throws MLoggingException { + public synchronized void setPausedState(boolean pausedState) throws MLoggingException { this.pausedState = pausedState; // if (!this.getPausedState()) { @@ -90,11 +121,11 @@ public class MLogFilter extends MObject { return this.pausedState; } - public boolean appendMessage(MLogFilter.Threshold level, String message) throws MLoggingException { + public synchronized boolean appendMessage(MLogFilter.Threshold level, String message) throws MLoggingException { return this.appendMessage(level, message, 0); } - public boolean appendSafeMessage(MLogFilter.Threshold level, String message) { + public synchronized boolean appendSafeMessage(MLogFilter.Threshold level, String message) { try { return this.appendMessage(level, message); } @@ -104,7 +135,7 @@ public class MLogFilter extends MObject { return false; } - public boolean appendMessage(MLogFilter.Threshold level, String message, int indentation) throws MLoggingException { + public synchronized boolean appendMessage(MLogFilter.Threshold level, String message, int indentation) throws MLoggingException { if (null == level) { throw new IllegalArgumentException("Invalid 'level': null."); } @@ -117,12 +148,14 @@ public class MLogFilter extends MObject { // if (!this.getPausedState()) { this.flushMessages(); + // + this.updateLastLogMessageDatetime(); } // return true; } - public boolean appendSafeMessage(MLogFilter.Threshold level, String message, int indentation) { + public synchronized boolean appendSafeMessage(MLogFilter.Threshold level, String message, int indentation) { try { return this.appendMessage(level, message, indentation); } diff --git a/8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilterMarkMessageAppender.java b/8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilterMarkMessageAppender.java new file mode 100644 index 0000000..aff2832 --- /dev/null +++ b/8.x/src/main/java/com/marcozanon/macaco/logging/MLogFilterMarkMessageAppender.java @@ -0,0 +1,77 @@ +/** + * Macaco + * Copyright (c) 2009-2023 Marco Zanon . + * See LICENSE for details. + */ + +package com.marcozanon.macaco.logging; + +import com.marcozanon.macaco.MConstants; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; + +public class MLogFilterMarkMessageAppender implements Runnable { + + public static enum RunMode { + INITIALIZING, + RUNNING, + STOPPED; + } + + protected MLogFilter logFilter = null; + + protected MLogFilterMarkMessageAppender.RunMode runMode = null; + + /* */ + + public MLogFilterMarkMessageAppender(MLogFilter logFilter) { + this.setRunMode(MLogFilterMarkMessageAppender.RunMode.INITIALIZING); + // + this.setLogFilter(logFilter); + } + + /* Log filter. */ + + protected void setLogFilter(MLogFilter logFilter) { + if (null == logFilter) { + throw new IllegalArgumentException("Invalid 'logFilter': null."); + } + // + this.logFilter = logFilter; + } + + protected MLogFilter getLogFilter() { + return this.logFilter; + } + + /* Threading. */ + + public synchronized void setRunMode(MLogFilterMarkMessageAppender.RunMode runMode) { + if (null == runMode) { + throw new IllegalArgumentException("Invalid 'runMode': null."); + } + // + this.runMode = runMode; + } + + public synchronized MLogFilterMarkMessageAppender.RunMode getRunMode() { + return this.runMode; + } + + public void run() { + this.setRunMode(MLogFilterMarkMessageAppender.RunMode.RUNNING); + // + while (MLogFilterMarkMessageAppender.RunMode.RUNNING == this.getRunMode()) { + if (MConstants.LOG_FILTER_MARK_INTERVAL <= ChronoUnit.SECONDS.between(this.getLogFilter().getLastLogMessageDatetime(), LocalDateTime.now())) { + this.getLogFilter().appendSafeMessage(MLogFilter.Threshold.STANDARD, MConstants.LOG_FILTER_MARK_MESSAGE); + } + // + try { + Thread.sleep(MConstants.LOG_FILTER_MARK_MESSAGE_APPENDER_SLEEP_INTERVAL); + } + catch (InterruptedException exception) { + } + } + } + +} -- 2.30.2