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 {
protected boolean pausedState = false;
protected LinkedList<MLogMessage> logMessageQueue = new LinkedList<MLogMessage>();
+ 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();
}
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()) {
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);
}
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.");
}
//
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);
}
--- /dev/null
+/**
+ * Macaco
+ * Copyright (c) 2009-2023 Marco Zanon <info@marcozanon.com>.
+ * 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) {
+ }
+ }
+ }
+
+}